2013-03-19 66 views
0

好的,我知道這應該很簡單,但我會圍繞着圈子。我有兩個表和兩個函數,每個運行查詢,第一個函數獲取產品,第二個獲取產品圖像從兩個表格構建一個數組(從一個表格中獲取一個表格以獲得另一個表格的產品)

我想獲得一個數組,它是產品,它的圖像...

這裏是我的代碼...

/** 
* Gets the requested product from the DB 
* 
* @param string $productUrl 
* @param string $productID 
*/ 
private function _db_get_product($productUrl = null, $productID = null) { 

    if (empty($productUrl) && empty($productID)) 
     return; 

    $db = $this->getConnection(); 
    $q = "SELECT " . $this->_leaf_sql_fields() . 
      " FROM content_products_items pr WHERE pr.productStatus >= " 
      . menuMachine::getMinimumStatus() . " "; 

    if (!empty($productUrl)) 
     $q .= " AND productUrl = '" . $productUrl . "'"; 

    if (!empty($productID)) 
     $q .= " AND productID = '" . $productID . "'"; 

    if ($res = $db->recordsetSingle($q)) 
     $this->_product = $res; 

    return $res; 
} 

/** 
* Get the images for the product 
* @return array 
*/ 
private function _db_get_product_images($productID) { 

    $db = $this->getConnection(); 

    $q = "SELECT * FROM content_products_images WHERE productID = '" . $productID . "'"; 

    $this->_productImages = $db->recordset($q); 

} 

回答

0

你只是在尋找一個查詢到的同一個函數內兩者結合?

//Basic query, improve it according to your needs 

SELECT 
* 
FROM 
content_products_items as p, 
content_products_images as i 
WHERE 
p.productID = $productId AND 
i.productID = p.productID; 

或者爲了調用這兩個函數並將結果組合到一個數組中?

$myProduct = array(
    'productData' => $this->_db_get_product($productUrl, $productID), 
    'productImages' => $this->_db_get_product_images($productID), 
); 

兩者都應該引導你進入工作方向。

0

我第一次嘗試在StackOverflow上回答這裏的人,所以請耐心等待......但我認爲下面是你要找的東西?

$product = array('product' => _db_get_product($URL, $ID), 'images' => _db_get_product_images($ID)); 

或者,如果你想要的一切一氣呵成,不需要爲別的兩種不同的方法,可以按如下方式重寫_db_get_product方法:

private function _db_get_product($productUrl = null, $productID = null) { 

    if (empty($productUrl) && empty($productID)) 
     return; 

    $output = array(); 
    $db = $this->getConnection(); 
    $q = "SELECT " . $this->_leaf_sql_fields() . 
     " FROM content_products_items pr WHERE pr.productStatus >= " 
     . menuMachine::getMinimumStatus() . " "; 

    if (!empty($productUrl)) 
     $q .= " AND productUrl = '" . $productUrl . "'"; 

    if (!empty($productID)) 
     $q .= " AND productID = '" . $productID . "'"; 

    if ($res = $db->recordsetSingle($q)) 
     array_push($output, $res); 

    $q2 = "SELECT * FROM content_products_images WHERE productID = '" . $productID . "'"; 
    array_push($output, $db->recordset($q2)); 

    return $output; 
} 
相關問題