0

I am new to CodeIgniter and working with arrays. I am trying to get the value of each check box into the same row in the database. I know I need to loop through the array, but then I am not sure how to tell it to write the values under their corresponding headings in the database. This is what I have tried but its writing 0 to the database for all options. Thanks to anyone taking the time to awnser my question.

//view

<input type="text" name="name" /> Name
<input type="checkbox" value="1" name="options[]" /> Option 1
<input type="checkbox" value="2" name="options[]" /> Option 2
<input type="checkbox" value="3" name="options[]" /> Option 3
<input type="checkbox" value="4" name="options[]" /> Option 4

//controller

//require at least 1 checkbox
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('options[]', 'Options', 'required');

//not sure how to do this part, 
//some how get values to the corresponding "option"
$name = $this->input->post('name');
$option1 = $this->input->post('options[0]');
$option2 = $this->input->post('options[1]');
$option3 = $this->input->post('options[2]');
$option4 = $this->input->post('options[3]');

//insert to database
$this->load->model('add', 'add_model');
$this->add_model->create_person($name, $option1, $option2, $option3, $option4);

//database

| id | name | option1 | option2 | option3 | option4 |

2
  • Maybe I'm missing something but why dont you use different name attribute for each of the checkboxes since the values will be inserted in different columns? i.e. <input type="checkbox" value="3" name="option3" /> Option 3 Commented Aug 31, 2012 at 15:28
  • I used the options[] array because the validation requires at least 1 checkbox to be selected. Would there be a better way to go about this? Commented Aug 31, 2012 at 15:32

1 Answer 1

1

I personally would have different named checkboxes and would validate by jQuery in the view before sending the form, but here's the code that will point you in a right direction.

When you submit your form options array will have as many elements as you have selected on the form. If no checkbox has been selected the array would be empty so your code:

$option3 = $this->input->post('options[2]');

would be trying to get the third element of an empty array.

You need to loop through existing elements and assign those values to your variables. You can do that by using this code:

$myArr = array(1 => 0, 2 => 0, 3 => 0, 4 => 0);   //set all the variables to 0
foreach($_POST['options'] as $checked)          //loop throught the selected options
{
    $myArr[$checked] = $checked;                     //assign to corresponding array element                            
}

$this->add_model->create_person($name, $myArr[1], $myArr[2], $myArr[3], $myArr[4]);

The code above isn't particularly elegant but I hope it will help you

Sign up to request clarification or add additional context in comments.

1 Comment

You should use $this->input->post('options') instead of $_POST['options'] so that CI can do XSS filtering on the input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.