Forum Moderators: coopster

Message Too Old, No Replies

Make product sorting work

How to make product sorting work using php

         

borisz

8:02 pm on Dec 30, 2010 (gmt 0)

10+ Year Member



hi all,

this is my fourth post here and so far I got great answers and help. I got a bit more complex question now which I can’t figure out alone since I’m still PHP beginner and hopefully someone will help me with this. What I want is to make product sort menu, for example when user click on “Price” or “Expiration Date” it sort results by those values DESC or ASC. Also I want when user click “Free Shipping” to show products that include ONLY free shipping. I get results from database using below code and the page Im working on is called deals.php:

<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
if (PHP_VERSION < 6) {
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
}

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$maxRows_LatestDeals = 15;
$pageNum_LatestDeals = 0;
if (isset($_GET['pageNum_LatestDeals'])) {
$pageNum_LatestDeals = $_GET['pageNum_LatestDeals'];
}
$startRow_LatestDeals = $pageNum_LatestDeals * $maxRows_LatestDeals;

$colname_LatestDeals = "-1";
if (isset($_GET['ProductBrand'])) {
$colname_LatestDeals = $_GET['ProductBrand'];
}
mysql_select_db($database_coupons_database, $coupons_database);
$query_LatestDeals = sprintf("SELECT * FROM products WHERE ProductBrand = %s ORDER BY ProductDateAdded DESC", GetSQLValueString($colname_LatestDeals, "text"));
$query_limit_LatestDeals = sprintf("%s LIMIT %d, %d", $query_LatestDeals, $startRow_LatestDeals, $maxRows_LatestDeals);
$LatestDeals = mysql_query($query_limit_LatestDeals, $coupons_database) or die(mysql_error());
$row_LatestDeals = mysql_fetch_assoc($LatestDeals);

if (isset($_GET['totalRows_LatestDeals'])) {
$totalRows_LatestDeals = $_GET['totalRows_LatestDeals'];
} else {
$all_LatestDeals = mysql_query($query_LatestDeals);
$totalRows_LatestDeals = mysql_num_rows($all_LatestDeals);
}
$totalPages_LatestDeals = ceil($totalRows_LatestDeals/$maxRows_LatestDeals)-1;

$colname_BrandPass = "-1";
if (isset($_GET['BrandName'])) {
$colname_BrandPass = $_GET['BrandName'];
}
mysql_select_db($database_coupons_database, $coupons_database);
$query_BrandPass = sprintf("SELECT * FROM brands WHERE BrandName = %s", GetSQLValueString($colname_BrandPass, "text"));
$BrandPass = mysql_query($query_BrandPass, $coupons_database) or die(mysql_error());
$row_BrandPass = mysql_fetch_assoc($BrandPass);
$totalRows_BrandPass = mysql_num_rows($BrandPass);
?>


as from above you can see when user go to url e.g. deals.?ProductBrand=NameOfBrand it will show results for "NameOfBrand" now this is the sorting bar:


<div id="Sorter">
SORT BY: <a href=""><img src="../images/brands/Sorter.png" border="0" />Expiration Date</a> <span>|</span>
<a href="">Price</a>
<span></span><span></span><span></span>
SHOW ONLY:
<span id="SelectedSort"><img src="../images/brands/Sorter.png" border="0" />All</span> <span>|</span>
<a href="deals.php?ProductShipping=1">Free Shipping</a>
</div>


and this is results container:


<?php do { ?>
<div class="BrandDealsResults">
<?php
if($row_LatestDeals['ProductThumb']=="") {
echo '<div id="BrandDealsImgContainer"></div>';
}
else
{
echo '<div id="BrandDealsImgContainer"><a href="../products/coupons/details.php?ProductID='. $row_LatestDeals['ProductID'] .'"><img src="'. $row_LatestDeals['ProductThumb'] .'" alt="Low Cost Products" align="left" border="0"/></a></div>';}?>
<div id="BrandDealsContentRepeat"><a href="" class="BrandDealsTitle"><?php echo $row_LatestDeals['ProductName']; ?></a><br /><?php echo $row_LatestDeals['ProductCartDesc']; ?>
<span id="AdditionalDealInfo">
<?php
if ($row_LatestDeals['ProductLimited']=="1") {
echo '<span id="greentext">'. $limitedtimeoofer .'</span>';}
else {
echo '<span id="additonalinfo">'. $dateexpire .' '. date("F d, Y", strtotime($row_LatestDeals['ProductExpirationDate'])) .' </span>';}
?>
<?php if($row_LatestDeals['ProductSavingsCode']!="") { echo '
<br>
<span id="redtext">
'. $couponcode .' '. $row_LatestDeals['ProductSavingsCode'] . '
</span>';}?>
</span>
</div>

<div id="BrandDealsRatingsContainer"><?php echo $totalRows_DealsRatings ?> </div>

<div id="BrandDealsButton">
<span id="BrandDealsSave">SAVE: <?php echo $row_LatestDeals['ProductPrice']; ?></span><br />
<?php if ($row_LatestDeals['ProductPay']!=""){ echo '<span id="BrandDealsPay">PAY: $'. $row_LatestDeals['ProductPay'] .'</span><br />';} else { echo '';}
?>
<?php
if ($row_LatestDeals['ProductShipping']=="1"){
echo '<span id="BrandDealsShipping">+ Free Shipping</span>';}
else {
echo '';}
?>
<img src="../images/body/redirect-button.png" />
</div>
</div>
<?php } while ($row_LatestDeals = mysql_fetch_assoc($LatestDeals)); ?>


and this is database structure (I only added only few rows):

ProductID
ProductName
ProductProvider
ProductBrand
ProductPay <---- Sort by this Price DESC or ASC
ProductShipping <---- This is Free Shipping Row
ProductExpirationDate <----- Sort by this date DESC or ASC


hope I left enough data lol so I can get help or maybe finished code :)
Thanks all in advance. Best Regards!

coopster

5:06 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You will need to capture the value of the GET request variables and check them for existence first, probably with isset [php.net]. Next, you would use the value in your query by adding an ORDER BY.