Looping twice.

From: Date: Wed, 10 Jan 2001 13:30:13 +0000
Subject: Looping twice.
Groups: php.db 
Request: Send a blank email to php-db+get-5778@lists.php.net to get a copy of this message
I am struggling with displaying a query and I could use some help. Any direction or advice would be greatly appreciated. I have a query that returns 3 fields, name, sub_area, and description. (actually it returns others but these three will be enough to describe.) I order the query by sub_area, name. My goal is to list the results in a group for each sub_area. (i.e. $sub_area has $counter records and they are...) My attempt at this is to loop though the records several times, once to create an array of the sub_area and their count, then one loop each for each sub_area to process the records. This sure seems like a lot of looping through records! There must be a better way, I think I am just stuck looking at this from one direction. Your help will be appreciated. <? $sub_count = 1; $array_count = 0; $last_area = ''; while ($f = db_fetch_object($qid)) { $this_area = $f->sub_area; if ($last_area == '') { $last_area = $this_area; $sub_count=1; } else if ($this_area != $last_area) { $area_list[$array_count][0] = "$last_area"; $area_list[$array_count][1] = "$sub_count"; $last_area = $this_area; $sub_count=1; $array_count++; } else { $sub_count++; } } $area_list[$array_count][0] = "$last_area"; $area_list[$array_count][1] = "$sub_count"; mysql_data_seek($qid,0); foreach (array_keys($area_list) as $bar) { print($area_list[$bar][0].' has '.$area_list[$bar][1].', they are:\n'); mysql_data_seek($qid,0); while ($r = db_fetch_object($qid)) { if ($r->sub_area == $area_list[$bar][0]) { print($name.'<br>); } } } ?> Thom Dunaway thom@wheretostay.com www.where2stay.com

« previous php.db (#5778) next »