I am working on someone else program. I have 2 tables Products and Seller. These tables are connected and the connection is made in the Seller table. I want to display the Product name from the Products table and also display the Seller who has made the product entry.
This is how I display the product table:
public function getProductName($product_id,$language_id) {
$query = $this->db->query("SELECT name FROM " . DB_PREFIX . "product_description WHERE
product_id = '".(int)$product_id."'
AND language_id = '".(int)$language_id."'");
if($query->row AND $query->row['name'] != '') {
return $query->row['name'];
} else {
return '-';
}
}
The Seller table has fields seller_name, seller_id, sproduct_id. How can I pull the associated seller name for each product?
Using your code:
public function getProductName($product_id, $language_id)
{
$strSQL = "SELECT p.name, s.seller_name FROM " . DB_PREFIX .
"product_description p INNER JOIN " . DB_PREFIX .
"Seller s ON p.product_id = s.sproduct_id WHERE product_id = '" .
intval($product_id) . "' AND language_id = '" .
intval($language_id) . "'";
$query = $this->db->query($strSQL);
if($query->row AND $query->row['name'] != '') {
return $query->row['name'];
} else {
return '-';
}
}
If product_id and language_id are integer values you don't need to enclosed them in ''. Also remember to add error checking to make sure that $product_id and $language_id have valid values for your query. More info on SQL joins.
You'll want to join the two tables together on the common product_id key. Here's the resultant Sql you'll want. Also, if product_id and language_id are integers in the database (as they seem to be in PHP) that you can drop the '':
"SELECT pd.name, s.seller_name
FROM " . DB_PREFIX . ".product_description pd
INNER JOIN " . DB_PREFIX . ".Seller s on pd.product_id = s.sproduct_id
WHERE
product_id = ".(int)$product_id."
AND language_id = ".(int)$language_id.";";
Related
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'];
Hy all,
I'm working on an multi store website.
The main store is this. The second store ( same db, multi store ) is this with uid 4
I've got for the second store an categorie with multiple childs, with grand childs and so on...
My question is very simple, how can i make the categorie ( with uid 355 ) the parent categorie for that store instead of uid 0
I'll use VQMOD later, but i'm now inserting code directly in the files, for fast programming / debugging
The file that i'm changing is in: [root]/catalog/module/category.php
I've changed this code:
public function getCategories($parent_id = 0) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
To this:
public function getCategories($parent_id = 0) {
if( $this->config->get('config_store_id') == 4 && $parent_id == 0 ){
$parent_id = 355;
echo "test1";
}
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category c LEFT JOIN " . DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id) LEFT JOIN " . DB_PREFIX . "category_to_store c2s ON (c.category_id = c2s.category_id) WHERE c.parent_id = '" . (int)$parent_id . "' AND cd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND c2s.store_id = '" . (int)$this->config->get('config_store_id') . "' AND c.status = '1' ORDER BY c.sort_order, LCASE(cd.name)");
return $query->rows;
}
And that kinda works. At the home page, the wrong categorie menu list is gone ( there's no menu anymore ), and at an categorie list page like this one there are the right categorie's on the left.
If i delete the && $parent_id == 0 part, it goes wrong at both the page's.
Does anybody got an clue of how to fix it? Any help would be apriciated. If you need more info, i will give it to you.
-ps- sorry for my bad english
Alright, after a lot of try's i searched the fora's of OpenCart better and came upon this thread:
OpenCart Fora Thread
The anser there is as follows:
There's a "top" flag you set to make it show up as a top level menu
So what i did is this:
I've edited the new main category ( with uid 355 ) and flagged it as top, that didn't helped, but i left it on.
Then i've edited all the direct childs of the main category and flagged them as top and foila, there they are. the problem was solved.
So with my edit to the main core, and the flagging of the categorie's, the problem is now solved
$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)){
foreach($r as $item_id2)
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$item_id2. '"';}
This is for a product "wish list." Each is relative to a user. I can add to the wish list, but I can't delete the proper item. What this code is doing is deleting the last item on the list, or perhaps the item with the highest 'image_id'.
Either way, I'm not getting the relative 'image id' pertaining to the the associated item.
I'm pretty sure this should be enough information to solve the problem. I'm most certain my problem lies in the MySQL query:
mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
first of all as mentioned before your queries are NOT secure, please use PDO. anyways try this :
$something_else = mysql_query('SELECT image_id FROM items p LEFT JOIN list up ON p.item_id = up.item_id WHERE up.UserID = "' . $user_id . '"');
while ($r=mysql_fetch_assoc($something_else)) {
$query ='DELETE FROM list WHERE UserID="' .$user_id. '" AND item_id="' .$r['image_id'] . '"';
}
I am really struggling on how to achieve this and hope that someone can help me. I have the existing function and query:
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id)
}
This works, but it sets all products to Zero when I actually want it to only set products to Zero when that product exists in another table.
i.e., in explanatory terms:
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id AND product_id exists in CustomerProducts)
}
I am not au fait with joins, but I am not sure if I even need to use a join here as the query seems more simple than that.
Can anyone point me in the right direction?
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . 'product SET quantity = 0, price = 0.00 WHERE product_id = ' . (int)$product_id ." AND product_id =(select DISTINCT(product_id) from CustomerProducts where product_id= $product_id)" )
}
This may work.
use this will work for you are not assigning db.product and make sure you write query in a string then execute.
And you see you query, by removing comments
public function setStockAndPrice($product_id) {
$query_string = "UPDATE " . DB_PREFIX . ".product SET quantity = '0', price = '0.00' WHERE product_id = '$product_id'";
// echo "Query : " . $query_string;
$query = $this->db->query($query_string);
}
public function setStockAndPrice($product_id) {
$query = $this->db->query('UPDATE ' . DB_PREFIX . '.product p, ' . DB_PREFIX . '.CustomerProducts cp SET p.quantity = 0, p.price = 0.00 WHERE p.product_id = ' . (int)$product_id . ' AND p.product_id = cp.product_id');
}
I am trying to make dynamic product filtering for opencart in which user could add filters, assign them to categories and assign filter values for products.
Currently I Have these mysql tables:
Filters - ID NAME
Filters_categories - ID FILTER_ID CATEGORY_ID
Filters_values - ID FILTER_ID VALUE
Filters_products - ID FILTER_ID VALUE_ID PRODUCT_ID
The problem is with this structure that i can't get products when more than one filter is activated because i get MySQL query which looks something like this:
SELECT product_id FROM Filters_products WHERE ((name_id='1' OR name_id='2') AND filter_id='1') AND ((name_id='3' OR name_id='4') AND filter_id='2')
And it doesn't make any sense. I can't figure out how should i change my database structure to make dynamic filtering possible. How can I solve this situation?
Thank you.
SELECT product_id FROM Filters_products WHERE (screen_id='1' OR screen_id='2') and (connection_id='1')
Use this awesome extension,
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=10098
Hope this helps.
in file /catalog/model/catalog/product.php find line nr. 91 and replace "if {}" block whit this code...
if (!empty($data['filter_category_id'])) {
if (!empty($data['filter_sub_category'])) {
$sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
}
if (!empty($data['filter_filter'])) {
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id) {
$filterSQL = $this->db->query("SELECT * FROM " . DB_PREFIX . "filter WHERE filter_id = ". $filter_id);
$implode[$filterSQL->row['filter_group_id']][] = (int)$filter_id;
}
foreach($implode AS $implode2) {
$sql .= " AND (";
foreach($implode2 AS $filterID) {
$sql .= "(SELECT count(*) FROM product_filter WHERE product_filter.product_id=p.product_id AND product_filter.filter_id=$filterID) OR ";
}
$sql = substr($sql, 0, -4);
$sql .= ")";
}
}
}