2

いくつかの製品といくつかの画像があります。さらに多くの画像があり、それらには 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();

これにより、最初の画像と残りの情報のみが得られます。それを result_array() に変更すると、2番目のものも別の配列になります。(意味をなさない製品からの他の結果とともに)。

4

1 に答える 1

5

上で述べたように、もう一度データベースにアクセスしてその製品の画像配列を取得し、それらの結果を元のクエリ配列に追加し直すことができます。

$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;
于 2012-11-20T10:27:50.117 に答える