Jump to content

Nested loops - data with group headers...


Jim R

Recommended Posts

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.

 

 

 

 

Link to comment
Share on other sites

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";	
    }

Link to comment
Share on other sites

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);

 

Link to comment
Share on other sites

@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.

Link to comment
Share on other sites

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";
    }

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.