Solved

php jump menu to sort asc or desc

Posted on 2013-02-06
6
591 Views
Last Modified: 2013-02-11
I have a list of records currently sorted by date. If I wanted a jump menu so that I can choose to sort by date asc or desc, how would I do it? I have tried something like this but it does nothing unfortunately.

<select name="jumpMenu" id="jumpMenu">
              <option value="date_filter.php?$_GET['proddate']=asc">Date asc to desc</option>
            </select>

Open in new window

0
Comment
Question by:jonofat
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 38859674
Here's a general overview.

You need to pass in a variable to your php script indicating ASC or DESC, and then build that into your database query. Your select would need to look something like:

<select name="sortOrder">
<option value="asc">Date Asc to Desc</option>
<option value="desc">Date Desc to Asc</option>
</select>

You would then submit this to your script (javascript / submit button etc) and then in PHP you would have a variable called sortOrder as part of either the GET array or the POST array, depending on how you submit.

You then build and execute your query accordingly:

$qry = sprintf("select * from table order by someField %s", $_GET['sortOrder']);
0
 

Author Comment

by:jonofat
ID: 38875154
Sorry for the delay in getting back to you. I am still struggling a bit with this. I have a recordset that is displaying date when you load the page. It is called rsCons.

Now, I have a jump menu as you have described that changes the url to this for example:

mypage.php?proddate=DESC

So, I created a new recordset with a query that looks like this:

SELECT * FROM consumption WHERE proddate = %s ORDER BY proddate DESC

My confusion is that the original data is showing from rsCons yet I created a new recordset for the query. So, I don't really know how to get them to work together.
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 38875575
Assuming you're using the same page to display the data, you'll need some kind of check to see whether the data should be displayed ASC or DESC. According to your previous post, you set the order to DESC by adding a querystring to the original URL, so your code in mypage.php should look something like this:

$sortorder = "ASC"; //set a default value.
if (isset($_GET['proddate']) && $_GET['proddate'] == "DESC") { $sortorder = "DESC"; } //check the querystring
$myQuery = sprintf("SELECT * FROM consumption ORDER BY proddate %s", $sortorder); //create the query

//now run your query - $myQuery

Open in new window

This code sets a default sort order (ASC). It then checks to see if the URL has a querystring containing a variable called proddate with a value of DESC. If it does, then it sets the order to DESC, and finally it creates the query with the appropriate ORDER BY clause.

Also, the query in your previous post looks odd. Your are trying to pass the variable into the WHERE clause (WHERE proddate = %s), rather than the ORDER BY clause (ORDER BY proddate %s)!
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

Author Comment

by:jonofat
ID: 38875602
Awesome! That works! :) I will give you points.

Just a quick one though, is it possible to add other criteria? Like what if I wanted to to also be able to be sorted alphabetically or from lowest price to highest price?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 38875629
You can pass in as many parameters as you like through the querystring and build your sql query accordingly. For example, you could set your querystring to pass in the field and sort order, such as:

mypage.php?sortfield=price&sortorder=ASC
mypage.php?sortfield=company&sortorder=DESC
mypage.pgp?sortfield=lastname&sortorder=ASC

You would then build your query like so:

$field = $_GET['sortfield'];
$order = $_GET['sortorder'];
$myQuery = sprintf("SELECT * FROM yourTable ORDER BY %s %s",$field, $order);

You'd need to add some additional logic so the query made sense, such as making sure a value was passed in for both parameters etc. Just remember, that it's very easy for a user to change the querystring in GET requests just by typing in a different URL, so you'll need to make it bomb proof!
0
 

Author Comment

by:jonofat
ID: 38875637
Awesome! Thanks for all your help :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now