Opencart to enable autocomplete for name AND model - php

I am using OC 2.2.0. On frontend, I need autocomplete both on name and model. Right now, only name autocompletes. In catalog/model/catalog/product.php I changed
$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
to
$sql .= " OR LCASE(p.model) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
But still can't get autocomplete on model. Instead, I always must enter the whole model name, but it must be autocomplete. Any suggestions?

After many days of researching and looking at the code, I managed to understand and fix my problem. I was looking at all the wrong places. The ONLY file which needs changing is getdata.php which is in the root of your OC. So, default autocomplete is for the product name only. It is defined in this file like this:
$sql = "SELECT pd.name FROM " . DB_PREFIX . "product p," . DB_PREFIX . "product_description pd WHERE p.status = 1 AND p.product_id = pd.product_id AND language_id = '".$id."' AND UPPER(pd.name) like UPPER('%$q%') GROUP BY pd.product_id ORDER BY pd.name ASC";
$res = mysql_query($sql);
if(mysql_num_rows($res)>0){
while($ro = mysql_fetch_assoc($res)){
$name = str_replace( array( '\'', '"', ',' , ';', '<', '>','&quot','&'), ' ', $ro['name']);
//$str[]= $name."\n";
echo $name."\n";
}
}
Now, all you need to do is add the exact query for model or whichever database field in the oc_product table you need autocomplete for. Meaning, for example, copy the name query and replace pd.name with p.model and wherever you find ['name']replace with ['model']. And voila! I really hope this helps someone, as up until today I had trouble realizing this myself.

Related

PHP fix Column 'id' in field list is ambiguous without change column name

I have this vode for show result using PHP join method:
$DB_QUERY = mySqli::f("SELECT name,id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
foreach($DB_QUERY as $row){
echo $row['id'];
}
Now I see this :
Column 'id' in field list is ambiguous
I now this error when I have two column with name id. how do fix this error without change id name in one column?!
EDIT:
author table:
id|name|
authors table:
id|author_id|book_id
$DB_QUERY = mySqli::f("SELECT author.name, author.id as a_id,
authors.id as as_id, authors.author_id, authors.book_id
FROM author INNER JOIN authors ON author.id = authors.author_id
WHERE authors.book_id = ? ORDER BY name ASC LIMIT 8 ", $id);
For instance.
Then your field real names haven't changed but in your php you can use their temporary "nicknames" a_id and as_id.
Though I am not sure you need authors.id for anything... if it is only your PK on the table maybe you should drop it and use authors.author_id and authors.book_id as your PK... or.. if you are not bringing content from other tables with it... just don't mention it on your select.
You have selected id in your query without any alias & you are applying join on Author & Authors table. I think as both tables contains id column you are getting the error.
Try this
$DB_QUERY = mySqli::f("SELECT name, " . AUTHORS . ".id, " . AUTHOR . ".id,author_id,book_id FROM " . AUTHOR . " JOIN " . AUTHORS . " ON
" . AUTHOR . ".id = " . AUTHORS . ".author_id WHERE " . AUTHORS . ".book_id = ? ORDER BY name ASC LIMIT 8 ", $id);

Why is this PHP / mySQL query giving me an error?

I am generating the first part of the query like this:
while ($all_products = $db->fetch_array($all_prods))
{
$filter_string .= 'AND product_id !=';
$filter_string .= $all_products['item_id'];
$filter_string .= ' ';
}
and then the second part like this:
$sql_more_items = $db->query("SELECT * FROM db_products
WHERE owner_id='" . $user_id . "' AND active=1 '" . $filter_string . "'
ORDER BY RAND() LIMIT 10");
However it's giving me a mySQL syntax error and the $filter_string part strangely adds ' twice before and after the string, so it runs like this:
WHERE user_id='12345' AND active=1 'AND product_id !=0001 AND product_id !=0002 ' ORDER BY RAND ...
What am I doing wrong?
$filter_string adds ' because you put it there. :P
Try with just the double quotes around $filter_string:
$sql_more_items = $db->query("SELECT * FROM db_products WHERE owner_id='" . $user_id . "' AND active=1 " . $filter_string . "ORDER BY RAND() LIMIT 10");
$sql_more_items = $db->query("SELECT * FROM db_products
WHERE owner_id='" . $user_id . "' AND active=1 '" . $filter_string . "'
ORDER BY RAND() LIMIT 10");
Check the way you're performing a string concatenation (putting together strings). It seems like there's a copy/paste error as you're using '" instead of just a "
I would use whitespace (and a good code editor) to your advantage by reformatting your code to look like this:
$queryString = "SELECT * FROM db_products WHERE owner_id='$user_id'"
." AND active=1 " //Note these
. $filter_string //are separated
. "ORDER BY RAND() LIMIT 10 "; //into individual lines
$sql_more_items = $db->query($queryString);
This style helps you keep track of whether you're using " or ' for your strings and also helps you debug things more easily than putting it into one giant hard to read string.
That's probably because of the part
`"' AND active=1 '"`
^.... This ' here

Entering my WHERE into the query breaks it

So I have to add a WHERE query to this plugin I'm using for a reporting feature on a WordPress site. I have no time to do anything but add in another column and filter by the values in that column as there is not that much data to manage each update. The default value for the column I added is zero but I'll add new entries to represent years new people are added. However, when I filter based on the column value the whole query breaks and doesn't show up. I have no idea why. Here is the section involving its set up query displaying results.
<?php
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . "presidentsreport_breakdown WHERE list_id = " . $atts['list_id'];
$total_breakdowns = $wpdb->get_var($sql);
$sql = "SELECT p.person_id, p.name, p.notes, p.school_year, b.breakdown_id, b.name as breakdown, b.description as breakdown_description FROM " . $wpdb->prefix . "presidentsreport_person p INNER JOIN " . $wpdb->prefix . "presidentsreport_breakdown b ON b.breakdown_id = p.breakdown_id INNER JOIN " . $wpdb->prefix . "presidentsreport_list l ON l.list_id = b.list_id";
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
$where = "";
if($search != ''){
$where = " AND (p.name LIKE %s)";
$arg = '%' . $search . '%';
$args = array($arg);
}
$where = $wpdb->prepare($where, $args);
$order = " ORDER BY b.sort_order, b.breakdown_id, p.sort_name, p.name, p.person_id";
$results = $wpdb->get_results($sql . $clean_where . $where . $order);
?>
If I add anything in the variable $where it breaks the whole query. So if I add
<?php
$where = " WHERE p.school_year <= 2011";
?>
or
<?php
$where = " WHERE p.school_year = 0";
?>
Nothing will show up, For the last example if the default value is 0 everything should show up regardless. Thanks in advance for reading through!
Don't add WHERE to your variable. It is already assigned in $clean_where
$clean_where = " WHERE l.list_id = " . $atts['list_id'];
Here ------------^
You need to concatenate your addition parameters to the $where variable:
$where .= " AND p.school_year <= 2011";
There's no need of WHERE in where!

Opencart use Category as parent for store ( multi store system )

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

mysql query selecting wrong field

$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'] . '"';
}

Categories