【问题标题】:Filtering arrays using search form [php]使用搜索表单过滤数组 [php]
【发布时间】:2017-07-09 10:11:28
【问题描述】:

我有数组列表作为我的数据和 html 表单用于我的搜索过滤。 我想要实现的是使用搜索表单过滤数组。搜索表单已经在选择选项中具有价值。我有 3 个值可用于过滤 -> location, type and status。 如下所示:

<form name="search-form" method="GET">
   <div class="keyword-input"><input type="text" name="keyword" placeholder="Keyword"></div>
   <div class="searchform-title">Location</div>
   <div style="margin-bottom:20px;">
      <select name="location" style="width:100%; padding:5px">
         <option value='' selected>Any</option>
         <option value='A city'>A city</option>
         <option value='B City'>B City</option>
         <option value='C city'>C city</option>
         <option value='D city'>D city</option>
         <option value='L city'>L city</option>
         <option value='M city'>M city</option>
         <option value='T city'>T city</option>
      </select>
   </div>
   <div class="searchform-title">Type</div>
   <div style="margin-bottom:20px;">
      <select name="type" style="width:100%; padding:5px">
         <option value='' selected>Any</option>
         <option value='Type 1'>Type 1</option>
         <option value='Type 2'>Type 2</option>
         <option value='Type 3'>Type 3</option>
         <option value='Type 4'>Type 4</option>
      </select>
   </div>
   <div class="searchform-title">Status</div>
   <div style="margin-bottom:20px;">
      <select name="status" style="width:100%; padding:5px">
         <option value='' selected>Any</option>
         <option value='New'>New</option>
         <option value='Old'>Old</option>
         <option value='Under'>Under</option>
      </select>
   </div>
   <input class="proj-search-btn" type="submit" name="search" value="Search" />
</form>

使用 $_GET 函数,我可以知道用户想要什么过滤。 (例如来自网址search.php?location=A+city&amp;type=Type+2&amp;status=

现在我的数组列表如下所示:

$products = array();

       $products[] = array('name'=> 'Product 1','description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'location' => 'A city', 'type' => 'Type 1', '

status' => 'new', 'tags'=>'', 'page_url'=>'p1.html', 'image'=>'products/assets/images/1/prod-image.jpg');

    $products[] = array('name'=> 'Product 2','description' => 'Donec eleifend quam neque, ut mollis massa aliquet id.', 'location' => 'B city', 'type' => 'Type 1', 'status' => 'under', 'tags'=>'', 'page_url'=>'p2.html', 'image'=>'products/assets/images/2/prod-image.jpg');

    $products[] = array('name'=> 'Product 3','description' => 'Nam non tristique mi.', 'location' => 'A city', 'type' => 'Type 3', 'status' => 'new', 'tags'=>'', 'page_url'=>'p3.html', 'image'=>'products/assets/images/3/prod-image.jpg');

    $products[] = array('name'=> 'Product 4','description' => 'Vestibulum accumsan dolor id orci gravida viverra.', 'location' => 'C city', 'type' => 'Type 2', 'status' => 'new', 'tags'=>'', 'page_url'=>'p4.html', 'image'=>'products/assets/images/4/prod-image.jpg');

使用搜索表单,将数据数组过滤到位置、类型和状态,然后显示过滤结果。

考虑 ff.我遇到的问题:

  • 有没有可能类似于WHERE location = $_GET['location'] AND type = $_GET['type'] AND status = $_GET['status']的SQL查询条件的输出。

  • 当用户选择任何位置时,它将仅过滤用户选择的类型和状态(无论位置如何,因为它被选择为任何)

  • 当用户选择 anylocationtype 并选择 status 作为 new 时也是如此。

  • 过滤中所选值的任何组合都应该有效。

这是我迄今为止通过搜索Sample here所尝试的

感谢任何帮助。我正在学习 PHP 和数组方面的新东西。

【问题讨论】:

    标签: php html arrays forms multidimensional-array


    【解决方案1】:

    你可以使用array_filter:

    // assuming $products is an array of products...
    
    if (isset($_GET['location']) && $_GET['location']!=='') {
        // only apply this filter if $_GET['location'] is set and is not blank:
        $products = array_filter($products, function($product) {
            // return TRUE (keep this product in the list) if its location matches
            return $product['location'] === $_GET['location'];
        });
    }
    
    // $products is now only the products whose location matched the selected location
    // unless "All" was selected as the location, in which case $products is unchanged
    
    // you can repeat the above code again for type and status to filter $products further
    

    【讨论】:

    • 非常感谢!这回答了我的问题,而且很容易理解。
    • 我想知道过滤后如何将所有数组$products按产品名称排序?
    • 您需要查看usort() 来执行此操作。该链接上的示例 #2 接近您正在处理的数据。
    • 如果您不介意还有一个问题...从上面的代码中,我如何过滤like 不等于所选选项的值的位置。示例return $product['location'] which is like $_GET['location']; 因为有时我会输入一些位置、类型或状态的大写字母(A citya CityA City)。
    • 在回调函数的use ($keyword) 部分,您可以声明多个要使用的变量。所以,你可以做类似function($product) use ($keyword, $location, $status) { return stripos($product['description'], $keyword) !== false &amp;&amp; $product['location'] === $location &amp;&amp; $product['status'] === $status; }
    猜你喜欢
    • 2015-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 1970-01-01
    相关资源
    最近更新 更多