Jump to content


Photo

order by criteria


  • Please log in to reply
2 replies to this topic

#1 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 14 September 2006 - 01:25 PM

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:

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

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

switch($_POST['criteria']) {
		
			case '1' :
			$b="date";
			break;
			case '2' :
			$b="linkid";
			break;
}

switch($_POST['ordering']) {
		
			case '1' :
			$c="desc";
			break;
			case '2' :
			$c="asc";
			break;
}

AMD rather than Intel , PHP rahter than ASP

#2 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 14 September 2006 - 01:34 PM

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:
switch ($_POST['order_by_box']){
case 1:
$order='category';
break;
case 2:
$order='name';
break;
}
then when it comes to doing the query:
mysql_query("SELECT * FROM table WHERE a=b ORDER BY ".$order);

I think that'll work, but I've not tested it.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#3 OLM3CA

OLM3CA
  • Members
  • PipPipPip
  • Advanced Member
  • 52 posts
  • LocationCyprus

Posted 14 September 2006 - 01:55 PM

hi I dont really understand,I couldnt do that.Can you please explain it more.

Is this correct with syntax ?

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

I mean can I use order by $x  $y like this ???
first one is date id etc... second one is asc,desc
AMD rather than Intel , PHP rahter than ASP




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users