cash-cash
cash-cash

Reputation: 75

PHP & MYSQL: How can i neglect empty variables from select

if i have 4 variables and i want to select DISTINCT values form data base

<?php
$var1 = ""; //this variable can be blank
$var2 = ""; //this variable can be blank
$var3 = ""; //this variable can be blank
$var4 = ""; //this variable can be blank

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE **keywords ='$var1' OR author='$var2' OR date='$var3' OR forums='$var4'** ");

?>

note: some or all variables ($var1,$var2,$var3,$var4) can be empty

what i want: i want to neglect empty fields

lets say that $var1 (keywords) is empty it will select all empty fileds, but i want if $var1 is empty the result will be like

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE author='$var2' OR date='$var3' OR forums='$var4' ");

if $var2 is empty the result will be like

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE keywords ='$var1' OR date='$var3' OR forums='$var4' ");

if $var1 and $var2 are empty the result will be like

$result = mysql_query("SELECT DISTINCT title,description FROM table WHERE date='$var3' OR forums='$var4' ");

and so on

Upvotes: 0

Views: 1748

Answers (6)

cash-cash
cash-cash

Reputation: 75

Thanks alot every one specially experimentX .. Your answer helped me to get the right function i Just replaced (isset) with (!empty) .. Then every thing will be more than OK

$vars = array(
        (!empty($_GET["var1"]))? " keyword = '". $_GET["var1"] ."' ": null, 
        (!empty($_GET["var2"]))? " author  = '". $_GET["var2"] ."' ": null,
        (!empty($_GET["var3"]))? " date    = '". $_GET["var3"] ."' ": null,
        (!empty($_GET["var4"]))? " forums  = '". $_GET["var4"] ."' ": null
    );


function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" OR ", $newvars);

$sql = "SELECT DISTINCT title, description FROM table ".(($where)?"WHERE ".$where: null);

echo $sql;

with this function if there is empty variable it will be neglected

Thanks again every one for your helpful suggestion

Upvotes: 1

S L
S L

Reputation: 14328

Well, there are manu ways of doing this but the shortest way I have found is creating an array of the following form

$vars = array(
        (isset($_GET["var1"]))? " keyword = '". $_GET["var1"] ."' ": null, 
        (isset($_GET["var2"]))? " author  = '". $_GET["var2"] ."' ": null,
        (isset($_GET["var3"]))? " date    = '". $_GET["var3"] ."' ": null,
        (isset($_GET["var4"]))? " forums  = '". $_GET["var4"] ."' ": null
    );

function myfilterarray($var)
{
    return !empty($var)?$var: null;
}

$newvars = array_filter($vars, 'myfilterarray');

$where = join(" OR ", $newvars);

$sql = "SELECT DISTINCT title, description FROM table ".(($where)?"WHERE ".$where: null);

echo $sql;

Your result for http://localhost/?var1=sadfsadf&var2=sadfasdf&var3=asdfasdf

 SELECT DISTINCT title, description FROM table WHERE keyword = 
 'sadfsadf' OR author = 'sadfasdf' OR date = 'asdfasdf' 

Your result for http://localhost/?

SELECT DISTINCT title, description FROM table 

Upvotes: 0

Philoxopher
Philoxopher

Reputation: 1674

(I just posted the below in his duplicate post, so I'm re-posting the below here)

Forgive me if anything is wrong, it's very late here and I just typed this in notepad on Windows, without an environment to test on. * Use with caution * :)

$vars = array(
'blah1' => '',
'blah2' => '',
'blah3' => '',
);

 $sql_statement = "SELECT first, last FROM names WHERE";

 $clause = "";
 foreach($vars as $k=$v)
{
$k = trim($k);
if(!empty($k))
{
    $clause .= " `$k` = '$v' OR";
}
}
$clause = rtrim($clause, "OR");

// $clause should have what you want.

Upvotes: 0

Dimitry
Dimitry

Reputation: 6613

Try this.


$vars = array(
  'keywords' => '', // instead of var1
  'author' => '',  // instead of var2
  'date' => '', // instead of var3
  'forums' => '', // instead of var4
);

$where = array();
foreach ($vars as $varname => $varvalue) {
  if (trim($varvalue) != '') $where[] = "`$varname` = '" . mysql_real_escape_string($varvalue) . "'";
}
$result = mysql_query("SELECT DISTINCT title, description FROM table WHERE " . join(" OR ", $where));

Upvotes: 4

Jerome
Jerome

Reputation: 275

Why do you not simply build a if else structure? Like

if ($var1!="" && $var2!="" && $var3!="" && $var4!=""){
   $result = mysql_query("SELECT DISTINCT title,description FROM table WHERE keywords ='$var1' OR author='$var2' OR date='$var3' OR forums='$var4' ")
} else if ($var2!="" && $var3!="" && $var4!=""){
   $result = mysql_query("SELECT DISTINCT title,description FROM table WHERE author='$var2' OR date='$var3' OR forums='$var4' ");
} else if {
...
}

Upvotes: 0

Matt
Matt

Reputation: 1265

make your select statement string before you call mysql_query(...) so do something along the lines of this:

$queryString = "Select DISTINCT title, description FROM table WHERE";
if(!empty($var1))
   $queryString .= " keywords = $var1";

and so forth for all of your variables. you could also implement a for loop and loop through your $var1 - $var# and check for !empty($var#)

Upvotes: 0

Related Questions