Reply to Re: How to remove row from ezSQL object?

Your name:

Reply:


Posted by Jerry Stuckle on 02/27/07 11:08

deko wrote:
> I need to loop through a number of categories (in object $category) and
> compare items (from object $itemdata) to create a list of items
> organized by category.
>
> 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;
> }
> }
> }
>
> The output should look something like this:
>
> Apples (cat_name)
> -- red (item_name)
> -- green
> -- small
> -- large
>
> Oranges
> -- ripe
> -- rotten
>
> The $category and $itemdata objects are created using ezSQL:
>
> $category = $db->get_results("SELECT cat_id, cat_name ... FROM Table_A");
>
> $itemdata = $db->get_results("SELECT category_id, item_name ... FROM
> Table_B");
>
> My question is this:
>
> How do I optimize this?
>
> Once an $item_datum->category_id has been matched to a
> $cat_item->cat_id, I no longer need that row ($item_datum) in the
> $itemdata object, so further iterations of the nested foreach loop need
> not include the previously matched $item_datum.
>
> How do I remove the matched $item_datum from the $itemdata object after
> it has been matched?
>
> Will this reduce iterations of the nested foreach loop?
>
> Thanks in advance.
>

I don't use ezSQL, but you should be able to get all of your items by
category id, i.e.

SELECT cat_id, cat_name, item_name ... FROM Table_A, Table_B WHERE
TableA.cat_id = Table_B.category_id ORDER BY cat_name, item_name

Or similar. Now just go through the list. Every time your cat_name
changes, output cat_name followed by item_name. If cat_name doesn't
change, just output item_name.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация