Jim R Posted September 4, 2011 Share Posted September 4, 2011 I'm trying to output this: Status Player Player Player Status Player Player Player Status Player Player I tried this, but it didn't work: while($players = mysql_fetch_assoc($results)) { foreach ($players as $player=>$status){ echo '<div>' . $player['status'] . '</div>'; foreach ($status as $key) { echo $key['playerFirst'] . ' ' . $key['playerLast'] . ', '. $key['year'] . '<br>'; } } } I guess I'm trying to find a more efficient way of doing this rather than if/elseif for each one. Right now, there are only four groupings, but in the future there could be as many 406. Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/ Share on other sites More sharing options...
Psycho Posted September 4, 2011 Share Posted September 4, 2011 It would have been helpful if you gave an explanation of the data being returned. You should be running your query to return data in this fashion (ordered by status first): player_name | status Player 1 | Status 1 Player 2 | Status 1 Player 3 | Status 2 Player 1 | Status 2 Player 4 | Status 2 Player 5 | Status 3 Player 6 | Status 3 Player 7 | Status 4 Player 8 | Status 4 Then your display code would look something like this $currentStatus = false; //Flag to detect change in status while($player = mysql_fetch_assoc($results)) { if($currentStatus != $player['status']) { //Status has changed, display status header $currentStatus = $player['status']; echo "<div>{$currentStatus}</div>\n"; } //Display player info echo "{$key['playerFirst']} {$key['playerLast']}, {$key['year']}<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265359 Share on other sites More sharing options...
voip03 Posted September 4, 2011 Share Posted September 4, 2011 Can you use CSS? Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265394 Share on other sites More sharing options...
Jim R Posted September 4, 2011 Author Share Posted September 4, 2011 Sorry, sort of took it for granted. Table "schools", just matches the User, which is a coach, to his team/players. All the information being output is from "players", one status per player. $sql = "SELECT * FROM players as p INNER JOIN schools as s WHERE p.tid = s.id ORDER BY status, playerLast"; $results = mysql_query($sql); Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265396 Share on other sites More sharing options...
Jim R Posted September 4, 2011 Author Share Posted September 4, 2011 @mjdamato What you produced works pretty well. It doesn't output the player information though. The structure shows up, as best as I can tell exactly how I want it. @voip03 Do you mean to determine the structure? Certainly you can to determine how it actually looks, whether you want to use a table or list format. In this instance, I just need a list. Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265401 Share on other sites More sharing options...
Psycho Posted September 4, 2011 Share Posted September 4, 2011 Sorry, I didn't pay attention to the variables used in the player output. This should work $currentStatus = false; //Flag to detect change in status while($player = mysql_fetch_assoc($results)) { if($currentStatus != $player['status']) { //Status has changed, display status header $currentStatus = $player['status']; echo "<div>{$currentStatus}</div>\n"; } //Display player info echo "{$player['playerFirst']} {$player['playerLast']}, {$player['year']}<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265403 Share on other sites More sharing options...
Jim R Posted September 4, 2011 Author Share Posted September 4, 2011 @mjdamato, should have used $player instead of $key when outputting the player information. It works really well. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/246410-nested-loops-data-with-group-headers/#findComment-1265405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.