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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog