[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
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');
}
}
}
?>