[英]How to print a name of primary key which is used multiple times in other table in codeigniter
我有兩張桌子
Team
Teamid
Teamname
Playingteams
Playingteamsid
Team1 fk(teamid)
Team2 fk(teamid)
Team3 fk(teamid)
Team table
Teamid teamname
1 kkkk
2 jjjj
3 llll
4 gggg
5 aaaa
Playingteam table
Ptid team1 team 2 team 3
1 1 3 5
2 2 4 5
3 1 2 4
我希望視圖為
Playingteams
Pt id Team 1 Team2 Team3
1 Kkkk Llll Aaaa
2 Jjjj Gggg Aaaa
3 Kkkk Jjjj Gggg
所以如果我想在一張表中打印team1 team2 team3名稱我該怎么辦?
我正在使用foreach打印playteam表
Foreach($pt as $row)
{
echo’
<td> ‘.$row->playingteamsid.’ </td>’
<td> ‘.$row->team1.’ </td>’
<td> ‘.$row->team2.’ </td>’
<td> ‘.$row->team3;’ </td>’
}
在模型中
$query = $this->db->select('team1','team2','team3')->from('playingteams')->limit(1)->get();
$array = $query->result();
return $array;
在控制器中
my_team = array();
my_team = array(
'team1' => $array[0]->team1,
'team2' => $array[0]->team2,
'team3' => $array[0]->team3,
);
您可以使用json_encode()在視圖中打印這些值。 要打印表名,請從數組中獲取密鑰。
my_team = <?php echo json_encode($my_team); ?>
for(let x in my_team) {
for(my_team.hasOwnProperty(x) {
console.log(x); // here we can take team names
}
}
請注意,這些表名不是從數據庫中獲取的。 我在控制器中分配這些值。 如果你想從你必須用於循環控制器的模型中取出這些vales。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.