如果条件设置PHP变量

时间:2019-04-12 10:49:47

标签: php variables if-statement conditional-statements

我正在创建一个必须检查多个输入并创建用于调用SQL查询的变量的函数,我面临的问题是我想更改一个变量以免使用每次都执行SELECT调用,而是执行AND调用。如何构造这样的功能?这是我目前拥有的。

RUN echo -e "\
[mongodb]\n\
name=MongoDB Repository\n\
baseurl=https://repo.mongodb.org/yum/redhat/7Server/mongodb-org/3.2/x86_64/\n\
gpgcheck=0\n\
enabled=1\n" >> /etc/yum.repos.d/mongodb.repo

# Install mongodb
RUN yum update -y && yum install -y mongodb-org

# Set up directory requirements
RUN mkdir -p /data/db /var/log/mongodb /var/run/mongodb
VOLUME ["/data/db", "/var/log/mongodb"]

# Expose port 27017 from the container to the host
EXPOSE 27017

# Start mongodb
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--port", "27017", "--dbpath", "/data/db", "--pidfilepath", "/var/run/mongodb/mongod.pid"]

我试图更改变量$ firstTime,但从未如我所愿地产生。

第一次编辑:

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

}

当然,我还添加了:        全球$ firstTime;

1 个答案:

答案 0 :(得分:0)

由于变量是全局变量,因此您需要通过添加来定义全局变量

global $firstTime;

searchByColumnsearchByColumn2函数的第一行。它将引用全局变量$firstTime