I'm creating a function that has to check for several inputs and create a variable that I use to call a SQL query, the problem I'm facing is that I want to change a variable so that I don't use the SELECT call every time, but instead the AND call. How does one construct such a function? This is what I currently have.
if ($_GET['filtering2'] != "" || $_GET['filtering3'] != "" || $_GET['filtering4'] != "" || $_GET['filtering5'] != "" || $_GET['filtering6'] != "" || $_GET['filtering7'] != "" || $_GET['filtering7'] != "") {
function searchByColumn($values, $columnName)
{
$string = implode(" OR season LIKE ", $values);
if ($firstTime = 2) {
return "SELECT * FROM shrubs2 WHERE season LIKE $string";
} else {
return " AND WHERE season LIKE $string";
}
}
$colNames = array("season"); // can add here more column names
foreach ($colNames as $colName) {
$str .= searchByColumn($array_name, $colName);
$firstTime = 3;
}
}
if ($_GET['filtering8'] != "" || $_GET['filtering9'] != "" || $_GET['filtering10'] != "" || $_GET['filtering11'] != "") {
function searchByColumn2($values, $columnName2)
{
$string2 = implode(" OR 日照 LIKE ", $values);
if ($firstTime = 2) {
return "SELECT * FROM shrubs2 WHERE 日照 LIKE $string2";
} else {
return " AND WHERE 日照 LIKE $string2";
}
}
$colNames2 = array("日照"); // can add here more column names
foreach ($colNames2 as $colName2) {
$str .= searchByColumn2($array_name2, $colName2);
$firstTime = 3;
}
}
I was trying to change the variable $firstTime, but it never resulted as I wished for it to do.
first edit:
if($_GET['filtering8'] != "" || $_GET['filtering9'] != "" || $_GET['filtering10'] != "" || $_GET['filtering11'] != "" ){
function searchByColumn2($values, $columnName2) {
$string2 = implode(" OR 日照 LIKE ", $values);
if($firstTime != 3){
return "SELECT * FROM shrubs2 WHERE 日照 LIKE $string2";
$firstTime = 3;
} else{
return " AND WHERE 日照 LIKE $string2";
}
}
$colNames2 = array("日照"); // can add here more column names
foreach($colNames2 as $colName2) {
$str .= searchByColumn2($array_name2, $colName2);
}
}
and of course I've added: global $firstTime;