question about sorting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

question about sorting

Post by Milan »

I have the following :

Code: Select all

mysql_select_db($database_pickageek, $pickageek);
$query_projects = sprintf("SELECT Projectname, Description, Phase, Pview, timeposted, ownerid, category, pricemin, pricemax, dateposted, ID FROM projects WHERE category = '%s'  ORDER BY timeposted ASC", $colname_projects);
$query_limit_projects = sprintf("%s LIMIT %d, %d", $query_projects, $startRow_projects, $maxRows_projects);
$projects = mysql_query($query_limit_projects, $pickageek) or die(mysql_error());
$row_projects = mysql_fetch_assoc($projects);
and i have links Sort by [ TIME POSTED ] [ PROJECT NAME ] [ PAGE VIEWS] [ OWNER ] [ MAX PRICE]

can i change "ORDER BY timeposted" with a variable? What i want is to allow users to choose how to sort their results.

thanks!
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Your using double quotes, so just use variable interpolation

Code: Select all

$col_name = 'timeposted';
$sql_query = "SELECT * FROM my_table ORDER BY $col_name ASC";
Just make sure you secure the variable...make sure it's a column name, etc...

Cheers :)
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

i will gibe it a try right away!
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

this is what i get


Parse error: parse error, unexpected T_VARIABLE
Milan
Forum Commoner
Posts: 97
Joined: Wed May 17, 2006 6:08 pm

Post by Milan »

sorry i am an idiot!

i forgot the ;

:roll:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

it happens :)
Post Reply