Jump to content

sorting by column


mnymkr

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.