views:

26

answers:

3

I have an array, which holds values like this:

$items_pool = Array ( 
[0] => Array ( [id] => 1 [quantity] => 1 ) 
[1] => Array ( [id] => 2 [quantity] => 1 ) 
[2] => Array ( [id] => 72 [quantity] => 6 ) 
[3] => Array ( [id] => 4 [quantity] => 1 )
[4] => Array ( [id] => 5 [quantity] => 1 ) 
[5] => Array ( [id] => 7 [quantity] => 1 ) 
[6] => Array ( [id] => 8 [quantity] => 1 ) 
[7] => Array ( [id] => 9 [quantity] => 1 ) 
[8] => Array ( [id] => 19 [quantity] => 1 ) 
[9] => Array ( [id] => 20 [quantity] => 1 ) 
[10] => Array ( [id] => 22 [quantity] => 1 ) 
[11] => Array ( [id] => 29 [quantity] => 0 ) 
) 

Next, I have a form that I am trying to populate. It loops through the item database, prints out all the possible items, and checks the ones that are already present in $items_pool.

<?php foreach ($items['items_poolpackage']->result() as $item): ?>

<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>"> 

<?php endforeach; ?>

I know what logically I'm trying to accomplish here, but I can't figure out the programming.

What I'm looking for, written loosely is something like this (not real code):

<input type="checkbox" name="measure[<?=$item->id?>][checkmark]" value="<?=$item->id?>" <?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>>

Specifically this conditional loop through the array, through all the key values (the ID) and if there's a match, the checkbox is selected.

<?php if ($items_pool['$item->id']) { echo "SELECTED"; } else { }?>

I understand from a loop structured like this that it may mean a lot of 'extra' processing.

TL;DR - I need to loop within the array, check for the key 'id', then print a string.

A: 

The proper attribute is checked="checked"...

if (isset($item_pool[$item->id]) && $item_pool[$item->id] > 0) {
    echo ' checked="checked"';
}
Brant
That conditional doesn't post if there's a match. The array isn't labeled true to the ID, it needs to cycle through each array and compare the ID from the item to any ID in the array.
dmanexe
A: 
<?php echo ($items_pool[$item->id] ? ' checked="checked" ' : ''); ?>

Another option would be to use array_keys to grab the keys of the selected items and then do something like in_array($item->id, $item_keys). All depends on how you feel like doing the work. The tertiary operators are the easiest way to do what you want to inline. You can also use a simple if statement to echo the attribute if you want it.

if($items_pool[$item->id]) echo ' checked="checked"';
Jeff Rupert
The problem with this is that the item's ID doesn't relate to the array, meaning $items_pool[n] where n is an auto-incrementing (1, 2, 3)...it almost has to be like $items_pool[][$item->id]
dmanexe
+1  A: 

If I understand correctly, you need something like this?

array_walk($items_pool, create_function('$array', 'global $item; if( in_array($item->id, $array) ) { echo "checked=\"checked\""; }'));

Conside this though, if you stored your products in a single dimensional array (presuming the ids will always be unique

$items_pool = array(id, quantity)

You will not end up with having duplicated entries of products and can incremement/decrement the quantity easier.

$items_pool[id]++; /* or */ $items_pool[id] = $items_pool[id] + 2;

Both techniques should work if the id exists in the array or not.

If you are hoping to store other attritubes in this array other than quantity, you can

$items_pool = array(id=>array(quantity=>3, colour=>"red"));    
$items_pool[id][quantity]--;
bigstylee