[thelist] sql group one field

Damien COLA damiencola at wanadoo.fr
Wed Sep 1 04:14:36 CDT 2004


The one shot way is simply :
query : SELECT serie, model FROM `tablename` ORDER BY model

then in php you loop the whole result set and each time the model
changes you create a new key in the array with the name of the model, if
the model doesn't change you simply add the 'serie' value to the array
set by the key of the current model.

php code in email (not verified) :

$results = mysql_query("SELECT serie, model FROM `tablename` ORDER BY
model"); 
$output = array(); 
$mykey = '';
while($row = mysql_fetch_assoc($results)) {
   if ($row[model] != $mykey) $output[$row[model]] = array();

   array_push($output[$row[model]], $row[serie]);	
}

at the end you should have an array of arrays with keys being the model
names.

----- original message
I'm sure there is a one-shot way of doing this, but here would be my
attempt (PHP example to show algorithm -- without error checking):

$results = mysql_query("SELECT UNIQUE model FROM `tablename`"); $output
= array(); while($row = mysql_fetch_assoc($results)) {
   $query = "SELECT serie FROM `tablename` WHERE model =
'".$row['model']."'";
   $r2 = mysql_query($query);
   while($row2 = mysql_fetch_assoc($r2))
   {
      $temp[] = $row2['serie'];
   }
   $output[$row['model']] = implode(",",$temp);
   $temp = array();
}

This would give you:

echo $output['abc'];

1234,1233,1222


-- 

News! - Evolt.org conference for web professionals. 
17-19 September 2004 in Toronto, Canada. 
Details at http://TOevolt.org

* * Please support the community that supports you.  * *
http://evolt.org/help_support_evolt/

For unsubscribe and other options, including the Tip Harvester 
and archives of thelist go to: http://lists.evolt.org 
Workers of the Web, evolt ! 



More information about the thelist mailing list