mnymkr Posted May 8, 2007 Share Posted May 8, 2007 I have read a ton of stuff on this and just can't get my brain around it. I am pulling two columns from MySQL by using post on a form. The result look something like this carat color cut 1.00yellowround 2.00clearemerald how can I make the headers be links that when click resorts the table by that column? can I do this with POST? Link to comment https://forums.phpfreaks.com/topic/50518-sorting-by-column/ Share on other sites More sharing options...
mmarif4u Posted May 8, 2007 Share Posted May 8, 2007 Do u have code, please post it we will try to help u out. Link to comment https://forums.phpfreaks.com/topic/50518-sorting-by-column/#findComment-248184 Share on other sites More sharing options...
hackerkts Posted May 8, 2007 Share Posted May 8, 2007 Do it using $_GET, at the end of the query put ORDER by `$_GET['column_name']`. Then on the headers put $_GET['column_name'], that's just rough idea. Link to comment https://forums.phpfreaks.com/topic/50518-sorting-by-column/#findComment-248185 Share on other sites More sharing options...
cmgmyr Posted May 8, 2007 Share Posted May 8, 2007 SELECT * FROM gems ORDER BY carat ASC Link to comment https://forums.phpfreaks.com/topic/50518-sorting-by-column/#findComment-248189 Share on other sites More sharing options...
emehrkay Posted May 8, 2007 Share Posted May 8, 2007 you can do it with get. you'll have to keep track of the order in the page so before you write the url do $order = ($order == 'DESC') ? 'DESC' : 'ASC'; just make the link say something like page.php?sort=color&order=$order and your select statement should look somethign like "SELECT ... WHERE ... ORDER BY". $_GET['sort'] ." ". $_GET['order']; that should work, but that is the short version if you take variables directly from the $_GET superglobal/querystring, you'll be wide open for all types of sql injection attacks so do some error checking before you run the query switch($_GET['sort']){ case 'color': $sort = 'color'; break; case 'cut': $sort = 'cut'; break; deafult: $error = true; } do that type thing again for each $_GET var and before you run the query check for an error if($error){ //do nothing or display error }else{ //run query } i hope this helps, its pretty simple and was written in 2 mins Link to comment https://forums.phpfreaks.com/topic/50518-sorting-by-column/#findComment-248192 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.