jfarthing Posted December 19, 2007 Share Posted December 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/ Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 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.... Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419021 Share on other sites More sharing options...
teng84 Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419025 Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419030 Share on other sites More sharing options...
teng84 Posted December 19, 2007 Share Posted December 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419035 Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 Edit: Bleh way off topic Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419040 Share on other sites More sharing options...
jfarthing Posted December 19, 2007 Author Share Posted December 19, 2007 Seems to work good thanks, but how about transferring that data to the next page of results...? Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419047 Share on other sites More sharing options...
corbin Posted December 19, 2007 Share Posted December 19, 2007 Just add order={$order}&ad={$ad} to the links with the pages.... (Well, $ad would need to be converted back to 0/1 before being put in the link) Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419048 Share on other sites More sharing options...
jfarthing Posted December 20, 2007 Author Share Posted December 20, 2007 Nice...works perfect! Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/82416-solved-phpmysql-order-data-dynamically/#findComment-419060 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.