Forum Moderators: coopster
Table: game
id___level___stage___name
1______1_______2_____Jim
2______3_______1_____Mark
3______3_______4_____Jane
4______2_______1_____Joyce
5______1_______1_____Kim
6______1_______1_____Matt
7______3_______2_____Mike
8______1_______3_____Steve
What I would like is for it to end up looking like this:
Level 1 - Stage 1
Kim
Matt
Level 1 - Stage 2
Jim
Level 1 - Stage 3
Steve
Level 1 - Stage 4
Level 2 - Stage 1
Joyce
Level 2 - Stage 2
Level 2 - Stage 3
Level 2 - Stage 4
Level 3 - Stage 1
Mark
Level 3 - Stage 2
Mike
Level 3 - Stage 3
Level 3 - Stage 4
Jane
I have this but it doesn't work that well.
$i=1;
$result = mysql_query("SELECT * FROM game ORDER BY level,stage");
while ($row = mysql_fetch_array($result)) {
$level=$row['level'];
$stage= $row['stage'];
$name= $row['name'];
if($episode==$i){
print 'Level '.$level.' - Stage '.$stage'<br />';
$i++;}
print $name'<br />';
}
This does well for the first couple lines but then it stops printing the levels. For the life of me I cannot figure this out. Any help would be great.
Thank you
Matt
you could try something like this and see what happens. I am not a MySQL guru and I would bet this could be done easier with some kind of join in one query but this should get you thinking about alternate solutions atleast.
$result = mysql_query("SELECT name FROM game GROUP BY name");
while ($row = mysql_fetch_array($result)) {
$sub_result = mysql_query("SELECT level,stage FROM game WHERE name = $row[name] ORDER BY level,stage");
while ($sub_row = mysql_fetch_array($sub_result)) {
print "Level $sub_row[level] - Stage $sub_row[stage]<br />";
}
print "$row[name]<br />";
}
$curLevel = '';
$curStage = '';
$result = mysql_query("SELECT * FROM game ORDER BY level,stage");
while ($row = mysql_fetch_array($result)) {
$level=$row['level'];
$stage= $row['stage'];
$name= $row['name'];
if($level != $curLevel ¦¦ $stage != $curStage){
print 'Level '.$level.' - Stage '.$stage'<br />';
$curLevel = $level;
$curStage = $stage
}
print $name'<br />';
}
The one thing that won't give you is headers for level/stages that have no players. If you got the complete list of levels and stages in another table, you can outer join your game table to it, or if all levels have 4 stages you can add a loop inside the if statement to print the intervening headers:
...
if($level != $curLevel ¦¦ $stage != $curStage){// Output missing headers up to the new level.
while($curLevel < $level) {
while($curStage < 4) {
$curStage++;
print 'Level '.$curLevel.' - Stage '.$curStage'<br />';
}
$curLevel++;
$curStage = 0;
}// Output missing headers for new level up to the new stage.
while($curStage < $stage) {
$curStage++;
print 'Level '.$curLevel.' - Stage '.$curStage'<br />';
}// Output header for new level and stage.
print 'Level '.$level.' - Stage '.$stage'<br />';
}
...