How do you sort your MySQL results according to column name?
Hi,
I normally output the results of a mysql query according to ID. I display the results in tables.
Example:
PHP Code:
<?php
// file.php
if(isset($GET['op'])) { switch($op) { case "sort_ID"; $query = "SELECT ID,name,surname,date FROM table ORDER BY ID"; break; case "sort_name"; $query = "SELECT ID,name,surname,date FROM table ORDER BY name"; break; case "sort_surname"; $query = "SELECT ID,name,surname,date FROM table ORDER BY surname"; break; case "sort_date"; $query = "SELECT ID,name,surname,date FROM table ORDER BY date"; break; } } else { // use default sorting $query = "SELECT ID,name,surname,date FROM table"; }
When this table is displayed, I would like to sort the results according to name, surname or date when clicked on the column name. When clicked again, I want to sort them upside down (DESC).
What I did may work (except the reversal sorting) but what if I had many columns? Do I need to write so many lines of code?
f(isset($GET['op'])) { $op = $_GET['op']; $query = "SELECT ID,name,surname,date FROM table ORDER BY ".$op; }
HTH
Dan
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
$query = 'SELECT '.implode(', ', $sortColumns).' FROM table ORDER BY '.$sortColumns[$_GET['op']].' '.$sortOrder[$_GET['ord']];
$fileName = getenv('SCRIPT_NAME');
include 'db.php';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
echo '<tr>';
foreach($sortColumns as $column)
{
echo '<td>',$row[$column],'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
dumpfi
__________________
"Failure is not an option. It comes bundled with the software."
....../)/)..(\__/).(\(\................../)_/)......
.....(-.-).(='.'=).(-.-)................(o.O)...../<)
....(.).(.)("}_("}(.)(.)...............(.)_(.))¯/.
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.
What if I do a request to "file.php?op=ID;DROP DATABASE"? I can execute any SQL command with your solution.
then you would just wrap the GET in mysql_real_escape_string to prevent that wouldn't you?
And more to the point you would not have such permissions for a user over the web, you would create a secure user with only select, insert and update privileges and none other.
I'm looking to possibly do the same as above, and like the code posted by dumpfi to create the sortable table.
But can someone maybe help explain/show how you can possibly expand the sortable columns to include additional columns that do not officially exist in your database. I'd like to include columns/fields that are being created during the actual sql query such as:
if(isset($GET['op'])) {
switch($op) {
case "sort_ID";
$query = "SELECT ID,name,surname,date FROM table ORDER BY ID";
break;
case "sort_name";
$query = "SELECT ID,name,surname,date FROM table ORDER BY name";
break;
case "sort_surname";
$query = "SELECT ID,name,surname,date FROM table ORDER BY surname";
break;
case "sort_date";
$query = "SELECT ID,name,surname,date FROM table ORDER BY date";
break;
}
} else { // use default sorting
$query = "SELECT ID,name,surname,date FROM table";
}
could be be written as: (just change the $_GET["op"] values from sort_ID,sort_name,etc to ID,name,etc)
PHP Code:
$query = "SELECT ID,name,surname,date FROM table";
if (isset($_GET["op"]))
$query .= " ORDER BY " . $_GET["op"];
Secondly, to make them order in the opposite direction add " DESC" to the end of your query, so "SELECT * FROM table ORDER BY col" becomes "SELECT * FROM table ORDER BY col DESC"
Thirdly, if you'd like to sort by multiple rows just add a comma. For example "SELECT * FROM tables ORDER BY col1,col2 DESC,col3" would order first by col1 then by col2 (descending) then by col3
Hopefully all that can help you get your code working more efficiently.
You probably should, so you can add something useful to the topic, rather than confuse the user by making them think that this is something new. It also saves you time.
You probably should, so you can add something useful to the topic, rather than confuse the user by making them think that this is something new. It also saves you time.