member123 Posted May 12, 2008 Share Posted May 12, 2008 I currently have a MySQL table that is set up like this: team_id team player 1 Expos David 1 Expos James 1 Expos Jeremy 2 Pirates Alex 2 Pirates Steve To pull this table into an array, I have this code: $result = bb_db_query("SELECT * FROM bb_players"); if ($myrow=mysql_fetch_array($result)) { do { $players[] = $myrow["player"]; }while ($myrow=mysql_fetch_array($result)); } where bb_db_query is basically a function that queries the database and bb_players is the name of the table. However, I want to turn the array into a multidimensional array that makes use of the team IDs and teams as well. How should I go about modifying my code to make a multidimensional array that includes the team IDs and team names linked together, and has sub-arrays consisting of the player names for each teams? I think I can figure out everything except how to include both the IDs and the team names. Thanks! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 12, 2008 Share Posted May 12, 2008 $result = bb_db_query("SELECT * FROM bb_players"); while ($myrow=mysql_fetch_array($result)) { $team[myrow['team_id']]['name'] = $myrow['team']; $team[myrow['team_id']]['players'][] = $myrow['player']; } Quote Link to comment Share on other sites More sharing options...
member123 Posted May 12, 2008 Author Share Posted May 12, 2008 Thanks a bunch! Quote Link to comment Share on other sites More sharing options...
member123 Posted May 12, 2008 Author Share Posted May 12, 2008 I'm getting this error: Parse error: syntax error, unexpected '[', expecting ']' in /home/ticker/public_html/index.php on line 12 Line 12 is $team[myrow['team_id']]['name'] = $myrow['team']; I declare the array with this: $team = array(); How should I adjust that to declare it as a multidimensional array? Quote Link to comment Share on other sites More sharing options...
corbin Posted May 12, 2008 Share Posted May 12, 2008 Change the two "myrow['team_id']]" to "$myrow['team_id']]". Quote Link to comment Share on other sites More sharing options...
member123 Posted May 12, 2008 Author Share Posted May 12, 2008 Ah yes, thank you! Quote Link to comment Share on other sites More sharing options...
member123 Posted May 12, 2008 Author Share Posted May 12, 2008 With the one-dimension array, I had a foreach statement to display the contents of the array. With the multidimensional array, I tried using two foreach statements, but it doesn't seem to be working correctly. How should I set things up differently now? Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 Something like <?php $teams = array ( 1 => array ( 'name' => 'Expos', 'players' => array ('David','James','Jeremy') ) , 2 => array ( 'name' => 'Pirates', 'players' => array ('Alex','Steve') ) ); foreach ($teams as $id => $teamdata) { echo "$id : " . $teamdata['name'] . '<br/>'; foreach ($teamdata['players'] as $player) { echo $player . '<br/>'; } echo '<br/>'; } ?> Quote Link to comment Share on other sites More sharing options...
member123 Posted May 12, 2008 Author Share Posted May 12, 2008 Thanks Barand, that code works great when the array is hard coded like that. However, with this code: $result = bb_db_query("SELECT * FROM bb_players"); while ($myrow=mysql_fetch_array($result)) { $team[$myrow['team_id']]['name'] = $myrow['team']; $team[$myrow['team_id']]['players'][] = $myrow['player']; } used to fill the array, it doesn't seem to be working correctly. Do you see something that I should change with this code? Thanks. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 12, 2008 Share Posted May 12, 2008 They should be identical. I ran your code then mine --> [pre]1 : Expos David James Jeremy 2 : Pirates Alex Steve Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 12, 2008 Share Posted May 12, 2008 It could be because the code I provided used the variable $team whereas Barand's code uses the variable $teams. Make sure the variables are named the same. Quote Link to comment Share on other sites More sharing options...
member123 Posted May 13, 2008 Author Share Posted May 13, 2008 It's not working, thanks everyone! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 13, 2008 Share Posted May 13, 2008 It's not working, thanks everyone! I'm sorry, did you mean to say "It's now working"? If not, then what is happening? Show us the code you currently have. Quote Link to comment 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.