Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Recordset Sort Problem

Posted on 2009-07-05
5
Medium Priority
?
254 Views
Last Modified: 2013-12-13
Hi.  I am trying to sort a recordset.  I had a friend work on some javascript and while I think we've made some progress it still isn't working.  I am trying to sort by price - ASC or DESC or discount - ASC or DESC.  I would like to keep all the options in one drop down box due to space constraints on my page.  Please note that the recordset query was created by Dreamweaver and there is a repeat-region at work here as well.

Notice that I am getting the Order By variables in the recordset (order and sort) but not even sure if I need this.

The form action needs to maintain the original variable parameters of category and subcategory (this works) and then apply the sort (this is not working).

I only included the code that I think is applicable - please let me know if you need more information.   Thank you for your help; this newbie has been working at this for over a week now and any help is greatly appreciated.

I
The Recordset Query:
 
mysql_select_db($database_golena_IDBdb, $golena_IDBdb);
$query_rsProductsDiscount = sprintf("SELECT ((IDBdb.retail_price-IDBdb.price)/IDBdb.retail_price) AS discount, IDBdb.name, IDBdb.shortDescription, IDBdb.merchant_display, IDBdb.retail_price, IDBdb.price, IDBdb.url, IDBdb.thumbnail, IDBdb.productID, IDBdb.category, IDBdb.category_display, IDBdb.subcategory, IDBdb.subcategory_display, IDBdb.merchant FROM IDBdb WHERE (((IDBdb.subcategory)='%s')) ORDER BY %s %s", $colname2_rsProductsDiscount,$orderby_rsProductsDiscount,$sortby_rsProductsDiscount);
$query_limit_rsProductsDiscount = sprintf("%s LIMIT %d, %d", $query_rsProductsDiscount, $startRow_rsProductsDiscount, $maxRows_rsProductsDiscount);
$rsProductsDiscount = mysql_query($query_limit_rsProductsDiscount, $golena_IDBdb) or die(mysql_error());
$row_rsProductsDiscount = mysql_fetch_assoc($rsProductsDiscount);
 
if (isset($_GET['totalRows_rsProductsDiscount'])) {
  $totalRows_rsProductsDiscount = $_GET['totalRows_rsProductsDiscount'];
} else {
  $all_rsProductsDiscount = mysql_query($query_rsProductsDiscount);
  $totalRows_rsProductsDiscount = mysql_num_rows($all_rsProductsDiscount);
}
$totalPages_rsProductsDiscount = ceil($totalRows_rsProductsDiscount/$maxRows_rsProductsDiscount)-1;
 
The Form:
 
<form name="SortForm" method="get" action="">
                                              <select name="Order By">
											 
                                                <option value selected="Price ASC">Sale Price - Low to High
                                                </option>
                                                <option value="Price DESC">Sale Price - High
                                                to Low</option>
                                                <option value="Discount ASC">Discount Low
                                                to High</option>
                                                <option value="Discount DESC">Discount -
                                                High to Low</option>
                                              </select>
					 <input type="hidden" name="category" value="<?php echo htmlentities($_GET['category']) ?>">
					 <input type="hidden" name="subcategory" value="<?php echo htmlentities($_GET['subcategory']) ?>">                                              
					<input type="submit" name="Submit" value="go">
                                            </form>

Open in new window

0
Comment
Question by:golena
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 24781208
immediately after:

$query_limit_rsProductsDiscount = sprintf(....);
 
put:
echo "<pre>\n{$query_limit_rsProductsDiscount}</pre>";
 
what does it print? What you posted does not reveal much about:
$colname2_rsProductsDiscount
$orderby_rsProductsDiscount
$sortby_rsProductsDiscount

Open in new window

0
 

Author Comment

by:golena
ID: 24781255
It prints:

SELECT ((IDBdb.retail_price-IDBdb.price)/IDBdb.retail_price) AS discount, IDBdb.name, IDBdb.shortDescription, IDBdb.merchant_display, IDBdb.retail_price, IDBdb.price, IDBdb.url, IDBdb.thumbnail, IDBdb.productID, IDBdb.category, IDBdb.category_display, IDBdb.subcategory, IDBdb.subcategory_display, IDBdb.merchant FROM IDBdb WHERE (((IDBdb.subcategory)='Outdoor-Patio-Cushions')) ORDER BY price ASC LIMIT 0, 10

Also, here are the two queries for order and sort:

$sortby_rsProductsDiscount = "ASC";
if (isset($_GET['sort'])) {
  $sortby_rsProductsDiscount = (get_magic_quotes_gpc()) ? $_GET['sort'] : addslashes($_GET['sort']);
}

$orderby_rsProductsDiscount = "price";
if (isset($_GET['order'])) {
  $orderby_rsProductsDiscount = (get_magic_quotes_gpc()) ? $_GET['order'] : addslashes($_GET['order']);
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 24781375
>>Also, here are the two queries for order and sort:
But  that does not make use of the select list to do the ordering, which is what you want.

On your HTML form, this is NOT correct:
<option value selected="Price ASC">Sale Price - Low to High</option>
 
it should be:
<option selected value="Price ASC">Sale Price - Low to High</option>
 
Also, rename your select to:
 <select name="OrderBy">
 
Notice that it does not have a space.  Then check if OrderBy exists and  is so, use that instead:
 
The Recordset Query:
 
mysql_select_db($database_golena_IDBdb, $golena_IDBdb);
if( isset($_REQUEST['OrderBy']) && !empty($_REQUEST['OrderBy']) )
	$query_rsProductsDiscount = sprintf("SELECT ((IDBdb.retail_price-IDBdb.price)/IDBdb.retail_price) AS discount, IDBdb.name, IDBdb.shortDescription, IDBdb.merchant_display, IDBdb.retail_price, IDBdb.price, IDBdb.url, IDBdb.thumbnail, IDBdb.productID, IDBdb.category, IDBdb.category_display, IDBdb.subcategory, IDBdb.subcategory_display, IDBdb.merchant FROM IDBdb WHERE (((IDBdb.subcategory)='%s')) ORDER BY %s", $colname2_rsProductsDiscount,$_REQUEST['OrderBy']);
else
	$query_rsProductsDiscount = sprintf("SELECT ((IDBdb.retail_price-IDBdb.price)/IDBdb.retail_price) AS discount, IDBdb.name, IDBdb.shortDescription, IDBdb.merchant_display, IDBdb.retail_price, IDBdb.price, IDBdb.url, IDBdb.thumbnail, IDBdb.productID, IDBdb.category, IDBdb.category_display, IDBdb.subcategory, IDBdb.subcategory_display, IDBdb.merchant FROM IDBdb WHERE (((IDBdb.subcategory)='%s')) ORDER BY %s %s", $colname2_rsProductsDiscount,$orderby_rsProductsDiscount,$sortby_rsProductsDiscount);
 
$query_limit_rsProductsDiscount = sprintf("%s LIMIT %d, %d", $query_rsProductsDiscount, $startRow_rsProductsDiscount, $maxRows_rsProductsDiscount);
$rsProductsDiscount = mysql_query($query_limit_rsProductsDiscount, $golena_IDBdb) or die(mysql_error());
$row_rsProductsDiscount = mysql_fetch_assoc($rsProductsDiscount);
 
if (isset($_GET['totalRows_rsProductsDiscount'])) {
  $totalRows_rsProductsDiscount = $_GET['totalRows_rsProductsDiscount'];
} else {
  $all_rsProductsDiscount = mysql_query($query_rsProductsDiscount);
  $totalRows_rsProductsDiscount = mysql_num_rows($all_rsProductsDiscount);
}
$totalPages_rsProductsDiscount = ceil($totalRows_rsProductsDiscount/$maxRows_rsProductsDiscount)-1;
 
The Form:
 
<form name="SortForm" method="get" action="">
                                              <select name="OrderBy">
                                                <option selected value="Price ASC">Sale Price - Low to High</option>
                                                <option value="Price DESC">Sale Price - High to Low</option>
                                                <option value="Discount ASC">Discount Low to High</option>
                                                <option value="Discount DESC">Discount - High to Low</option>
                                              </select>
                                         <input type="hidden" name="category" value="<?php echo htmlentities($_GET['category']) ?>">
                                         <input type="hidden" name="subcategory" value="<?php echo htmlentities($_GET['subcategory']) ?>">                                              
                                        <input type="submit" name="Submit" value="go">
                                            </form>

Open in new window

0
 

Author Closing Comment

by:golena
ID: 31599945
Excellent solution - thank you!!!!!!!
0
 
LVL 82

Expert Comment

by:hielo
ID: 24781594
You are welcome!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question