It is currently Sat Feb 04, 2012 3:35 pm

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Building a dynamic sql query with ORDER BY + Variables
PostPosted: Thu Mar 11, 2010 5:20 pm 
Offline

Joined: Thu Mar 11, 2010 5:08 pm
Posts: 5
hi, im building a search engine (ebay like) wich should work like this: you can display a list of products by selecting the category or typing a search keyword (i'll try to make it like you can search your keyword only inside that category later), and when result list is being shown, you can sort it by the field's name (name, brand, category and price) by clicking the fields header, doing so i get a variable with the fields name to put in my sql query on "order by $field" for example. First problem: it says i have an incorrect mysql syntax. Second problem: i need an initial 'order by' value, wich changes when i click a link specifying a name for the order by. If someone can help, i would be really glad. here's the sql query section.
Code:
$string=$_GET['string'];
$cat=$_GET['cat'];
$order=$_GET['order'];
$dir=$_GET['dir'];
$sql="SELECT * FROM `test` WHERE `name` LIKE '$string' OR `brand` LIKE '$string' OR `category` LIKE '$string' ORDER BY $order $dir";


the $order variable defines the column name wich the result will be sorted by, and the $dir variable dedfines DESC or ASC only. my syntax error is related to this, because if i change the
Code:
$order=$_GET['order'];
$dir=$_GET['dir'];
to
Code:
$order=id;
$dir=DESC;
the sql syntax works fine, as you can see in http://fael097.site11.com/test (obviously ordering result by column wont work)
thanks for any kind of help!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2010 7:24 pm 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
That easy to handle with the code

Code:
//default values for $order and $dir
$dir     = "asc";

$order = "name";

$string = "";  //empty default (show all within reason)

$cat     = ""; //no cat set yet

//basic sql to pull [all] records
$sql     = "SELECT * FROM test WHERE 1 ";

//get the values (note you should validate this data or someone will hack
//your site)
//(string) justed forces the data to be a string (this is not validation)
//the '%' indicates a wildcard search, so the below statements would match
//anything where the $string value is in the word, beginning, middle or end

if(!empty($_GET['string'])){
  $string=(string)$_GET['string'];   //search string
 
  $sql .= " AND name LIKE '%$string%' OR brand LIKE '%$string%' ";
}//end if

if(!empty($_GET['cat'])){
  $cat    =(string)$_GET['cat'];      //category
 
  $sql .= " OR category like '%$string%' ";


//check $dir first since it just needs to be a var to fit into the sql
if(!empty($_GET['dir'])){
  $dir     =(string)$_GET['dir'];          //dir of order by
}

if(!empty($_GET['order'])){
  $order =(string)$_GET['order'];   //order by

  $sql .= "ORDER BY $order $dir";
}



_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 11, 2010 11:03 pm 
Offline

Joined: Thu Mar 11, 2010 5:08 pm
Posts: 5
perfect! thanks much

just a few changes and you can see here:

http://fael097.site11.com/test


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 12, 2010 1:01 am 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6498
Location: toronto, canada
works nice!

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron