【问题标题】:How to print a name of primary key which is used multiple times in other table in codeigniter如何打印在codeigniter的其他表中多次使用的主键名称
【发布时间】:2019-05-02 18:20:27
【问题描述】:

我有两张桌子

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

我想查看

参赛队伍

Pt id       Team 1     Team2       Team3
1            Kkkk       Llll          Aaaa
2           Jjjj          Gggg        Aaaa
3          Kkkk        Jjjj           Gggg

所以现在如果我想在表格中打印 team1 team2 team3 name 我应该怎么做?

我正在使用 foreach 打印 playteam 表

Foreach($pt as $row)
{
  echo’
<td> ‘.$row->playingteamsid.’ </td>’
<td> ‘.$row->team1.’ </td>’
<td> ‘.$row->team2.’ </td>’
<td> ‘.$row->team3;’ </td>’
}

【问题讨论】:

  • 我认为你需要使用子查询来实现你的输出

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

在模型中

$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
 }
}

请注意,这些表名不是从数据库中获取的。我在控制器中分配这些值。如果你想从模型中获取这些值,你必须使用循环控制器。

【讨论】:

  • 我想从团队表中打印 team1 名称
  • 我不明白
猜你喜欢
  • 2018-01-05
  • 2017-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
相关资源
最近更新 更多