How do I make these fields autopopulate from the database?

Posted by dmanexe on Stack Overflow See other posts from Stack Overflow or by dmanexe
Published on 2010-04-09T21:24:16Z Indexed on 2010/04/09 21:33 UTC
Read the original article Hit count: 316

Filed under:
|
|
|
|

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 echo within a loop if the item going through the loop exists within another array.

© Stack Overflow or respective owner

Related posts about php

Related posts about loops