CodeIgniter Forums
Match 2 arrays to declare selected on checkbox - 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: Match 2 arrays to declare selected on checkbox (/showthread.php?tid=54064)



Match 2 arrays to declare selected on checkbox - El Forum - 08-21-2012

[eluser]ppwalks[/eluser]
Hi all,

I am trying to create an "edit" product section where they are connected through a joining table. I have one array which contains the current selected id's drawn from the joining table and I want to match that to the generated checkboxes from another array, basically if it matches the key then selected else not.

Here are my arrays:
Code:
Array ( [0] => Array ( [product_id] => 327 [product_name] => segfedsgfds [description] => 32sd2e [discounted_price] => 0.00 [price] => 0.00 [image] => imac_2.jpg [image_2] => [thumbnail] => imac_2.jpg [meta_keywords] => [meta_description] => [category_ids] => 1,4,7 [category_names] => French,Animal,Valentine's ) )

the [category_ids] are the previous selected and my generated select boxes:

So I then explode the comma separated values to create this array of selected:
Code:
Array ( [0] => 1 [1] => 4 [2] => 7 ) (Selected category ids)
Here my array that builds the categories
Code:
Array ( [0] => Array ( [category_id] => 1 [name] => French ) [1] => Array ( [category_id] => 2 [name] => Italian ) [2] => Array ( [category_id] => 3 [name] => Irish ) [3] => Array ( [category_id] => 4 [name] => Animal ) [4] => Array ( [category_id] => 5 [name] => Flower ) [5] => Array ( [category_id] => 6 [name] => Christmas ) [6] => Array ( [category_id] => 7 [name] => Valentine's ) [7] => Array ( [category_id] => 8 [name] => nick s cat ))
So where the first array contains the category ids of 1,4,7
I would like to have the checkboxes selected on those values when the check boxes are generated, any help would be greatly appreciated.

thanks

Paul




Match 2 arrays to declare selected on checkbox - El Forum - 08-21-2012

[eluser]DarkManX[/eluser]
pls use [code] tags for yours code, its exhausting to read code like that.


Match 2 arrays to declare selected on checkbox - El Forum - 08-21-2012

[eluser]ppwalks[/eluser]
Apologies, now ammended


Match 2 arrays to declare selected on checkbox - El Forum - 08-21-2012

[eluser]DarkManX[/eluser]
Code:
<?php foreach($checkboxes as $box):?>
<p>
  &lt;input
   type="checkbox"
   value="&lt;?=$box['category_id'];?&gt;"
   &lt;?=(in_array($box['category_id'],$selected_cat_ids))?'checked':'';?&gt;&gt;
  &lt;?=$box['name'];?&gt;
</p>
&lt;?php endforeach;?&gt;

Well, i discovered the forum highlighting doesnt work with alternative php syntax, anyway, thats the way you can solve your problem.


Match 2 arrays to declare selected on checkbox - El Forum - 08-21-2012

[eluser]ppwalks[/eluser]
Sorry For late Reply, I used your in array method and worked like a charm, thanks a bunch