我正在使用基於mcv設計模式的OpenCart商店。我需要一個模型函數返回一個數組,但我需要在該數組內調用另一個返回數組的函數。下面這個函數打破了這裏:PHP - 將數組返回給另一個數組的組合函數
'gallery_images' => $this->getGalleryImages($data['product_id'])
public function getImportProductInfo() {
$query = $this->db->query("SELECT `product_id` FROM " . DB_PREFIX . "product WHERE `status` = '0' AND `affiliate_id` = '" . $this->affiliate->getID() . "'");
$pids = array();
foreach($query->rows as $result) {
$pids[] = $result['product_id'];
}
// product & product description
$query_product = $this->db->query("SELECT p.model,
p.product_id,
p.quantity,
p.image,
p.price,
p.weight,
p.length,
p.width,
p.height,
pd.name AS 'product_name',
pd.description AS 'product_description',
cd.name AS `category_name`,
m.name AS 'manufacturer_name'
FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_description pd ON p.product_id = pd.product_id
LEFT JOIN " . DB_PREFIX . "product_to_category ptc ON p.product_id = ptc.product_id
LEFT JOIN " . DB_PREFIX . "category_description cd ON ptc.category_id = cd.category_id
LEFT JOIN " . DB_PREFIX . "manufacturer m ON p.manufacturer_id = m.manufacturer_id
WHERE p.product_id IN (" . $this->db->escape(implode(',',$pids)) . ")");
foreach($query_product->rows as $data) {
$product_data[] = array(
'product_id' => $data['product_id'],
'model' => $data['model'],
'quantity' => $data['quantity'],
'featured_image' => $data['image'],
'price' => $data['price'],
'weight' => $data['weight'],
'length' => $data['length'],
'width' => $data['width'],
'height' => $data['height'],
'product_name' => $data['product_name'],
'description' => $data['product_description'],
'category_name' => $data['category_name'],
'manufacturer_name' => $data['manufacturer_name'],
'gallery_images' => array($this->getGalleryImages($data['product_id']))
);
}
return $product_data;
}
這裏是getGalleryImages()函數。注意這裏我也試過return implode(', ',$images);
,所以我沒有建立數組,但它仍然打破。
public function getProductGalleryImages($product_id) {
$query = $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$product_id . "'");
$images = array();
foreach($query->rows as $result) {
$images[] = $result['image'];
}
//return implode(', ',$images);
return $images;
}
每個產品都有存儲在交叉表中的多個圖像的URL,我需要添加到陣列,它一個上午都在戰鬥我...任何想法將不勝感激。在此先感謝:)這裏
OK是嵌入在陣列中的SQL工作
'gallery_images' => $this->db->query("SELECT `image` FROM " . DB_PREFIX . "product_image WHERE `product_id` = '" . (int)$data['product_id'] . "'")
但它返回這樣的:
[gallery_images] => stdClass Object (
[row] => Array ([image] => motorcycle/12_06_27/031.JPG)
[rows] => Array ([0] => Array ([image] => motorcycle/12_06_27/031.JPG))[num_rows] => 1)))
所有我想要的是['image']
一部分。
@dkelliner所以,你的答案是正確的我的問題,但我已經嘗試了所有這些情況下,他們仍然打破這就是爲什麼我感到困惑。如果我直接在數組中嵌入sql,它會工作,但它返回一堆我不需要的東西。我已經添加到上面的代碼來顯示我的結果。 – handmdmr
告訴我它不是「getProductGalleryImages」vs「getGalleryImages」:) – dkellner
(另外,你正在查詢裏面的查詢對你的速度來說不是很健康,還有其他方法,但這是另外一個問題。) – dkellner