Re: multi-row count advice needed.
| From: | php3 at developersdesk dot com | Date: | Wed, 27 Dec 2000 07:57:54 +0000 |
| Subject: | Re: multi-row count advice needed. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-31927@lists.php.net to get a copy of this message | ||
Addressed to: Jenny Koozer <jennykoozer@yahoo.com>
php-general@lists.php.net
** Reply to note from Jenny Koozer <jennykoozer@yahoo.com> Tue, 26 Dec 2000 21:42:38 -0800
(PST)
>
> Hi all!
>
> I have a bunch of rows and am counting the "number of instances"
> within each. Here's what I've figured thus far, am looking for tips
> on "doing it more efficiently" as to lesson workload on server :
>
> $result = mysql_query("SELECT foo FROM table");
>
> while($row = mysql_fetch_array($result)) {
>
> if ($row['foo'] == 'a') { $count['a']++; } elseif
> ($row['foo'] ==
> 'b') { $count['b']++; } elseif ($row['foo'] == 'c') {
> $count['c']++;
> } else { $count['z']++; }
>
> }
>
> Created is an array full of row totals, which is desired. Other ways
> to do this?
Use the database to get the counts if that is all you are looking for.
SELECT foo, count(*)
FROM table
GROUP BY foo
ORDER BY foo
If you are doing other things, and want to count of instances of
foo as part of other things:
while( list( $foo, ... ) = mysql_fetch_row( $result )) {
$Count[ $foo ] ++;
}
Rick Widmer
Internet Marketing Specialists
http://www.developersdesk.com