
February 27th, 2007, 10:05 AM
| | | How to remove row from ezSQL object?
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. | 
February 27th, 2007, 11:25 AM
| | | Re: How to remove row from ezSQL object?
deko wrote: Quote:
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
================== | 
February 27th, 2007, 07:25 PM
| | | Re: How to remove row from ezSQL object?
deko wrote: Quote: Quote:
>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.
| Unfortunately, the problem is more complex than those example queries make it
look (they were simplified for clarity).
ezSQL aside, I think the issue is removing an item from an object in PHP. I'm
assuming there's a way to do this... (?) | 
February 27th, 2007, 10:15 PM
| | | Re: How to remove row from ezSQL object?
ezSQL aside, I think the issue is removing an item from an object in PHP
I tried this:
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, current($itemdata), 1);
}
}
};
which sees to work, but not accurately... if the matched item_datum is the first
in the array, the second element is removed. | 
February 27th, 2007, 10:35 PM
| | | Re: How to remove row from ezSQL object?
$i =1
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 - 2], 1);
reset($itemdata);
}
}
}
This seems to work.
Is there a way to access the array indexer so I don't have to use $i?
I tried prev($itemdata) but could not get that to work for some reason.... | 
February 28th, 2007, 12:05 AM
| | | Re: How to remove row from ezSQL object?
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... ? | 
February 28th, 2007, 01:55 AM
| | | Re: How to remove row from ezSQL object?
deko wrote: Quote:
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...es.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
================== |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|