Welcome Guest, Not a member yet? Register   Sign In
Wizard application
#1

[eluser]Joshb[/eluser]
Hi, I'm trying to create a wizard style application but am not sure how to do something...

What I want, is that all pages of the wizard appear as being in the root, ie domain.com/ for every page, rather than domain.com/, domain.com/2, domain.com/3...

I have the following code:
Code:
<?php

class Main extends Controller {

    function index()
    {
        $data = array();
        
        if($this->input->server('REQUEST_METHOD') == 'POST')
        {
            $this->_process_info();
        }
        
        $this->load->view('step1', $data);
    }
    
    function _process_info()
    {
        //validation checks
        //
        //if(!valid) echo errors
        //else go to step 2
        $this->_step_2();
    }
    
    function _step_2()
    {
        $this->load->view('step2');
    }
}

In the _process_info() function, if validation is successful, I want to go to _step_2() and exit the index function.

i.e. if _process_info successfully validates the data, it should stop what its doing and go to _step_2, otherwise assign errors to $data for output on the step1 view.

What would be the best way to do that? I'm sure its something easy, I just cant think of it...

Thanks,
Josh
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

Assuming your users will be logged in, you can store the page number and data in the cookie as userdata or flashdata.
#3

[eluser]Dam1an[/eluser]
Whats about something like this
Code:
class Main extends Controller {
    public function index() {
        $this->_step_1();
    }
    
    private function _step_1() {
        // set rules
        if($this->form_validation->run() == false) {
            // not run/fails valiation
            $this->load->view('step_1');
        } else {
            // process data, save to database etc
            
            // Move onto step 2
            $this->_step_2();
        }
    }
    
    .. same for step 2, 3 etc
}

You never technically redirect from index, so if Main is your default controller, the URL should never change
It could/should work in theory, but not tested
#4

[eluser]Joshb[/eluser]
Thanks for the quick replies - I've used a combination of both. The codes now running something like this:

Code:
class Main extends Controller {

    function Main()
    {
        parent::Controller();
        $this->load->helper('form');
        $this->load->library('form_validation');
    }
    
    function index()
    {
        if($this->session->userdata('wizard_page'))
        {
            switch($this->session->userdata('wizard_page'))
            {
                case 2:
                    $this->_page_2();
                    break;
                case 3:
                    $this->_page_3();
                default:
                    die('An error occured. Attempted to find page '.$this->session->userdata('wizard_page'));
                    break;
            }
        }
        else
        {
            $this->form_validation->set_rules('firstname', 'First Name', 'required');
            if($this->form_validation->run())
            {
                // Form is valid, do whatever is needed
                
                // Set session data
                $this->session->set_userdata('wizard_page', 2);
                redirect('/', 'location');
            }
            else
            {
                $this->load->view('page1');
            }
        }
    }
    
    function _page_2()
    {
        if($this->form_validation->run())
        {
            // Form is valid, do whatever is needed
            
            // Set session data
            $this->session->set_userdata('wizard_page', 3);
            redirect('/', 'location');
        }
        else
        {
            $this->load->view('page2');
        }
    }
    
    function _page_3()
    {
        // As _page_2()
    }
}

Thanks for your help,

Josh




Theme © iAndrew 2016 - Forum software by © MyBB