我遇到了一些困难,希望得到你们的帮助。我正在构建一个幻想游戏,其中有具有以下结构的幻想团队表,其中每个玩家都将使用从玩家表生成的唯一 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/