use*_*771 2 database codeigniter relationship
我有一些产品和图片。图片更多,并且具有p_id。如何获得多个?这是一个画廊。我当前的查询:
$this->db->select('prod_id, prod_name, prod_link, prod_description, prod_status, prod_price, brand_link, link, cat_link, normal, thumbnail');
$this->db->from('product');
$this->db->join('category', 'cat_id = prod_category');
$this->db->join('brands', 'brand_id = prod_brand');
$this->db->join('images', 'p_id = prod_id');
$query = $this->db->get();
return $query->row_array();
Run Code Online (Sandbox Code Playgroud)
这只给了我第一张图片以及其他信息。如果我将其更改为result_array(),它也会在第二个数组中给我第二个。(以及产品的其他结果,这没有任何意义)。
如上所述,您可以再次访问数据库以获取该产品的图像数组,然后将这些结果重新添加到原始查询数组中。
$this->db->select('prod_id, prod_name, prod_link, prod_description, prod_status, prod_price, brand_link, link, cat_link, normal');
$this->db->join('category', 'cat_id = prod_category');
$this->db->join('brands', 'brand_id = prod_brand');
$query = $this->db->get('product')->result_array();
// Loop through the products array
foreach($query as $i=>$product) {
// Get an array of products images
// Assuming 'p_id' is the foreign_key in the images table
$this->db->where('p_id', $product['prod_id']);
$images_query = $this->db->get('images')->result_array();
// Add the images array to the array entry for this product
$query[$i]['images'] = images_query;
}
return $query;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6776 次 |
| 最近记录: |