Codeigniter连接多个ID并显示到视图

时间:2014-06-30 08:29:51

标签: php mysql codeigniter join model

我坚持做某些事情,并感谢你们的帮助。我正在构建一个幻想游戏,我有一个幻想团队表,下面的结构,每个玩家将保存与玩家表生成的唯一ID。这是我的播放器表

playerID  | playerName       | teamID  | value | point
13          peter Cech            2         8       0
15          Fernando Torres       2         9       0

这是我的fantasyteam表

teamID |  fantasyteam     | userID | GK1 | GK2 | DEF1 | DEF2 | MID1 | MID2 | FWD1 | FWD2
95        Washindi FC          1      13     2     3       6      7     12      15    18

我想要实现的是加入fantansyteam表和玩家表,其中键将是幻想团队表中玩家的ID。这是我的模特: -

            function get_fantansy_team($userID){
            $where=array(
            'userID'=>$userID,

            );
            $this->db->select();
            $this->db->from('fantansyteams AS FT');
            $this->db->join('player AS P1', 'FT.GK2= P1.playerID');
            $this->db->join('player AS P2', 'FT.GK1= P2.playerID','left outer');
            $this->db->where('FT.userID', $userID);
            $query = $this->db->get();
            return $query->result_array();


            }

这是我的控制器: -

public function user($userID)
   {
     $this->load->model('team_model');
     $data['myteam']=$this->team_model->get_fantansy_team($userID);
         $this->load->view('myteam_view',$data);    
   }    

以下是我的观点: -

     <?php  echo   "<pre>" ;print_r($myteam);echo "</pre>" ;?>
    <?php foreach($myteam as $player):
        echo $player['GK1'] ;
        echo $player['playerName'] ;
    endforeach;?>

有人可以帮助我如何在我的视图中显示playerName和其他字段的用户团队吗?

1 个答案:

答案 0 :(得分:1)

你可以尝试一下,看看你得到了什么:

function get_fantansy_team($userID){
    $where=array(
        'userID'=>$userID,
    );
    $this->db->select('P1.playerName, P2.playerName, FT.fantasyteam');
    $this->db->from('fantasyteam AS FT'); //corrected table name
    $this->db->join('player AS P1', 'FT.GK2= P1.playerID');
    $this->db->join('player AS P2', 'FT.GK1= P2.playerID','left outer');
    $this->db->where('FT.userID', $userID);
    $query = $this->db->get();
    return $query->result_array();
}