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);

}
4

2 回答 2

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),
);

两者都应该引导您进入工作方向。

于 2013-03-19T11:17:47.727 回答
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;
}
于 2013-03-19T11:21:46.937 回答