简体   繁体   English

Codeigniter 显示表 2 中每条 id 与表 1 相似的记录的记录

[英]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.

相关问题 php:对于一个mysql表中的每个记录,显示第二个表中的所有记录 - php: for each record in one mysql table, display all the records from a 2nd table 如何显示带有剩余记录的表中的最后一条记录? - How to display the last record from the table with remaining records? Codeigniter MVC /显示表中的所有记录 - Codeigniter MVC / Display all records in table 根据CodeIgniter中数据透视表中的类别ID显示帖子 - Display posts according to category ID from pivot table in CodeIgniter Codeigniter如何连接3表并从用户ID显示 - codeigniter how to join 3 table and display from user id 我想连接数据并显示一个表中的所有记录和另一个表中的一个记录 - I want to join data and display all records from one table and just one record from another table 如何从一个表中获取现有记录的 ID 并将其与其他记录一起插入到另一个表中? - How to grab an ID of an existing record from one table and insert it along with other records into another table? Codeigniter从其中page_id和user_id的表中删除记录 - Codeigniter delete record from table where page_id and user_id 提取表中与Codeigniter Active Record值相同的记录数 - Pull count of records in a table that have the same value with Codeigniter Active Record 整齐地显示与表中的ID相关的记录 - Display records associated with an ID in a table neatly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM