Jump to content

Problem with Foreach Loops...


Jim R

Recommended Posts

I'm trying to create a list of Rosters all on one page from a data table.  I want each roster to show its Team Number ($team) and coaches name ($nameCoach) at the top of the list.  I tried moving the Foreach loop around, but I can't get the results I want.  

 

So it should look like:

A1 - Coach Brett Crist

player name

player name

player name

etc

 

A2 - Coach Paul Goser

player name

player name

etc

 

Here is the link to the page.  Basically, for each player it lists all the coaches.  

 

http://metroindybasketball.com/resources/league2012/2012rosters.php


$coach = array (
'A1' =>'Coach Brett Crist',
'A2' =>'Coach Paul Gosser',
'A3' =>'Coach Brian Tonsani',
'B1' =>'Coach Dustin Harvey',
'B2' =>'Coach Kyle Smith',
'B3' =>'Coach Trevor Andershock',
'C1' =>'Coach Jim Reamer',
'C2' =>'Coach Aaron Butcher',
'C3' =>'Coach Bob Cerbone',
'D1' =>'Coach Larry Baker',
'D2' =>'Coach John Tate',
'D3' =>'Coach Victor Stallworth',
'E1' =>'Coach Mark Bailey',
'E2' =>'Coach Angelo Smith',
'E3' =>'Coach Mike Lawson',
'E4' =>'Coach Jeremy Tiers',
'F1' =>'Coach Aaron Butcher',
'F2' =>'Coach Duke Pryor',
'F3' =>'Coach Damon Hawkins',
'F4' =>'Coach Roy Hairston',
'G1' =>'Coach Kristof Kendrick',
'G2' =>'Coach Michael Tucker',
'G3' =>'Coach Kenton Granger',
'G4' =>'Coach Steve Turner',
'H1' =>'Coach Seve Beach',
'H2' =>'Coach Matt Lacey',
'H3' =>'Coach Derrick Gentry',
'H4' =>'Coach Kayle Funkhouser',
'I1' =>'Coach Courtney James',
'I2' =>'Coach Barry Kroot',
'I3' =>'Coach Chris Hawkins',
'J1' =>'Coach Chris Sibila',
'J2' =>'Coach Larry Baker',
'J3' =>'Coach Dan Schukraft'
);

$query = 'SELECT * FROM fallLeague10 WHERE 2012team IS NOT NULL ORDER BY 2012team,2012number';
$results = mysql_query($query) //or trigger_error('MySQL error: ' . mysql_error())
;


$currentTeam = false; 

while($line = mysql_fetch_assoc($results)) {


	    if($currentTeam != $coach)
        {
		foreach ($coach as $team=>$nameCoach)
                {
            //Status has changed, display status header
            $currentTeam = $line['2012team'];
			echo '<tr><td colspan="6"><hr></td></tr>
			<tr><td colspan="6"><span class="coach">'. $team . ' - ' .$nameCoach. '</td></tr>
			';
			
			
		
		}
         }
	echo '<tr>
                        <td>' . $line['2012number'].'</td>
			<td><b>' . $line['nameFirst'] . ' ' . $line['nameLast'] . '</b></td>
			<td><center>'. $line['feet'] . '\'' . $line['inches'] . '"</center></td>
			<td><center>'. $line['grade'] . '</center></td>		
			<td>'. $line['school'] . '</td>';

               echo '<td><b>'. $line['college'].'</b></td>';

		echo '</tr>';}

	        echo '</tbody></table>';
Edited by Jim R
Link to comment
Share on other sites

 

	    if($currentTeam != $coach)

 

You're comparing a scalar value to an array of values there. You need to be comparing $currentTeam to one of the values from $line to determine whether it has changed or not. Considering you have $currentTeam = $line['2012team'][/code] later on, you probably want to be comparing to [ic]$line['2012team'] in that if statement.

Link to comment
Share on other sites

I'm really not able to wrap my head around what you said.

 

I wasn't able to get the right results before trying the foreach loop so I resorted to the IF loop.  That gave me the follow:

 

A1 - 

player name

player name

player name

etc

 

A2 - 

player name

player name

etc

 

 

That didn't put in the coaches' names.  I'm trying to get help on the foreach loop relative to the array.  The IF statement works without the array.  I went with it instead of the foreach but would like get the desired result.  

Link to comment
Share on other sites

I see, the team is there.  Assuming that the column 2012team matches the keys in the $coach array, then something along these lines should work (not tested).

$currentTeam = '';

while($line = mysql_fetch_assoc($results)) {
	if($line['2012team'] != $currentTeam) {
		$currentTeam = $line['2012team'];		
		echo '<tr><td colspan="6"><hr></td></tr>
		<tr><td colspan="6"><span class="coach">' . $currentTeam . ' - ' . $coach['currentTeam'] . '</td></tr>';
	}
	echo '<tr>
            <td>' . $line['2012number'].'</td>
			<td><b>' . $line['nameFirst'] . ' ' . $line['nameLast'] . '</b></td>
			<td><center>'. $line['feet'] . '\'' . $line['inches'] . '"</center></td>
			<td><center>'. $line['grade'] . '</center></td>		
			<td>'. $line['school'] . '</td>
			<td><b>'. $line['college'].'</b></td>
		</tr>';
}
Edited by AbraCadaver
Link to comment
Share on other sites

Got your DM.  That code brought it back to where it was before I tried the foreach.  It posts the Team Number but no coach's name, then the roster underneath.  

 

I left it there so you could see it.  

Edited by Jim R
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.