[英]Codeigniter display records from table 2 for each record with id similar to table 1
I've been bugged by this problem in CodeIgniter.我一直被 CodeIgniter 中的这个问题困扰。 I want to display all records in table 2 for each record in table 1. like this illustration:
我想为表 1 中的每条记录显示表 2 中的所有记录。如下图所示:
id | name | id | pets |
1 | john doe | 1 | dogs |
2 | jane doe | 1 | cats |
2 | hamsters |
2 | birds |
2 | iguanas |
and display like this in views:并在视图中显示如下:
id: 1
name: John doe
pets: dogs, cats
id: 2
name: jane doe
pets: hamsters, birds, iguanas
this is an analogy.这是一个类比。 any way to do it?
有什么办法吗?
Since your problem is not clear because you are not given any code.由于您的问题不清楚,因为您没有得到任何代码。 I have give only idea to join the tables.
我只给出加入表格的想法。 You can write query like this in codeigniter.
您可以在 codeigniter 中编写这样的查询。
$data = $this->db->query("SELECT a1.id,al.name,a2.pets FROM TableA a1
INNER JOIN TableB a2
ON a1.id = a2.id WHERE a1.id=1")->result(); // here you got objects
print_r($data); //This will show the result
If you give your code then we will surely solve your problem.如果您提供代码,那么我们一定会解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.