从php中的复选框选择中编写多个条件的较短方式

时间:2015-04-06 02:55:56

标签: php mysql checkbox

我将信息从html表单发送到php页面,我在这里检查mysql数据库的信息。

现在有5个复选框以及*(搜索栏,单选按钮等)*形式的其他变量。

有没有办法编写条件而不必为每个路径都有特定的if语句?否则我必须编写每个特定的路径,这就是很多输入:/

现在看起来像是:

if($orderBy == "price")
                {
                    if($searchBy == "begin")
                    {
                       if($_POST["gameType"] == "RTS")
                      {
                        $sql = "select * from gametbl where gme_title like '$title%' and where gme_type = 'RTS' ORDER BY gme_price DESC";
                      }
                    }

并且对于所有条件,这将会持续很长时间。有没有更好的方法呢?

1 个答案:

答案 0 :(得分:0)

这基本上就是我做一个方法链的意思。您可以在每个方法中指定在将特定变量输入其中时要执行的操作。它将基于单个值动态编写语句。这是一系列基于您的唯一代码的猜测:

<?php
    class SQLBuilder
        {
            protected   $order;
            protected   $sql;
            public      $statement;

            public  function Select($columns = false)
                {
                    $this->sql[]    =   "select";

                    if(is_array($columns))
                        $this->sql[]    =   implode(",",$columns);
                    else
                        $this->sql[]    =   ($columns != false)? $columns:"*";

                    return $this;
                }

            public  function Where($array = false,$like = false)
                {
                    if($array == false)
                        return $this;

                    if(in_array("where", $this->sql))
                        $this->sql[]    =   "and";

                    $this->sql[]    =   "where";    

                    if(is_array($array)) {
                            foreach($array as $key => $value)
                                $where[]    =   ($like != false)? "`$key` like '%".$value."$'":"`$key` = '".$value."'";

                            if(isset($where))
                                $this->sql[]    =   implode("and",$where);
                        }
                    else
                        $this->sql[]    =   $array;

                    return $this;
                }

            public  function From($table = 'gametbl')
                {
                    $this->sql[]    =   "from";
                    $this->sql[]    =   "`$table`";

                    return $this;
                }

            public  function OrderBy($value = false,$order = false)
                {
                    if($value != false) {
                            // I am guessing this is order
                            $heiarchy   =   ($order == 'begin')? " DESC":" ASC";

                            if($value == 'price')
                                $order  =   "`gme_price`".$heiarchy;
                        }

                    if(isset($order))
                        $this->sql[]    =   "order by ".$order;

                    return $this;
                }

            public  function Fetch($return_obj = false)
                {
                    $this->statement    =   implode(" ",$this->sql);

                    return ($return_obj != false)? $this:$this->statement;
                }
        }

    // I don't know what your form fields are called, these are just for instances
    $_POST['gameType']  =   'RTS';
    $_POST['gameTitle'] =   'whatever game';
    $_POST['orderBy']   =   'price';
    $_POST['list']      =   'begin';
    // Create instance of builder
    $SQLBuilder         =   new SQLBuilder();
    // This will just accumulate the statement based on fed-in variables.
    $sql                =   $SQLBuilder->Select()
                                        ->From()
                                        ->Where(array("gme_title"=>$_POST['gameTitle']),true)
                                        ->Where(array("gme_type"=>$_POST['gameType']))
                                        ->OrderBy($_POST['orderBy'],$_POST['list'])
                                        ->Fetch();
    echo $sql;
?>

给你:

select * from `gametbl` where `gme_title` like '%whatever game$' and where `gme_type` = 'RTS' order by `gme_price` DESC