Page 1 of 1

question about sorting

Posted: Fri May 26, 2006 1:51 pm
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!

Posted: Fri May 26, 2006 1:59 pm
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 :)

Posted: Fri May 26, 2006 2:03 pm
by Milan
i will gibe it a try right away!

Posted: Fri May 26, 2006 2:36 pm
by Milan
this is what i get


Parse error: parse error, unexpected T_VARIABLE

Posted: Fri May 26, 2006 2:38 pm
by Milan
sorry i am an idiot!

i forgot the ;

:roll:

Posted: Fri May 26, 2006 2:50 pm
by alex.barylski
it happens :)