Jump to content

order by criteria


OLM3CA

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]
Link to comment
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.
Link to comment
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
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.