Jump to content

checking if WHERE is already in query


stijn0713

Recommended Posts

I don't know precisely how to put it so i will give an example:

 

suppose you have a search form with input fields for:

 

-an age range,

-a data range,

-for the sex,

-postal code

 

as soon as the user would fill in one of these input fields, a query should be generated including a condition doing something with the inputted value.

 

For example, the user fills in maxAge, the the query should say: SELECT * FROM table WHERE age >= maxAge;

 

Filling more than one criterion then would do .= " AND criteria (condition) inputvalue " etc...

 

My question comes to the part about: how to check if there is already a WHERE in the clause and how to put WHERE or AND for every criterion.

 

Since doing like above, i won't be able to search for another criterion if i don't select maxAge first because then there would be no WHERE in the sql clause.

 

Hopefully i've put it clear

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/259011-checking-if-where-is-already-in-query/
Share on other sites

Put all your conditions into an array then implode it with whatever operator you need (AND or OR)

 

$where = array();
if ($blah){  $where[] = ' field >= whatever '; }
if ($bleh){ $where[] = ' this=that';}
if ($blargh){ $where[] = ' thisway!=thatway '; }

$sql = 'select ...';
if (count($where) > 0){
   $sql .= 'WHERE '.implode(' AND ', $where);
}

 

Damnit, I just typed 20 lines and you beat me to it kicken.

 

What's worse is he was able to answer the question in only 9 lines of code (one of which is blank), whereas you needed 20. :)

 

I jest of course - no ill intent meant.

 

But his was more or less pseudo code, and he collapsed the conditionals. :P

Put all your conditions into an array then implode it with whatever operator you need (AND or OR)

 

$where = array();
if ($blah){  $where[] = ' field >= whatever '; }
if ($bleh){ $where[] = ' this=that';}
if ($blargh){ $where[] = ' thisway!=thatway '; }

$sql = 'select ...';
if (count($where) > 0){
   $sql .= 'WHERE '.implode(' AND ', $where);
}

 

 

What if i sometimes need a OR operator and sometimes a AND or whatever other SQL syntax?

May not be the best solution, but this is what I came up with off the top of my head and before my coffee.

$where = '';

$age        = $_POST['age'];
$date       = $_POST['date'];
$sex        = $_POST['sex'];
$postalcode = $_POST['postalcode'];

if ($age != '') {
$where .= " AND age='$age'";
}

if ($date != '') {
$where .= " OR date='$date'";
}

if ($sex != '') {
$where .= " AND sex='$sex'";
}

if ($postalcode != '') {
$where .= " AND postalcode='$postalcode'";
}

$where = preg_replace('/^AND|OR/i', '', trim($where));

Archived

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

×
×
  • 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.