Excluding previous values from database using PHP and mysqli - php

So let's say I have 30 entries in my database, I select the first 7 to get on my first page like this:
$qryRandomGallery = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
FROM tblKWKids AS A
LEFT JOIN tblScore AS b
ON a.ScoreID = b.ScoreID
LEFT JOIN tblUser as C
ON a.UserID = c.UserID
ORDER BY RAND()
LIMIT 6";
if ($stmtRandomGallery = mysqli_prepare($dbconn, $qryRandomGallery)) {
mysqli_stmt_execute($stmtRandomGallery);
mysqli_stmt_bind_result($stmtRandomGallery, $KWTitel, $KWURL, $KWID, $KWKiddyBeschrijving, $ScoreAfb, $USER);
mysqli_stmt_store_result($stmtRandomGallery);
}
$qryRandomGalleryBIG = "SELECT a.Titel, a.KW, a.KWKidsID, a.KWKidsBeschrijving, b.ScoreAfbeelding, c.GebruikersNaam
FROM tblKWKids AS A
LEFT JOIN tblScore AS b
ON a.ScoreID = b.ScoreID
LEFT JOIN tblUser as C
ON a.UserID = c.UserID
ORDER BY RAND()
LIMIT 1";
if ($stmtRandomGalleryBIG = mysqli_prepare($dbconn, $qryRandomGalleryBIG)) {
mysqli_stmt_execute($stmtRandomGalleryBIG);
mysqli_stmt_bind_result($stmtRandomGalleryBIG, $KWTitelB, $KWURLB, $KWIDB, $KWKiddyBeschrijvingB, $ScoreAfbB, $USERB);
mysqli_stmt_store_result($stmtRandomGalleryBIG);
}
and then this as my php
while(mysqli_stmt_fetch($stmtRandomGallery)){
$content .= '<div>';
$content .= '<a href="galerij.php?id=' . $KWID . '">';
$content .= '<img src="' . $KWURL . '" title="' . $KWTitel . '" alt="' . $KWTitel . '" class="image">';
$content .= '<h5>' . $KWTitel . ' door: ' . $USER . '</h5>';
$content .= '<p>' . $KWKiddyBeschrijving . '</p>';
$content .= '<img src="' . $ScoreAfb . '" title="Score" alt="Score" class="img">';
$content .= '</a>';
$content .= '</div>';
}
while(mysqli_stmt_fetch($stmtRandomGalleryBIG)){
$content .= '<h2>Uitgelicht werk van ' . $USERB . '</h2>';
$content .= '<a href="galerij.php?id=' . $KWIDB . '">';
$content .= '<img src="' . $KWURLB . '" title="' . $KWTitelB . '" alt="' . $KWTitelB . '" id="image">';
$content .= '<h3>' . $KWTitelB . ' door: ' . $USERB . '</h3>';
$content .= '<h4>' . $KWKiddyBeschrijvingB . '</h4>';
$content .= '<img src="' . $ScoreAfbB . '" title="Score" alt="Score" id="score">';
$content .= '</a>';
}
Now, how do I exclude the results from the first stmt to select another random image for my BIG image?

Simple: Take the IDs of your first query and stuff them into the second query as a not in:
SELECT ...
FROM yourtable
WHERE idField NOT IN (x,y,z,p,q,r)
This'll exclude them from second query's results.

Related

MySQL LIKE keyword Multiple Columns but Which Column Queried?

I have two scripts below. The first script only searches one Column for a user-typed keyword, then it will display the results as a list and make BOLD the characters the user typed. That script works great.
The second script is something I modified to search multiple columns. It searches just fine. The problem is that I cannot get the BOLD (or make STRONG) if the searched value came from the other columns. How do I determine if the searched value came for Column 1, Column 2, Column 3, etc...? If the searched value came from column "DESCRIP" then I want to make the letter bold in the listed value.
First Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE `MUNTERS_PN` LIKE (:keyword) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Second Script:
<?php
require_once("../config.php");
$keyword = '%'.$_POST['keyword'].'%';
$rootval = $_POST['rval'];
$sql = "SELECT `ID`,`MUNTERS_PN`,`DESCRIP`, `IMG_PATH`, `MANUF`, `MANUF_PN` FROM `electrical_parts` WHERE (`MUNTERS_PN` LIKE (:keyword) OR `DESCRIP` LIKE (:keyword) OR `MANUF` LIKE (:keyword) OR `MANUF_PN` LIKE (:keyword) ) ORDER BY `MUNTERS_PN` ASC LIMIT 0, 5";
$query = $db_qms->prepare($sql);
$query->bindParam(':keyword', $keyword, PDO::PARAM_STR);
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
/******* INSERT CODE TO DETERMINE WHICH COLUMN WAS QUERIED ******/
// put in bold the written text
$partnum = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MUNTERS_PN']);
$manuf = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF']);
$manuf_pn = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['MANUF_PN']);
$descrip = str_replace($_POST['keyword'], '<span style="font-weight:700;font-size:14px;">'.$_POST['keyword'].'</span>', $rs['DESCRIP']);
// add new option
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $rs['MANUF_PN'] . '] <br/>' . $rs['DESCRIP'] . '</span></li>';
}
?>
Be careful, in the echo in the second script, you use $rs['MANUF'], $rs['MANUF_PN'] and $rs['DESCRIP'] instead of $manuf, $manuf_pn and $descrip.
That is why the replacements don't appear in the output.
So you should use :
echo '<li class="set_part" data-val="' . $rs['ID'] . '"><img src="' . $rootval . '../parts/' . $rs['IMG_PATH'] . '" width="100px;" style="padding-right:15px;">'.$partnum.'<span style="font-style:italic;font-size:13px;padding-left:10px;">[' . $rs['MANUF'] . ': ' . $manuf_pn . '] <br/>' . $descrip . '</span></li>';

mySQL for loop stops working after 31 row updates [Actually PHP related]

I have a form that uses a PHP for loop after it is submitted. The loop updates individual rows in the database one after the next based on the form data submitted. However, I've noticed that the loop stops working if there are more than 31 rows that need to be updated. It will update everything perfectly for the first 31 loops but then will not work after the 31st loop?
UPDATE: I've done some testing and it seems that the loop never finishes. It just refreshes the page in the browser instead of printing the results. What is causing this?
Is there a MYSQL query setting somewhere that is causing this?
Here is my loop's code:
for($i=1;$i<=$total_results2;$i++)
{
$strSQL = 'UPDATE timesheets_items SET ';
$strSQL .= 'time_1 = "' . convert_time($_POST['item_' . $i . '_time_1']) . '"';
$strSQL .= ', time_2 = "' . convert_time($_POST['item_' . $i . '_time_2']) . '"';
$strSQL .= ', time_3 = "' . convert_time($_POST['item_' . $i . '_time_3']) . '"';
$strSQL .= ', time_4 = "' . convert_time($_POST['item_' . $i . '_time_4']) . '"';
$strSQL .= ', time_5 = "' . convert_time($_POST['item_' . $i . '_time_5']) . '"';
$strSQL .= ', time_6 = "' . convert_time($_POST['item_' . $i . '_time_6']) . '"';
$strSQL .= ', time_7 = "' . convert_time($_POST['item_' . $i . '_time_7']) . '"';
$strSQL .= ', time_8 = "' . convert_time($_POST['item_' . $i . '_time_8']) . '"';
$strSQL .= ', time_9 = "' . convert_time($_POST['item_' . $i . '_time_9']) . '"';
$strSQL .= ', time_10 = "' . convert_time($_POST['item_' . $i . '_time_10']) . '"';
$strSQL .= ', time_11 = "' . convert_time($_POST['item_' . $i . '_time_11']) . '"';
$strSQL .= ', time_12 = "' . convert_time($_POST['item_' . $i . '_time_12']) . '"';
$strSQL .= ', time_13 = "' . convert_time($_POST['item_' . $i . '_time_13']) . '"';
$strSQL .= ', time_14 = "' . convert_time($_POST['item_' . $i . '_time_14']) . '"';
$strSQL .= ', time_total = "' . convert_time($_POST['item_' . $i . '_time_total']) . '"';
$strSQL .= ', ot_1 = "' . convert_time($_POST['item_' . $i . '_ot_1']) . '"';
$strSQL .= ', ot_2 = "' . convert_time($_POST['item_' . $i . '_ot_2']) . '"';
$strSQL .= ', ot_3 = "' . convert_time($_POST['item_' . $i . '_ot_3']) . '"';
$strSQL .= ', ot_4 = "' . convert_time($_POST['item_' . $i . '_ot_4']) .'"';
$strSQL .= ', ot_5 = "' . convert_time($_POST['item_' . $i . '_ot_5']) . '"';
$strSQL .= ', ot_6 = "' . convert_time($_POST['item_' . $i . '_ot_6']) . '"';
$strSQL .= ', ot_7 = "' . convert_time($_POST['item_' . $i . '_ot_7']) . '"';
$strSQL .= ', ot_8 = "' . convert_time($_POST['item_' . $i . '_ot_8']) . '"';
$strSQL .= ', ot_9 = "' . convert_time($_POST['item_' . $i . '_ot_9']) . '"';
$strSQL .= ', ot_10 = "' . convert_time($_POST['item_' . $i . '_ot_10']) . '"';
$strSQL .= ', ot_11 = "' . convert_time($_POST['item_' . $i . '_ot_11']) . '"';
$strSQL .= ', ot_12 = "' . convert_time($_POST['item_' . $i . '_ot_12']) . '"';
$strSQL .= ', ot_13 = "' . convert_time($_POST['item_' . $i . '_ot_13']) . '"';
$strSQL .= ', ot_14 = "' . convert_time($_POST['item_' . $i . '_ot_14']) . '"';
$strSQL .= ', ot_total = "' . convert_time($_POST['item_' . $i . '_ot_total']) . '"';
$strSQL .= 'WHERE week_start = "' . $week_start . '" AND employee_id = "' . $id . '" AND project_number = "' . $_POST['item_' . $i . '_project_number'] .'" AND task = "' . $_POST['item_' . $i . '_task'] .'"';
mysql_query($strSQL);
}
I have now solved the issue ! The problem was caused by my version of PHP (5.3) and the amount of input variables allowed by PHP on a form.
The problem ended up not being MySQL related at all. The bug found was limiting how many form variables were taken in by PHP thus not allowing MySQL to ever get to see those variables when it came time to run the repeated queries loop.
To resolve the issue, I had to update to PHP (5.4 or greater) and then set the "max_input_vars" setting to 3000. (The default setting only allows 1000.)
I hope this helps anyone who ever comes across this same problem.

Selecting multiple random values from 1 table? PHP

I want 3 random images, first is working, but the other two just won't work.
Preferably, I want to pick 2 other images then the first and the second, and the third to be all different.
Here's my code:
$content .= '<div>';
$qryFirstImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
FROM tblKWKids AS a
LEFT JOIN tblUser AS b
ON a.UserID = b.UserID
ORDER BY RAND()";
if ($stmt = mysqli_prepare($dbconn, $qryFirstImage)) {
mysqli_stmt_bind_result($stmt, $KW, $KWKidsBeschrijving, $TitelKW, $GebruikersNaam);
mysqli_stmt_execute($stmt);
mysqli_stmt_fetch($stmt);
mysqli_close($dbconn);
}
$content .= '<img src="' . $KW . '" width="240px" height="240px" alt="' . $TitelKW . '" title="' . $TitelKW . '">';
$content .= '<h5>' . $TitelKW . ' door: ' . $GebruikersNaam . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving . '</p>';
$content .= '</div>';
$content .= '<div>';
$qrySecondImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
FROM tblKWKids AS a
LEFT JOIN tblUser AS b
ON a.UserID = b.UserID
ORDER BY RAND()";
if ($stmt2 = mysqli_prepare($dbconn, $qrySecondImage)) {
mysqli_stmt_bind_result($stmt2, $KW2, $KWKidsBeschrijving2, $TitelKW2, $GebruikersNaam2);
mysqli_stmt_execute($stmt2);
mysqli_stmt_fetch($stmt2);
}
$content .= '<img src="' . $KW2 . '" width="240px" height="240px" alt="' . $TitelKW2 . '" title="' . $TitelKW2 . '">';
$content .= '<h5>' . $TitelKW2 . ' door: ' . $GebruikersNaam2 . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving2 . '</p>';
$content .= '</div>';
$content .= '<div>';
$qryThirdImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
FROM tblKWKids AS a
LEFT JOIN tblUser AS b
ON a.UserID = b.UserID
ORDER BY RAND()";
if ($stmt3 = mysqli_prepare($dbconn, $qryThirdImage)) {
mysqli_stmt_bind_result($stmt3, $KW3, $KWKidsBeschrijving3, $TitelKW3, $GebruikersNaam3);
mysqli_stmt_execute($stmt3);
mysqli_stmt_fetch($stmt3);
}
$content .= '<img src="' . $KW3 . '" width="240px" height="240px" alt="' . $TitelKW3 . '" title="' . $TitelKW3 . '">';
$content .= '<h5>' . $TitelKW3 . ' door: ' . $GebruikersNaam3 . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving3 . '</p>';
$content .= '</div>';
Please look at those
php.net/mysqli_stmp::fetch
php.net/mysqli_stmt::close
Wrote very little code, search and much more.
$qryFirstImage = "SELECT a.KW, a.KWKidsBeschrijving, a.Titel, b.GebruikersNaam
FROM tblKWKids AS a
LEFT JOIN tblUser AS b
ON a.UserID = b.UserID
ORDER BY RAND()";
if ($stmt = mysqli_prepare($dbconn, $qryFirstImage)) {
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $KW, $KWKidsBeschrijving, $TitelKW, $GebruikersNaam);
for($i = 0; ($i < 3 && mysqli_stmt_fetch($stmt)); $i++){
$content .= '<div>';
$content .= '<img src="' . $KW . '" width="240px" height="240px" alt="' . $TitelKW . '" title="' . $TitelKW . '">';
$content .= '<h5>' . $TitelKW . ' door: ' . $GebruikersNaam . '</h5>';
$content .= '<p>' . $KWKidsBeschrijving . '</p>';
$content .= '</div>';
}
mysqli_stmt_close($stmt);
}

How do I fetch value of category from database using posted numeric id

First of all, forgive me for being very green with PHP. I am a recruiter for dentists and
I have a form that I have my candidates fill out online.
I created 4 mysql tables that hold information for discipline, type buyer, transition time and graduation year. This allows me to change options 1 time rather than doing this on multiple pages on my website.
You can see the form at http://www.missouridentalpracticeforsale.com/indexnewbjs.php
When the form is submitted, I have it working correctly to dump in the database into the database table I created. However, I have an email that is sent to me every time I get a new lead and for the fields that have been created in these tables, I am only getting the ID value and I need to get the content associated with the ID..
Ex. instead of getting dentist for discipline, I get the value of 1
Instead of getting orthodontist buyer for type of buyer I get #3
Here is my code for both the index page and the form submittal page
----------small portion of code for 2 fields Discipline and Type Buyer--------------
<tr>
<td>Discipline:</td>
<td><select name="disc" class="tableappointdata" id="disc">
<?php $discpfs_sql = 'SELECT id, disc FROM discpfs WHERE id >= 0'; $discpfs_res = sql_query($discpfs_sql,$leads_db); while($discpfs = mysql_fetch_array($discpfs_res)){ echo '<option value="' . $discpfs['id'] . '">' . $discpfs['disc'] . '</option>'; } ?>
</select> </td>
</tr>
<tr>
<td>Type Buyer:</td>
<td><select name="typebuy" class="tableappointdata" id="typebuy">
<?php $typebuy_sql = 'SELECT id, typebuy FROM typebuy WHERE id >= 0'; $typebuy_res = mysql_query($typebuy_sql,$leads_db); while($typebuy = mysql_fetch_array($typebuy_res)){ echo '<option value="' . $typebuy['id'] . '">' . $typebuy['typebuy'] . '</option>'; } ?>
</select> </td>
</tr>
-------------------------------Form Submittal Code------------------------------------
$msg .= 'Name: ' . $_POST['first_name'] . ' ' . $_POST['last_name'] . '<br />' . "\r\n";
$msg .= 'Discipline: ' . $_POST ['disc'] . '<br />' . "\r\n";
$msg .= 'Type Buyer: ' . $_POST['typebuy'] . '<br />' . "\r\n";
$msg .= 'Transition Time: ' . $_POST['transtime'] . '<br />' . "\r\n";
$msg .= 'Practice Type Desired: ' . $_POST['desire'] . '<br />' . "\r\n";
$msg .= 'Practice Size Wanted: ' . $_POST['practsize'] . '<br />' . "\r\n";
$msg .= 'Number of Ops: ' . $_POST['numberops'] . '<br />' . "\r\n";
$msg .= 'Graduation Year: ' . $_POST['grad'] . '<br />' . "\r\n";
$msg .= 'City: ' . $_POST['city'] . '<br />' . "\r\n";
$msg .= 'State: ' . $_POST['state'] . '<br />' . "\r\n";
$msg .= 'Zip: ' . $_POST['zip'] . '<br />' . "\r\n";
I know this is probably a very easy fix, but I am going crazy trying to figure out how I get the words to show up and not the corresponding ID numbers from the table in my email.
Any help would be greatly appreciated.
Thanks
Here is the error.
echo '<option value="' . $discpfs['id'] . '">' . $discpfs['disc'] . '</option>';
You are displaying the actual content on the select option, but what is sent by the form's POST method is the "value" field, that is, the $discpfs['id'].
You have 2 options them, if you are not using the id after submitting the form you can send the actual content on the "value" field
echo '<option value="' . $discpfs['disc'] . '">' . $discpfs['disc'] . '</option>';
but if you use the id, you can make another query using the id and build your string $msg using the value retrieved by the query.
Use a query like this:
$discpfs_sql = 'SELECT id, disc FROM discpfs WHERE id ='. $_POST['disc'];
Use this code instead.
$disc_id=$_POST ['disc'];
$discpfs_sql = mysql_query("SELECT disc FROM discpfs WHERE id = '$disc_id'");
$disc=mysql_fetch_array($discpfs_sql);
$type_id=$_POST ['typebuy'];
$typebuy_sql = mysql_query("SELECT typebuy FROM typebuy WHERE id = '$type_id'");
$typebuy=mysql_fetch_array($typebuy_sql);
$msg .= 'Name: ' . $_POST['first_name'] . ' ' . $_POST['last_name'] . '<br />' . "\r\n";
$msg .= 'Discipline: ' . $disc['disc'] . '<br />' . "\r\n";
$msg .= 'Type Buyer: ' . $typebuy['typebuy'] . '<br />' . "\r\n";
$msg .= 'Transition Time: ' . $_POST['transtime'] . '<br />' . "\r\n";
$msg .= 'Practice Type Desired: ' . $_POST['desire'] . '<br />' . "\r\n";
$msg .= 'Practice Size Wanted: ' . $_POST['practsize'] . '<br />' . "\r\n";
$msg .= 'Number of Ops: ' . $_POST['numberops'] . '<br />' . "\r\n";
$msg .= 'Graduation Year: ' . $_POST['grad'] . '<br />' . "\r\n";
$msg .= 'City: ' . $_POST['city'] . '<br />' . "\r\n";
$msg .= 'State: ' . $_POST['state'] . '<br />' . "\r\n";
$msg .= 'Zip: ' . $_POST['zip'] . '<br />' . "\r\n";

How to improve getTotalProducts on Opencart controllers

i didnt know before but there is a huge bug in Opencart official release, in a store when you have a lot, of products & categories, it takes upon 50sec!!! to load a page. 50sec!!! , i take a look into the code and google, and found that the problem is well documented, as almost everybody know that this line is causing everything. (counting from cache)
$product_total = $this->model_catalog_product->getTotalProducts($data);
The solution posted every where consists in comment out this line , wich by the way at least for me works better if i just set $product_total to be empty.. Like this
//$product_total = $this->model_catalog_product->getTotalProducts($data);
$product_total = "";
Anyway my problem was solved (the page load in 3 seconds instead of 50 sec) but the count was missing so i keep looking and finally i found this solution wich is pretty much , (until now, still testing) the best solution if your database is handling with a lot of products and categories..http://ergopho.be/speeding-up-opencart-using-the-cache-class/
What it does is basically wrap this entire section of code in an if block, and first checking if the file exists in the cache. If it does not, we run it like normal, and then store it to the cache. If it does, use the cached version instead.
In Controller/Common/Header.php you can found also this code (in the article he do it on categories) here is the code in this file also
$this->data['categories'] = array();
$categories = $this->model_catalog_category->getCategories(0);
foreach ($categories as $category) {
if ($category['top']) {
// Level 2
$children_data = array();
$children = $this->model_catalog_category->getCategories($category['category_id']);
foreach ($children as $child) {
$data = array(
'filter_category_id' => $child['category_id'],
'filter_sub_category' => true
);
$product_total = $this->model_catalog_product->getTotalProducts($data);
$children_data[] = array(
'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $product_total . ')' : ''),
'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'])
);
}
Yo have to wrap all this code into this lines
$this->data['categories'] = $this->cache->get('categories');
if(!count($this->data['categories'])) {
<!--Here goes the above code-->
$this->cache->set('categories', $this->data['categories']);
}
Its working fine so far, i hope this help some one else, also pls note that if you have a better way of doing this, i know there is a lot of not advanced users looking for this, so if you can share it with us, it would be great.
¿Can anyone figure out a better fix for this awful slowy bug?
Thanks and lest hope for the next version of Opencart this kind of issues are fixed.
Hope this helps. Peace
This really should go into the model as opposed to the controller.
I have a site that has over 15,000 active products. My method for this looks like so:
public function getTotalProducts($data = array()) {
if ($this->customer->isLogged()):
$customer_group_id = $this->customer->getCustomerGroupId();
else:
$customer_group_id = $this->config->get('config_customer_group_id');
endif;
$sql = "SELECT COUNT(DISTINCT p.product_id) AS total";
if (!empty($data['filter_category_id'])):
if (!empty($data['filter_sub_category'])):
$sql .= " FROM {$this->prefix}category_path cp
LEFT JOIN {$this->prefix}product_to_category p2c
ON (cp.category_id = p2c.category_id)";
else:
$sql .= " FROM {$this->prefix}product_to_category p2c";
endif;
if (!empty($data['filter_filter'])):
$sql .= " LEFT JOIN {$this->prefix}product_filter pf
ON (p2c.product_id = pf.product_id)
LEFT JOIN {$this->prefix}product p
ON (pf.product_id = p.product_id)";
else:
$sql .= " LEFT JOIN {$this->prefix}product p
ON (p2c.product_id = p.product_id)";
endif;
else:
$sql .= " FROM {$this->prefix}product p";
endif;
$sql .= " LEFT JOIN {$this->prefix}product_description pd
ON (p.product_id = pd.product_id)
LEFT JOIN {$this->prefix}product_to_store p2s
ON (p.product_id = p2s.product_id)
WHERE pd.language_id = '" . (int)$this->config->get('config_language_id') . "'
AND p.status = '1'
AND p.date_available <= NOW()
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'";
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'] . "'";
endif;
if (!empty($data['filter_filter'])):
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id):
$implode[] = (int)$filter_id;
endforeach;
$sql .= " AND pf.filter_id IN (" . implode(',', $implode) . ")";
endif;
endif;
if (!empty($data['filter_name']) || !empty($data['filter_tag'])):
$sql .= " AND (";
if (!empty($data['filter_name'])):
$implode = array();
$words = explode(' ', trim(preg_replace('/\s\s+/', ' ', $data['filter_name'])));
foreach ($words as $word):
$implode[] = "pd.name LIKE '%" . $this->db->escape($word) . "%'";
endforeach;
if ($implode):
$sql .= " " . implode(" AND ", $implode) . "";
endif;
if (!empty($data['filter_description'])):
$sql .= " OR pd.description LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
endif;
endif;
if (!empty($data['filter_name']) && !empty($data['filter_tag'])):
$sql .= " OR ";
endif;
if (!empty($data['filter_tag'])):
$sql .= "pd.tag LIKE '%" . $this->db->escape(utf8_strtolower($data['filter_tag'])) . "%'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.model) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.sku) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.upc) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.ean) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.jan) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.isbn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
if (!empty($data['filter_name'])):
$sql .= " OR LCASE(p.mpn) = '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "'";
endif;
$sql .= ")";
endif;
if (!empty($data['filter_manufacturer_id'])):
$sql .= " AND p.manufacturer_id = '" . (int)$data['filter_manufacturer_id'] . "'";
endif;
$cache = md5 ((int)$customer_group_id . serialize ($data));
$total = $this->cache->get('product.total.products.' . $cache);
if (!$total):
$query = $this->db->query($sql);
$total = $query->row['total'];
$this->cache->set('product.total.products.' . $cache, $total);
endif;
return $total;
}
You'll notice the caching section near the end there.
I also converted over to memcached for caching query results as opposed to file cache. The real problem comes in when you use seo_urls for this many products, I had to completely rewite the entire SeoUrl Controller and the Url library, but it's fast and I've got awesome urls :)

Categories