duanqian9593 2014-06-30 08:29
浏览 25
已采纳

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

I'm stuck on something and would appreciate some help from you guys. I am building a fantasy game where I have fantasy team table with the below structure where each player will be saved with a unique ID generated from players table. Here is my player table

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

and here is my fantasyteam table

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

what I want to achieve is joining fantansyteam table with player table in which the keys will be the ids of players in fantasy team table. Here is my model :-

            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();


            }

Here is my controller :-

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

And here is my view :-

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

Can someone help me how to display user team with the playerName and other fields in my view?

  • 写回答

1条回答 默认 最新

  • douan2907 2014-06-30 09:44
    关注

    Can you try this and see what you get:

    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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题