Jump to content

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));

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.