Welcome Guest, Not a member yet? Register   Sign In
What structure (MVC) should I use / simple app
#1

[eluser]jorre[/eluser]
I'm trying to build my first, relatively simple application with CI...

Since I'm stuck at the moment... maybe I could just start over and ask the experts here first, what basic structure I should use to accomplish the following:


1. I want to display a simple form with 2 input fields (name/email) -> this works Wink
2. When these are filled in, and user clicks the NEXT button, some 5 input boxes are shown (to create a poll)
3. When at least two of these are filled in, the user can press SAVE and the records are stored in database (name/email/poll_ID in 1 table, and poll_option_1, poll_option_2, poll_option_3, ... in another table, together with the poll_ID from the first table.

That's pretty much what I want to do, if I accomplish that much with CI already, I think I'm ready to build my app further...

So , what's the structure I should use for this? right now I have the following:

1 controller [has a function create1 [for step1] en a function create2 [for step2]
3 views -> one for step1, another for step2 [see above] and then a succes message [view 3]


this works 50%, but I'm stuck with the other 50%.

this works:


- showing form 1
- showing form 2

this doesn't work

-> saving data from form 1 AND 2 only after the final step -> when a user clicks SAVE in the last form/view

I added my controller code to this post, just to give you an idea of how an OOP-newbie is trying to get his first CI-app online!

Thanks a lot for helping me out here



Code:
<?php
      class Poll extends Controller
    {
        function Poll()
        {
            parent::Controller();
        }
        
        function index()
        {
            $this->load->view('pollview');
        }
        
        function create()
        {
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            
            //set validation rules
            $rules['poll_topic'] = "trim|required";
            $rules['poll_yourname'] = "trim|required";
            $rules['poll_youremail'] = "trim|valid_email";
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_topic'] = 'Poll topic';
            $fields['poll_yourname'] = 'Your name';
            $fields['poll_youremail'] = 'Your email address';
            $this->validation->set_fields($fields);
            
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s is required');
                        
            //run form validation check            
            if($this->validation->run() == FALSE)
            {
                //SHOW STEP 1
                $this->load->view('header');    
                $this->load->view('pollcreate_step1');    
                $this->load->view('footer');    
            }
            else
            {
                //SHOW STEP 2
                $this->load->view('header');    
                $this->load->view('pollcreate_step2');    
                $this->load->view('footer');
            }
        }
        
        
        function create_step2()
        {
            $this->load->helper(array('form', 'url'));
            $this->load->library('validation');
            //set validation rules
            $rules['poll_choice1'] = "trim|required";
            $rules['poll_choice2'] = "trim";    
            $rules['poll_choice3'] = "trim";    
            $rules['poll_choice4'] = "trim";    
            $rules['poll_choice5'] = "trim";    
            $rules['poll_choice6'] = "trim";    
            $rules['poll_choice7'] = "trim";    
            $rules['poll_choice8'] = "trim";    
            $rules['poll_choice9'] = "trim";    
            $rules['poll_choice10'] = "trim";    
            $this->validation->set_rules($rules);
            
            //set default names for display in error message
            $fields['poll_choice1'] = 'At least one option must be available in any poll';
            $this->validation->set_fields($fields);
                        
            //apply custom error formatting
            $this->validation->set_error_delimiters('<div class="error">', '</div>');
            $this->validation->set_message('required', '%s');
            
            //run form validation check            
            if($this->validation->run() == FALSE)
            {
                //SHOW STEP 1
                $this->load->view('header');    
                $this->load->view('pollcreate_step2');    //reload step 2
                $this->load->view('footer');    
            }
            else
            {
                //LOAD THE DATABASE MODEL AND INSERT THE NEW POLL
                $this->load->model('Poll_model','', TRUE);
                $this->Poll_model->add_poll_options();
                
                $this->load->view('header');    
                $this->load->view('messages/poll_success');    //show success message
                $this->load->view('footer');    
            }
                        
        }
                
        
    }
?&gt;
#2

[eluser]Edemilson Lima[/eluser]
I cannot saw where you did get the user submitted data. To do this, you must use:
Code:
$this->input->post('field_name');
If you want to keep the user submitted data for more than one request, you must store it in a session or pass them to the next request using flash variables. To store in a session, you must use:
Code:
$this->session->set_userdata('var_name',$data);
To get the session data, use:
Code:
$this->session->userdata('var_name');
Remember that CI session based on cookies do not store too much user data, since the session cookie can only store 4 KB. There are another options, like Native Session, based on PHP session engine.
#3

[eluser]jorre[/eluser]
I'm still not there, but thanks a lot already for giving me the tip about using sessions... trying to implement that right now, but I can't seem to be able to pass data from my controller to my view...

I keep getting "unknown variable" for some reason...
#4

[eluser]Pascal Kriete[/eluser]
The variables in your view will be the keys of the array you pass in.
Code:
//Controller
$data['test'] = 'Howdy';
$this->load->view('whatever', $data);

//View
&lt;?= $test ?&gt;

//Result
Howdy

That should work. Big Grin.
#5

[eluser]jorre[/eluser]
works like a charm! thank you !




Theme © iAndrew 2016 - Forum software by © MyBB