WhiteLine
WhiteLine

Reputation: 1991

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

enter image description here

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;

Upvotes: 0

Views: 2405

Answers (2)

Munjal Mayank
Munjal Mayank

Reputation: 131

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'];

Upvotes: 0

The Krotek
The Krotek

Reputation: 393

Try this:

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

Upvotes: 1

Related Questions