简体   繁体   中英

OpenCart, display the total number of products

I'm using OpenCart ecommerce, and I would like to see in the index of ecommerce the total number of products

I do not have (for now) access to the db opencart, so to understand what is the structure of the db I have referred to this image

在此处输入图片说明

And this is an example of a query that I'm trying to use to display the total number of products (the result is obviously not what I expect)

//Test Count Product
//$query_test = $db->query("SELECT " . DB_PREFIX . "product_description.name FROM " . DB_PREFIX . "product INNER JOIN " . DB_PREFIX . "product_description ON " . DB_PREFIX . "product.product_id = " . DB_PREFIX . "product_description.product_id");
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test as $row) {
    $count_test++;
}   
echo $count_test;

Try this:

$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product");
echo $query->row['total'];

Nicolo,

Your code is also correct. For that you need to use this code

$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");

$count_test = 0;

foreach ($query_test->rows as $row) {

$count_test++;

}

echo $count_test;

But, I will recommend Monkeyman's way to get product_totals but with some modification (Monkeyman's code will NOT work for multiple stores)

$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product_to_store WHERE store_id = '" . (int)$this->config->get('config_store_id') . "'");

echo $query->row['total'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM