duan198811 2019-04-12 10:49
浏览 99
已采纳

如果条件设置PHP变量

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;

  • 写回答

1条回答 默认 最新

  • douan7601 2019-04-12 10:56
    关注

    since the variable is global you need to define the global by adding

    global $firstTime;
    

    on first line of searchByColumn and searchByColumn2 function. it will refer to global variable $firstTime

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答