[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

php jump menu to sort asc or desc

Posted on 2013-02-06
6
Medium Priority
?
609 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 44

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 44

Accepted Solution

by:
Chris Stanyon earned 2000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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 44

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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.
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
The viewer will learn how to dynamically set the form action using jQuery.
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

650 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