单独处理GET参数PHP


Handle GET parameters individually PHP

首先,我对PHP还是个新手,所以请原谅我的理解不足。我正在开发一个表单,它可以从多个选择中获取选项,并根据这些选项向用户提供结果。此表单还存储这些搜索查询,以便以后可以导出它们。

最初,表单只有四个需要存储的参数,但现在我添加了第五个,这使得事情不那么合作。我已经搜索了,尝试了多种不同的方法,但我还是碰壁,正在寻求帮助。

期望发生的是用户选择一个或所有可用选项。前四个选择是简单的是/否问题,值为1表示是,0表示否。第五个是一系列国家名称,其值被设置为它们在数据库中的id。县域选择选项是通过Smarty动态填充的。

下面是用来存储这些值的PHP:

public function recordFilter($args)
    {
        $db = DBManager::getConnection();
        $_POST['type'] = 'f'; // type of search, find providers         
        foreach($args as $key => $a)
        {
            if(isset($_GET['county']) && is_numeric($_GET['county'])
            {
                  $_POST[$key] = $a ? (int)$_GET['county'] : 0; // store value of last parameter, or 0 if not set
            }
            $_POST[$key] = $a ? "y" : "n"; // store 'y' if $a is set, 'n' if not
            var_dump($_POST[$key]);
        }
        parent::save();
    }

目前正在发生的事情是我能够得到这个函数的所有值,并通过它们进行迭代。但是,由于我引入了第五个字段(并且通过不同的方法我试图将其拼凑在一起),要么我的第五个参数被设置为'y',它不会存储在数据库中,因为它的字段是int(2),或者前四个参数的设置值采用第五个参数的值,并最终在其字段中具有与县相关的id。

我想知道的是有什么更好的方法来处理这类问题?我认为while循环可能适合遍历前四个参数,并在完成后处理第五个参数,但弄清楚它的语法有点超出我的能力范围。我还尝试了一个switch语句,但根本不起作用。在这种情况下,使用if语句似乎是最大的麻烦,因为如果设置了'county',它会抛出整个循环。如有任何帮助,不胜感激。

由于编写代码在注释上看起来不太合适,我在这里发布了我的猜测。如果你描述更多的代码,比如$args是什么,或者你是如何处理请求的,这将有助于人们理解你的问题。

  1. 单独处理最后一个请求

    因为它是$_GET请求,你不能迭代它与$_POST

    foreach ($args as $key => $a) {
        $_POST[$key] = $a ? "y" : "n"; // store 'y' if $a is set, 'n' if not
    }
    if (isset($_GET['county']) && is_numeric($_GET['county']) {
          $_POST['county'] = $a ? (int)$_GET['county'] : 0; // store value of last parameter, or 0 if not set
    }
    

第二,我认为这是更好的方法

  • 不要依赖超全局变量

    将它们分配给另一个变量,不要忘记重构parent::save()方法上的代码

    public function recordFilter($args)
    {
        $db = DBManager::getConnection();
        $request = [];       
        foreach ($args as $key => $a) {
            //Sounds like you forgot to check if the $_POST[$key] is set
            $request[$key] = isset($_POST[$key], $a) ? "y" : "n";  
        }
        //You don't have to assign a value to super global $_POST with key of type
        $request['type'] = 'f';
        //Or you may need if statement if this field doesn't a required field
        $request['county'] = (int) $_GET['county'];
        parent::save($request);
    }
    

    在父类的某个地方

    protected function save(array $request)
    {
        //this is your bussiness
    }
    
  • 因为我只是猜测$args是什么,这比更好的一个好

  • filter_input()filter_input_array()赋值给局部变量

    public function recordFilter($args)
    {
        $db = DBManager::getConnection();
        //This array will be used for validating the input $_POST.
        //First, I grab the key of $args then assign the value
        //to $validate with the same key to return it as bool
        $validate = [];
        foreach(array_keys($args) as $key) {
            $validate[$key] = FILTER_VALIDATE_BOOLEAN;
        }
        //Get the post request value           
        $request = filter_input_array(INPUT_POST, $validate);
        //You don't have to assign a value to super global $_POST with key of type
        $request['type'] = 'f';
        //Receive 'county' only if it's int and greater than 1 and lower than 99
        $options = ['options' => ['min_range' => 1, 'max_range' => 99]];
        $request['county'] = filter_input(INPUT_GET, 'county', FILTER_VALIDATE_INT, $options);
        parent::save($request);
    }
    

    再次强调,parent::save()方法需要重构