I have the following query:
SELECT * FROM teams
LEFT JOIN participants
ON teams.teamNo = participants.teamNo
ORDER BY teams.teamNo ASC
The query obviously gets all the teams in the team table and where there is a matching participant in the participant table that comes back. Not every team may have assigned participants but every team must be displayed.
I want to diplay the data on the same page like so (notice team 2 has no current participants but is still displayed:
Team 1:
- Participant 1
- Participant 2
- Participant 3
Team 2:
Team 3:
- Participant 1
- Participant 2
I have the following array coming back from the SQL query currently:
Array
(
[0] => Array
(
[TeamNo] => 1
[ParticipantFirstNames] => Katie
[ParticipantSurname] => Bloggs
)
[1] => Array
(
[TeamNo] => 1
[ParticipantFirstNames] => Jenny
[ParticipantSurname] => Ruffles
)
[2] => Array
(
[TeamNo] => 1
[ParticipantFirstNames] => Hannah
[ParticipantSurname] => Cox
)
[3] => Array
(
[TeamNo] => 2
[ParticipantFirstNames] =>
[ParticipantSurname] =>
)
[4] => Array
(
[TeamNo] => 3
[ParticipantFirstNames] => Alex
[ParticipantSurname] => Glover
)
[5] => Array
(
[TeamNo] => 3
[ParticipantFirstNames] => Karl
[ParticipantSurname] => Lawrence
)
I believe I need to convert it to an array a bit like the following, but im not sure how to do this in PHP:
array(
array( 'TeamNo' => '1',
'TeamParticipants' => array(
array( 'ParticipantFirstName' => 'Harry',
'ParticipantSurname' => 'Bloggs'),
array( 'ParticipantFirstName' => 'James',
'ParticipantSurname' => 'Car'))
)
array( 'TeamNo' => '2',
'TeamParticipants' => array() )
array( 'TeamNo' => '3',
'TeamParticipants' => array(
array( 'ParticipantFirstName' => 'Harry',
'ParticipantSurname' => 'Bloggs'),
array( 'ParticipantFirstName' => 'James',
'ParticipantSurname' => 'Car')
)
)
)
I just cant get my head around arrays can someone help, or do I need a different query in the first place?? Im using PHP.