duandao1931 2019-05-02 18:20
浏览 46

如何打印在codeigniter中的其他表中多次使用的主键名称

I have two tables

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

And i want view as

Playingteams

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

so now if i want to print team1 team2 team3 name in a table what should i do?

I am using foreach to print playingteam table

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

1条回答 默认 最新

  • douzhan1963 2019-05-02 18:53
    关注

    In model

    $query = $this->db->select('team1','team2','team3')->from('playingteams')->limit(1)->get();
    $array = $query->result();
    return $array;
    

    In controller

    my_team = array();
    my_team = array(
                  'team1' => $array[0]->team1,
                   'team2' => $array[0]->team2,
                   'team3' => $array[0]->team3,
                   );
    

    You can print these values in view using json_encode(). To print table names take key from the array.

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

    Please note that thses table names are not taken from database. I assign these values in controller. if you want take theses vales from model you have to use for loop controller.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题