|
Posted by Jerry Stuckle on 02/28/07 01:46
deko wrote:
> 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... ?
>
http://us2.php.net/manual/en/control-structures.foreach.php
and
http://us2.php.net/manual/en/function.unset.php
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|