|
Posted by deko on 02/27/07 23:56
This is a better example:
$category = $db->get_results("SELECT cat_id, cat_name ... FROM Table_A");
$itemdata = $db->get_results("SELECT category_id, item_name ... FROM Table_B");
$i =0
foreach ($category as $cat_item)
{
echo $cat_item->cat_name;
foreach ($itemdata as $item_datum)
{
if ($item_datum->category_id == $cat_item->cat_id)
{
echo '--'.$item_datum->item_name;
array_splice($itemdata, $item_data[$i - 1], 1);
//the next category will not contain the matched item
//since items can only belong to one category
//so we can remove matched item_datum from itemdata
//and reduce iterations next time we are in this loop
}
$i++;
}
}
Apples (cat_name)
-- red (item_name)
-- green
-- small
-- large
Oranges
-- ripe
-- rotten
$itemdata is an object, not an array, which is why I need to index it with $i
(object items do not have numeric keys). So $i indexes the items in the
$itemdata object, allowing me to use array_splice() on an object... ?
[Back to original message]
|