Welcome Guest, Not a member yet? Register   Sign In
checkboxes headaches... help...
#1

[eluser]benmanson[/eluser]
Hi,

I am hoping this is something simple that I am doing wrong here. I have a series of checkboxes in a form with values from 1 to 24 ie:

Code:
&lt;input type="checkbox" name="categories[]" value="1"&gt; Investment Banking<br>
&lt;input type="checkbox" name="categories[]" value="2"&gt; Corporate and Institutional Banking<br>
...
&lt;input type="checkbox" name="categories[]" value="23"&gt; Logistics and Distribution<br>
&lt;input type="checkbox" name="categories[]" value="24"&gt; Education<br>

The problem I am having is in storing those values in my database, I only seem to get the first digit of the value, so the checkbox with a value of '24' is being read as '2'.

Here is the code in my controller that grabs the array via the post, and then uses a model to insert it into the database:

Code:
$staff_data['id'] = $this->input->post('id');
$categories = $this->input->post('categories');

if ($categories !== FALSE) {
                    
  foreach ($categories as $row) {
    $cat_data['cid'] = $row['categories'];
    $cat_data['sid'] = $staff_data['id'];
    $this->Cms_model->add_lookup($cat_data);
  }
}

When I print_r($categories) it outputs the array fine, the problem is in the foreach loop I think, because when I add a line to the loop to output the 'cid' and 'sid' values, it outputs the first digit for 'cid' only.

Hopefully someone can point out the error of my ways...

cheers
Ben
#2

[eluser]kgill[/eluser]
Just a quick thought, but shouldn't:

$cat_data['cid'] = $row['categories'];

just be:

$cat_data['cid'] = $row;

- K
#3

[eluser]benmanson[/eluser]
thanks K - worked like a charm + so simple, you're a lifesaver.

Is that because the array isn't multi-dimensional?

cheers
Ben
#4

[eluser]kgill[/eluser]
Yes, your $categories array contains one key/value pair for each checkbox that was submitted, so when you do a foreach on it the value being put into $row is the value from the form.
#5

[eluser]bluepicaso[/eluser]
Thanx people above....really nice..
Smile




Theme © iAndrew 2016 - Forum software by © MyBB