2013-03-19 69 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; 
} 
相关问题