CodeIgniter Forums
checkbox arrays - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: checkbox arrays (/showthread.php?tid=30642)



checkbox arrays - El Forum - 05-21-2010

[eluser]elmne[/eluser]
I'm trying to retrieve checkbox values from an array so that the checked id numbers in the array can be processed.

I do it like this

In the view, i declare the checkbox like this

Code:
<tr>


        <td>&lt;input type="checkbox" name="checkbox[&lt;?php echo $row-&gt;customer_type_id ?&gt;]" value="&lt;?php echo $row->customer_type_id ?&gt;" /></td>    


</tr>

Then when the form is submitted, i try to process it like this

Code:
$checked = $this->input->post('checkbox[]');


        $checked = $this->input->post('checkbox[]');
        $count = count($checked);
        for($i=1; $i < $count; $i++)

Followed by the sql command that uses
Code:
'".$checked[$i]."'
as a parameter.

But it gives me an error. The sql command fails to work even though no error is shown.

What's wrong with the above code?


checkbox arrays - El Forum - 06-11-2010

[eluser]daelsepara[/eluser]
you can try this code:

Code:
$checked = $this->input->post('checkbox');
foreach($checked as $key => $value)
{
// your sql command would then use:
// '".$key."'
//
// where $key is index to your checkbox item
// $value would indicate whether it was checked or not

$sql = "SELECT * from some_table WHERE id = '$key'";
}