Solved

php jump menu to sort asc or desc

Posted on 2013-02-06
6
589 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 42

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 42

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 42

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Creating and Managing Databases with phpMyAdmin in cPanel.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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

15 Experts available now in Live!

Get 1:1 Help Now