Jim R Posted June 19, 2013 Share Posted June 19, 2013 (edited) 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 June 19, 2013 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/ Share on other sites More sharing options...
kicken Posted June 19, 2013 Share Posted June 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436897 Share on other sites More sharing options...
Jim R Posted June 19, 2013 Author Share Posted June 19, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436914 Share on other sites More sharing options...
AbraCadaver Posted June 19, 2013 Share Posted June 19, 2013 Why the coach array? Do you not have the team and coach in the database? If not, there isn't anyway to match them up anyway! Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436921 Share on other sites More sharing options...
AbraCadaver Posted June 19, 2013 Share Posted June 19, 2013 (edited) 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 June 19, 2013 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436926 Share on other sites More sharing options...
Jim R Posted June 19, 2013 Author Share Posted June 19, 2013 No, I don't have the coaches' names in the database. I figured I could make it work via an array. Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436927 Share on other sites More sharing options...
Jim R Posted June 20, 2013 Author Share Posted June 20, 2013 (edited) 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 June 20, 2013 by Jim R Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436956 Share on other sites More sharing options...
mac_gyver Posted June 20, 2013 Share Posted June 20, 2013 to index into the $coach array, you have to use the $currentTeam as the index - $coach[$currentTeam] Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436957 Share on other sites More sharing options...
Jim R Posted June 20, 2013 Author Share Posted June 20, 2013 Oooo...very nice!! That worked, and it even kind of makes sense to me. :-) Thank you to you three who answered! Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1436970 Share on other sites More sharing options...
AbraCadaver Posted June 20, 2013 Share Posted June 20, 2013 to index into the $coach array, you have to use the $currentTeam as the index - $coach[$currentTeam] Doh! Good catch. Quote Link to comment https://forums.phpfreaks.com/topic/279360-problem-with-foreach-loops/#findComment-1437048 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.