php - Codeigniter 连接多个 id 并显示到 View

标签 php mysql codeigniter join model

我遇到了一些困难,希望得到你们的帮助。我正在构建一个幻想游戏,其中有具有以下结构的幻想团队表,其中每个玩家都将使用从玩家表生成的唯一 ID 进行保存。这是我的玩家表

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

这是我的 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

我想要实现的是将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();


            }

这是我的 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);    
   }    

这是我的观点:-

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

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

最佳答案

你能试试这个并看看你会得到什么吗:

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

关于php - Codeigniter 连接多个 id 并显示到 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24485911/

相关文章:

codeigniter - 函数 mcrypt_get_iv_size() 在 PHP 7.1 版的 codeigniter 中被弃用

php - mySQLi_affected_rows 检查不起作用,或者是...?

php mysql用户信息更新

PHP 表单空验证不起作用

php - PDO语法错误

php - Codeigniter Halogy cms 安装设置

php - 将以下 MySQL 查询转换为等效的 Codeigniter

php - XAMPP 的远程共享文件夹?

javascript - AJAX-在发布中获取特定的数组值

php - 保存时如何在Laravel中获取当前模型的id