OLM3CA Posted September 14, 2006 Share Posted September 14, 2006 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 https://forums.phpfreaks.com/topic/20728-order-by-criteria/ Share on other sites More sharing options...
gerkintrigg Posted September 14, 2006 Share Posted September 14, 2006 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 https://forums.phpfreaks.com/topic/20728-order-by-criteria/#findComment-91727 Share on other sites More sharing options...
OLM3CA Posted September 14, 2006 Author Share Posted September 14, 2006 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 https://forums.phpfreaks.com/topic/20728-order-by-criteria/#findComment-91741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.