Re: multi-row count advice needed.
| From: | Kyle Jerviss | Date: | Wed, 27 Dec 2000 06:46:04 +0000 |
| Subject: | Re: multi-row count advice needed. | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31926@lists.php.net to get a copy of this message | ||
$possibilities=array('a','b','c','z');
reset($possibilities);
while(list($key,$val)=each($possibilities)){
$temp=mysql_fetch_array(mysql_query("SELECT COUNT(foo) AS cnt ".
"FROM table WHERE foo=\"".$val."\""));
$count[$val]=$temp['cnt'];
}
If you've got a really, really HUGE number of possibilities, and it's
killing your server, make a new table with counts, and update them when
you update the rest of your data.
Jenny Koozer wrote:
>
> 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?
>
> TIA!
>
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Shopping - Thousands of Stores. Millions of Products.
> http://shopping.yahoo.com/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net