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
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
Share on other sites

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.