|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sorting By Arrays?
I'm total new at Arrays, I have did my all night search, and nothing seemed to answer my question.
So, Here it goes. How would I change ORDER BY in a query from a simple URL, for an option to sort by ? PHP Code:
My guess of how to call this from the URL? I knew it would not work, but I tried this. <a href="sample.php?$sortBy=1" title="Sort By: ID">ID</a> <a href="sample.php?$sortBy=2" title="Sort By: Date">Date</a> <a href="sample.php?$sortBy=3" title="Sort By: Title">Title</a> As, You can see, I'm totally lost. ![]() |
|
#2
|
|||
|
|||
|
I just reorganized your code a little and cleaned up some things:
This should work. Make sure you have error_reporting set to E_ALL so you can catch any syntax errors. Sort Links: Code:
<a href="sample.php?s=1">ID</a> <a href="sample.php?s=2">Date</a> <a href="sample.php?s=3">Title</a> Sort Code: Code:
$defaultSortMethod = 'id'; $sort_by = array ( "1" => "id", "2" => "date", "3" => "title" ); $sortBy = isset($sort_by[$_GET['s']]) ? $sort_by[$_GET['s']] : $defaultSortMethod; // dB Connection $connection = mysql_connect($hostType, ect... // Query for info $query = "SELECT * FROM $tableName WHERE id = '$id' ORDER BY $sortBy ";
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. |
|
#3
|
|||
|
|||
|
Well that was simple enough, Thank you so much.
Works Perfect! Now I see what is going on. Thanks Again laidBak! |
|
#4
|
|||
|
|||
|
No problem... just here to help.
Also note that you could have used a switch/case statement to accomplish the same thing. Also, you might want to read up on the $variable = true/false ? true : false statements in the php manual This is called the ternary conditional operator REF: http://www.php.net/manual/en/language.expressions.php |
|
#5
|
|||
|
|||
|
Very interesting, I will look more into that.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Sorting By Arrays? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|