Solved

php jump menu to sort asc or desc

Posted on 2013-02-06
6
595 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
[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
  • 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…

740 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