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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀