简体   繁体   English

如何从数据库中获取数据?

[英]how to fetch data from a database?

let's say that I have a database of coaches and players , there is coaches table1 like this :假设我有一个教练和球员的数据库,有这样的教练表:

id ID
name姓名
icon图标

and the players table2 is like this :玩家 table2 是这样的:

id ID
name姓名
age年龄
id_coach 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 ?如何获得每个教练的所有球员,包括球员的姓名和年龄(表 2)以及表 1 中每个教练的姓名和图标?

I have this sql request but its displaying only names and names of players我有这个 sql 请求,但它只显示玩家的名字和名字

       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>';
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM