let's say that I have a database of coaches and players , there is coaches table1 like this :
id
name
icon
and the players table2 is like this :
id
name
age
id_coach
how to get all players of every coach , with name and ages of players(table2) and name and icons of every coach from table1 ?
I have this sql request but its displaying only names and names of players
SELECT a.name AS coachName, a.icon , b.name , b.age
FROM table1 a JOIN
table2 b
ON b.id_coach = a.id order by a.id;
$output = array();
$output2 = '';
foreach($this->CoachPlayers as $key => $value){
$coachName = $value['coachName'];
$coachImage = $value['icon'];
$age = $value['age'];
if (!array_key_exists($coachName, $output)) {
$output[$coachName] = array();
}
$output[$coachName][] = $value['name'];
$output[$coachName]['image'] = $value['icon'];
}
foreach($output as $data => $values) {
echo "<h1>".$data."</h1>";
echo '<div style="text-align:center;"><img src="'.URL."public/images/coaches/".$coachImage.'" /></div>';
foreach ($values as $key => $value) {
'<p>'.$value.' </p>';
'<p>'.$age.' </p>';
}
Try with below code, it should work.
$output = array();
$output2 = '';
foreach($this->CoachPlayers as $key => $value){
$coachName = $value['coachName'];
$coachImage = $value['icon'];
if (!array_key_exists($coachName, $output)) {
$output[$coachName] = array();
}
$player = array();
$player['name'] = $value['name'];
$player['age'] = $value['age'];
$output[$coachName][] = $player;
$output[$coachName]['image'] = $value['icon'];
}
foreach($output as $data => $values) {
$coachImage = $values['image'];
echo "<h1>".$data."</h1>";
echo '<div style="text-align:center;"><img src="'.URL."public/images/coaches/".$coachImage.'"/></div>';
if(is_array($values)) {
foreach ($values as $key => $player) {
echo '<p>'.$player['name'].'</p>';
echo '<p>'.$player['age'].'</p>';
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.