使用搜索表单过滤数组[php]

时间:2017-02-20 08:06:52

标签: php html arrays forms multidimensional-array

我有数组列表作为我的搜索过滤的数据和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&type=Type+2&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。我遇到的问题:

  • 是否可能看起来像SQL查询条件的输出类似于WHERE location = $_GET['location'] AND type = $_GET['type'] AND status = $_GET['status']

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

  • 当用户选择任意同时locationtype并选择status 新。

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

这是我到目前为止通过搜索Sample here

尝试过的

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

1 个答案:

答案 0 :(得分: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