Jump to content

Archived

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

OLM3CA

order by criteria

Recommended Posts

hi  I am asking too many questions forgive me . (:

I want to order datas with a criteria selected by the user but I have mysql error.
Is this a right statement:

[code]$query="SELECT * FROM links WHERE catid=$m and status=1 order by $b $c LIMIT $a";
$result= mysql_db_query("yesterday",$query);[/code]

[code]        <select name="criteria">
  <option value="1" selected>by date</option>
  <option value="2">by reg no</option>
          </select>[/code]

[code]switch($_POST['criteria']) {

case '1' :
$b="date";
break;
case '2' :
$b="linkid";
break;
}[/code]

[code]switch($_POST['ordering']) {

case '1' :
$c="desc";
break;
case '2' :
$c="asc";
break;
}[/code]

Share this post


Link to post
Share on other sites
why not have a switch statement to ask the user what he wants and define the order by statement using that..? so have a drop-down box and then do a switch on it like this:
[code]switch ($_POST['order_by_box']){
case 1:
$order='category';
break;
case 2:
$order='name';
break;
}[/code]
then when it comes to doing the query:
[code]mysql_query("SELECT * FROM table WHERE a=b ORDER BY ".$order);[/code]

I think that'll work, but I've not tested it.

Share this post


Link to post
Share on other sites
hi I dont really understand,I couldnt do that.Can you please explain it more.

Is this correct with syntax ?

[code]$query="SELECT * FROM links WHERE catid=$m and status=1 order by $b $c LIMIT $a";
$result= mysql_db_query("yesterday",$query);[/code]

I mean can I use[color=red] order by $x  $y [/color] like this ???
first one is date id etc... second one is asc,desc

Share this post


Link to post
Share on other sites

×

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.