Solved

php jump menu to sort asc or desc

Posted on 2013-02-06
6
593 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

825 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