猿问

实现PHP搜索过滤表单的设计模式

我正在用 PHP 编写的网站上的搜索功能中实现过滤器。


我的目标是保持这种可管理性,因为该项目的规模将在未来一段时间内大幅增长。现在搜索页面有一些输入字段,php 通过检查表单按钮的名称是否在 $_POST 数组中来检查表单是否已提交。在这个搜索页面的右侧,有一个实际搜索结果的列表,它是一个 foreach 循环在打印到屏幕上的项目列表上的输出。为了更清楚,我编写了一些代码来解释它的作用,并展示了我自己想出的东西:


    <?php

    $arrayToDisplay = array(

            array("id"=>0,"name"=>"first","lastname"=>"last"),

            array("id"=>1,"name"=>"first","lastname"=>"last"),

    );


    $acceptedName = "";



    if(isset($_POST['buttonName'])) {

        if(isset($_POST['name'])) {

            $acceptedName = sanitize($_POST['name']);

        }

    }

?>


<form>

<input type="text" name="name">

<input type="submit" name="buttonName">

</form>


<?php



    foreach($arrayToDisplay as $person) {

            if(exists($acceptedName) && $acceptedName != "") {

                if($person["name" == $acceptedname) {

                    echo $person["id"]."  ".$person["name"];

                }       

            }

            else {

                echo $person["id"]."  ".$person["name"];

            }


    }

?>

这可以对一个值起作用,按名称过滤。但最终这些人员对象将具有近 50 个属性,搜索过滤器不仅要能够按名称过滤列表,还必须能够过滤各种人员属性。这个解决方案是不可维护的。


是否存在一种现有的设计模式,可以在这种情况下使用?


森林海
浏览 140回答 1
1回答

千巷猫影

有很多方法可以解决这个问题,有些人认为您可以/应该输入每个表单元素及其相应的处理程序,因为如果您想使用文本框、选择列表、单选按钮等,它会更容易维护。也就是说,您提到简单地使用文本框,所以这里有一种使用单个数组来驱动整个应用程序的方法。你没有提到你是否打算使用 SQL 来驱动你的搜索,所以我假设你会使用它。您可以使用单个数组来驱动 HTML 表单和将由传入值生成的查询。<?php// This array will drive your form and your query.$columns = [&nbsp; &nbsp; 'firstname',&nbsp; &nbsp; 'lastname',&nbsp; &nbsp; 'email',&nbsp; &nbsp; 'address_1',&nbsp; &nbsp; 'address_2',&nbsp; &nbsp; 'city'];// The form was submitted.if ($_SERVER['REQUEST_METHOD'] === 'POST') {&nbsp; &nbsp; // Match posted data to the `$columns` array and filter out any empty values.&nbsp; &nbsp; $postedData = array_filter(array_intersect_key($_POST, array_flip($columns)));&nbsp; &nbsp; // We have at least 1 search criteria to use.&nbsp; &nbsp; if (!empty($postedData)) {&nbsp; &nbsp; &nbsp; &nbsp; // Generate a list of columns for a WHERE clause. Ex. [`address_1 LIKE ?`, `name LIKE ?`]&nbsp; &nbsp; &nbsp; &nbsp; $terms = array_map(function ($val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "$val LIKE ? ";&nbsp; &nbsp; &nbsp; &nbsp; }, array_keys($postedData));&nbsp; &nbsp; &nbsp; &nbsp; // Generate a list of values wrapped in percent signs. Ex. [`123 King St.`, `%tony%`]&nbsp; &nbsp; &nbsp; &nbsp; $values = array_map(function ($val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "%$val%";&nbsp; &nbsp; &nbsp; &nbsp; }, $postedData);&nbsp; &nbsp; &nbsp; &nbsp; // Change this to `AND ` if needed.&nbsp; &nbsp; &nbsp; &nbsp; $query = 'SELECT * FROM table WHERE ' . implode('OR ', $terms);&nbsp; &nbsp; &nbsp; &nbsp; // Just showing you what the generated query and data looks like.&nbsp; &nbsp; &nbsp; &nbsp; var_dump([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_values($values)&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; // Finally, use them to drive your query.&nbsp; &nbsp; &nbsp; &nbsp; // $sql = $connection->prepare($query);&nbsp; &nbsp; &nbsp; &nbsp; // $sql->execute(array_values($values));&nbsp; &nbsp; }}?><form method="post">&nbsp; &nbsp; <?php foreach ($columns as $column): ?>&nbsp; &nbsp; &nbsp; &nbsp; <label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="<?php echo $column; ?>" placeholder="<?php echo $column; ?>">&nbsp; &nbsp; &nbsp; &nbsp; </label>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; <?php endforeach; ?>&nbsp; &nbsp; <input type="submit" value="Search"></form>样本输出array (size=2)&nbsp; 0 => string 'SELECT * FROM table WHERE firstname LIKE ? OR email LIKE ? ' (length=59)&nbsp; 1 =>&nbsp;&nbsp; &nbsp; array (size=2)&nbsp; &nbsp; &nbsp; 0 => string '%Tony%' (length=6)&nbsp; &nbsp; &nbsp; 1 => string '%tony@email.com%' (length=16)
随时随地看视频慕课网APP
我要回答