[PHP-DB] Array/DB match counting From: Dave VanAuken (dave <email protected>)
Date: 12/18/00

Am doing a postgres list of categories... want to count and display the
number of items (items in seperate table from categories) that match each
category while displaying the categories to the user.

Two questions:

1. Is is faster to do the below or to do another database select to parse
all the items as we go through each category in the category database
result...

2. Optimization of the below code?

# get all item categories
$resultCA=pg_exec($database,"SELECT Cat FROM ItemList ORDER BY CatID");
$CAnum=pg_numrows($resultCA);
$CAcount=0;
while($CAcount<$CAnum) {
        $CACapture[]=pg_result($resultCA,$CAcount,0);
        $CAcount++;
}
$CAresult=array_count_values($CACapture); # count items

# then in wour while loop that we are displaying the categories with
# not relavent but assume $Category is name of category
# and $CatID is id num of Category from seperate category search
        echo "$Category ($CAresult[$CatID])<br>\n";

This displays perfectly what I am looking for which is:

        My Category (8)
        Next Category (10)

Dave

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe <email protected>
For additional commands, e-mail: php-db-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>