Zen cart: I would like to query from a specific category its products name, price, image, description, and attributes

天大地大妈咪最大 提交于 2019-12-12 00:36:24

问题


I am using the following query:

<?php
 // retrieve products from database
 global $db;
$q1 = $db->Execute("select * from products");
$q1_items = array();
$q2_items = array();

while (!$q1->EOF){
$q1_items[] = $q1->fields;
$q1->MoveNext();
}
foreach ($q1_items as $item => $items) {
echo '<p><a href="index.php?main_page=product_info&products_id='. $items['products_id'] .'"><img src="images/'. $items['products_image'].'" alt="'. $items['products_name'].'" title="'. $items['products_name'].'" /></a>';
    echo ''.$items['products_price']. ''; ?>

<?php }
?>

The issue I am having is it doesn't not pull the product name and I would like to be able to query each product_description from TABLE_PRODUCTS_DESCRIPTION and Products_attributes.


回答1:


That's because products_name and products_description is in table products_description (or to be more specific in TABLE_PRODUCTS_DESCRIPTION), not in table products (TABLE_PRODUCTS).

To get all basic information (except attributes) You should execute following query:

$q = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON(p.products_id = pd.products_id) WHERE language_id = " . (int)$_SESSION['languages_id']);
$items = array();
while(!$q->EOF) {
    // let's get all attribues for product
    $productInfo = $q->fields;
    $qattr = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS_ATTRIBUTES . " WHERE products_id = " . (int)$q->fields['products_id']);
    $attr = array();
    while(!$qattr->EOF) {
        $attr[] = $qattr->fields;
        $qattr->MoveNext();
    }
    $productInfo['attributes'] = $attr;
    $items[] = $productInfo;
    $q->MoveNext();
}

// now let's output it
foreach($items as $item) {
    echo '<p><a href="index.php?main_page=product_info&products_id='. $item['products_id'] .'"><img src="images/'. $item['products_image'].'" alt="'. $item['products_name'].'" title="'. $items['products_name'].'" /></a>';
    echo $items['products_price'] . '</p>';
}

Note however that this code does NOT get products from specific category - it gets all products, even those that are deactivated. There are several ways to get products from specific category but they vary in performance. Unfortunately there's no best way to do this because it depends on the data. If the products You wish to retrieve belong to category with categories_id of 5 and it's their primary category it's enough to add to first query "WHERE master_categories_id = 5". But if the category is not master category for those products things get bit more complicated because We need to access products_to_categories table which causes performance hit for sites with many products. If You don't know/don't care about performance that much You can change first query to: (assuming that You already know categories_id of Your category):

$q = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON(p.products_id = pd.products_id LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c ON(p.products_id = p2c.products_id AND p2c.categories_id = YOUR_CATEGORY_ID) WHERE pd.products_id IS NOT NULL AND p2c.products_id IS NOT NULL AND language_id = " . (int)$_SESSION['languages_id']);

To get rid of inactive products execute

$q = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON(p.products_id = pd.products_id LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c ON(p.products_id = p2c.products_id AND p2c.categories_id = YOUR_CATEGORY_ID) WHERE products_status = 1 AND pd.products_id IS NOT NULL AND p2c.products_id IS NOT NULL AND language_id = " . (int)$_SESSION['languages_id']);

(Actually check on pd.products_id IS NOT NULL is not needed because We already check pd.language_id.)


EDIT Version without attributes

$q = $db->Execute("SELECT * FROM " . TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_DESCRIPTION . " pd ON(p.products_id = pd.products_id) WHERE language_id = " . (int)$_SESSION['languages_id']);
$items = array();
while(!$q->EOF) {
    $items[] = $q->fields;
    $q->MoveNext();
}

// now let's output it
foreach($items as $item) {
    echo '<p><a href="index.php?main_page=product_info&products_id='. $item['products_id'] .'"><img src="images/'. $item['products_image'].'" alt="'. $item['products_name'].'" title="'. $items['products_name'].'" /></a>';
    echo $items['products_price'] . '</p>';
}


来源:https://stackoverflow.com/questions/17752767/zen-cart-i-would-like-to-query-from-a-specific-category-its-products-name-pric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!