[英]I want to join data and display all records from one table and just one record from another table
I am a Codeigniter developer.我是一名 Codeigniter 开发人员。 I am working on an e-commerce site.
我在一个电子商务网站上工作。 I face one issue in the admin panel.
我在管理面板中遇到一个问题。 I have two tables table_1= tbl_category table_2= tbl_products
我有两个表 table_1= tbl_category table_2= tbl_products
I want to update a product.我想更新一个产品。 I fetch all data of product from tbl_products using this:
我使用这个从 tbl_products 获取产品的所有数据:
$this->db->select('*')
->from('tbl_products')
->where('cat_id,$id)
->get();
It is working perfect but I want to display a dropdown of all categories in the update product page.它运行良好,但我想在更新产品页面中显示所有类别的下拉列表。 The problem is I can't find a solution how I can do it.
问题是我找不到解决办法,我该怎么做。 I have to fetch one product by its id from tbl product but how to fetch all categories.
我必须通过 tbl 产品的 id 获取一个产品,但如何获取所有类别。 I want a solution in Codeigniter.
我想要一个 Codeigniter 的解决方案。 I will wait for your answers Please help me, friends.
我会等待你的答案 请帮助我,朋友。
you better remove the where clause or remove cat_id from where clause like:您最好删除 where 子句或从 where 子句中删除 cat_id ,例如:
$this->db->select('*')
->from('tbl_products')
->where('cat_id,$id) //remove where clause or remove cat_id
->get();
so that you will get all the categories selected以便您选择所有类别
if u want to display data from other table having a foreign key then use left join on the keys如果你想显示来自具有外键的其他表的数据,则在键上使用左连接
$this->db->select('*')
->from('tbl_products')
->join('tbl_category','tbl_category.id = tbl_products.id')
->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.