Web Hosting Talk







View Full Version : php grouping


lexington
12-19-2007, 06:26 AM
Hello, I have tried searching but I cannot find an example about this. How do I make it so that I could run a query that orders something by group name (that is the easy part I know how to do that) but what I would like it to do is display a title for each group. Like if the rows in the DB are like name, type, and I wanted to display the order by name and have the type title appear on the page? Such as:

NAME ---- TYPE

Males
aman --- Male
bman --- Male
cman --- Male
Females
awoman --- Woman
bwoman --- Woman
cwoman ---- Woman
Unknown
aunknown
bunknown
cunknown

Basically without using multiple while statements and causing a huge load. A while back someone created something like this with multiple while statements and the page took about 45 seconds to load. I have a complex script that is able to accomplish this task quickly but it is too complex to be able to simple copy and paste to acheive the same effect with another script. Thanks!

ergo
12-19-2007, 08:43 AM
for example you can do on every iteration a conditional check if $lastGrpName==$newGrpName, but will generate a lot of unnecessary if's if you expect lot of items to be shown on one page, if there would be less than 100 items per page it would be sufficient solution

sasha
12-19-2007, 09:28 AM
$current_title = '' ;
$out = '';
while ($r = mysql_fetch_assoc ($res) ) {
if ($r['group'] != $current_title) {
$current_title = $r['group'] ;
$out .= "<title>$current_title</title>" ;
}
$out .= "<name>{$r['name']}</name> -- <group>{$r['group']}</group>";
}
echo $out;

ergo
12-19-2007, 09:29 AM
thats exactly what i stated above :]

sasha
12-19-2007, 10:26 AM
thats exactly what i stated above :]

Yeah, but now it is all pretty and in colors :)

lexington
12-19-2007, 03:21 PM
Haha @ pretty. Hey sasha how are ya and thanks :)