Hey Mark,
Thanks mate this works excellently my query is down from 45 seconds to 1-1.5 seconds!!
Just noticed a wierd problem though... but I am pretty sure it was like this before your code.
http://www.designerclothesforless.co...-jeans.php?p=1 if you click to go on to page 2, 3 etc.. it doesn't display any products LOL
Any ideas?
Thanks for all your help BTW
Function:
PHP Code:
function getProducts2($query){
$starttime = microtime();
$startarray = explode(" ", $starttime);
$starttime = $startarray[1] + $startarray[0];
$result = mysql_query($query) or die(mysql_error());
$result2 = mysql_query("SELECT FOUND_ROWS()") or die(mysql_error());
$result2row = mysql_fetch_assoc($result2);
$totalrows = $result2row['FOUND_ROWS()'];
$num = mysql_num_rows($result);
//rest of code goes here
$i=0;
$return = array();
while ($i < $num) {
$return[$i]['name'] = mysql_result($result,$i,"name");
$return[$i]['description'] = mysql_result($result,$i,"description");
$return[$i]['url'] = mysql_result($result,$i,"url");
$return[$i]['imageurl'] = mysql_result($result,$i,"imageurl");
$return[$i]['price'] = mysql_result($result,$i,"price");
$return[$i]['price'] = number_format($return['price'], 2);
//SWITCH HYPHENS BACK
$return[$i]['name'] = str_replace("HYPHEN", "-", $return[$i]['name']);
$return[$i]['description'] = str_replace("HYPHEN", "-", $return[$i]['description']);
$return[$i]['url'] = str_replace("HYPHEN", "-", $return[$i]['url']);
$return[$i]['imageurl'] = str_replace("HYPHEN", "-", $return[$i]['imageurl']);
$return[$i]['price'] = str_replace("HYPHEN", "-", $return[$i]['price']);
$return[$i]['description'] = summarize_search_result($return[$i]['description'], 30);
// check image url and store
$var = @fopen("{$return[$i]['imageurl']}","r");
if ($var) $return[$i]['imageurl'] = $return[$i]['imageurl'];
else $return[$i]['imageurl'] = null;
$i++;
}
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime - $starttime;
$totaltime = round($totaltime,5);
echo "<b>$i.This page loaded in $totaltime seconds.</b><br><br>";
//return the total number of rows and the rows of data for this page
return array($totalrows, $return);
}
Page:
PHP Code:
dbConnect();
$results_per_page = 30;
if(!isset($_GET['p'])) $page_numer = 0;
else $page_numer = (int)$_GET['p'];
if(!is_int($page_numer)) die('exploit attempt, page is not a number');
$offset = $page_numer*$results_per_page;
list($num, $prods) = getProducts2("SELECT SQL_CALC_FOUND_ROWS * FROM products WHERE MATCH(category, name) AGAINST('+(jean jeans) +(mens men\'s mans man\'s \"designer men\") -(sweatshirt beanie shirt t-shirt)' IN BOOLEAN MODE) ORDER BY $sType $sDirection LIMIT $offset, $results_per_page");
if($num == 0) echo "<b><center>There are currently no products in this section</center></b>";
else {
$x = $results_per_page * ($page_numer - 1);
for($i=$x;$i<$results_per_page;$i++) {
if($prods[$i]['imageurl'] == null) echo "<a href='{$prods[$i]['url']}' target='_blank' rel='nofollow'><img src='assets/images/blank.gif'
align='right' border='0' width='100' height='100' alt='{$prods[$i]['name']}'/></a><b><a href='{$prods[$i]['url']}' target='_blank'
rel='nofollow'>{$prods[$i]['name']}</a></b><br/>{$prods[$i]['description']} <a href='{$prods[$i]['url']}' target='_blank' rel='nofollow'>Read
more</a><br/><br/><b>Price:</b> £{$prods[$i]['price']}<br clear='all'><br/><br/>";
else echo "<a href='{$prods[$i]['url']}' target='_blank' rel='nofollow'><img src='{$prods[$i]['imageurl']}' align='right' border='0' width='125'
height='123' alt='{$prods[$i]['name']}'/></a><b><a href='{$prods[$i]['url']}' target='_blank'
rel='nofollow'>{$prods[$i]['name']}</a></b><br/>{$prods[$i]['description']} <a href='{$prods[$i]['url']}' target='_blank' rel='nofollow'>Read
more</a><br/><br/><b>Price:</b> £{$prods[$i]['price']}<br clear='all'><br/><br/>";
}
}
echo "Page: ";
$number_of_pages = ceil($num / $results_per_page);
for($i=1;$i<$number_of_pages;$i++) {
if($i == $page_numer) echo " $i | ";
else echo "<a href='?p=$i'>$i</a> |";
}
echo "<br><br>";
-Ben
Bookmarks