Jump to content

Recommended Posts

I want to allow my users to choose dynamically how data is displayed from MySQL.  I will have the traditional table headers, and I want them to be able to click the header to sort say by name ascending, then click name again and it sorts by name descending, or click by date and it goes by date ascending, click date again it goes to date descending...catch my drift?  how do I accomplish this?

Link to comment
https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/
Share on other sites

This isn't the most effecient way, but I'm lazy and it's the method with the least coding I've come across:

 

$orders = array('field1', 'field2');

$order = (isset($_GET['order']) && ctype_digit($_GET['order']) && isset($orders[$_GET['order']]) ? $_GET['order'] : 0;

$ad = (isset($_GET['ad']) && $_GET['ad'] == 1) ? 'ASC' : 'DESC'; //default order DESC

$q = "SELECT * FROM table ORDER BY {$orders[$order]} {$ad}";

 

?order=0&ad=1 would order by field1 asc, or ?order=1&ad=0 would order by field2 DESC....

corbin you post so fast ... i get beaten for i guess 4 times now haha and wait you use ctype data hehe.

 

anyways in addition to have your query a bit better and easier add where 1=1 so you only have to think of the and  / or condition your going to ad

Yeah, I like ctype ;p.  It's better than is_numeric for stuff like that since is_numeric allows decimals...

 

Hrmmm....  I don't like the "where 1=1" it just looks... bleh to me....  Just of curiosity though, is it faster with or without the 1 = 1?

 

Anyway, to make auto toggling links, this would probably be the easiest way, although it's not the best performance wise.

 


function MakeLink($order) {
$ad = (isset($_GET['ad']) && $_GET['ad'] == 1) ? 1 : 0;
$a = (isset($_GET['order']) && $_GET['order'] == $order && $ad == 0) ? 1 : 0;
return '?order=' . $order . '&ad=' $a;
}

echo '<a href="'.MakeLink(0).'">Field1</a>';
echo '<a href="'.MakeLink(0).'">Field2</a>';

Yeah, I like ctype ;p.  It's better than is_numeric for stuff like that since is_numeric allows decimals...

 

Hrmmm....  I don't like the "where 1=1" it just looks... bleh to me....  Just of curiosity though, is it faster with or without the 1 = 1?

 

Anyway, to make auto toggling links, this would probably be the easiest way, although it's not the best performance wise.

 


function MakeLink($order) {
$ad = (isset($_GET['ad']) && $_GET['ad'] == 1) ? 1 : 0;
$a = (isset($_GET['order']) && $_GET['order'] == $order && $ad == 0) ? 1 : 0;
return '?order=' . $order . '&ad=' $a;
}

echo '<a href="'.MakeLink(0).'">Field1</a>';
echo '<a href="'.MakeLink(0).'">Field2</a>';

your defending your is numeric and regex yesterday right ?hehe anyways

 

Hrmmm....  I don't like the "where 1=1" it just looks... bleh to me....

 

it should not look bleh to you.. why?

if ever you run the query and no field where availbale you dont need to add a condition saying that

if (field == etc..){

$codntion.='where';

}

 

unlike if you have where 1=1 all you need to think is the and condition and etc.. you must know that you put where clause in the very first contion of your query

 

hope you get my poit I cant explain better my english is bad any waht its your choice  ;)

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.