PHP中有多个复选框过滤器 [英] Multiple Checkbox Filter in PHP

查看:140
本文介绍了PHP中有多个复选框过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图弄清楚这一点,但没有得到它的任何地方。我已经阅读了很多在这里和互联网上发布的其他问题,但无法找到解决方案。特别是在使用多个复选框的情况下。



当我尝试解释我面临的问题时,请耐心等待。

在页面上,我有一个人员列表,他们对应的位置,我从数据库中使用php / sql拉。我想要实现的是能够使用复选框根据位置过滤列表。



这是我目前用于复选框的代码。

  < UL> 
< li>< input type =checkboxname =check_list []value =sydney>悉尼< / li>
< li>< input type =checkboxname =check_list []value =durban>德班< / li>
< input type =checkboxname =check_list []value =delhi>德里< / li>
< li>< input type =checkboxname =check_list []value =cairo> Cairo< / li>
< li>< input type =checkboxname =check_list []value =madrid>马德里< / li>
< li>< input type =submit>< / li>
< / ul>

这是我用来检查复选框是否被选中的代码,并相应地修改了SQL查询

  if(empty($ _POST ['check_list'])){

$ SQLquery ='SELECT *从`人';
$ query = $ conn-> query($ SQLquery);
$ b} elseif(!empty($ _ POST ['check_list'])){

foreach($ _ POST ['check_list'] as $ check)

$ location = $ check;

$ query = $ conn-> prepare('SELECT * FROM`people` where'p_location` =:location');
$ query-> execute(array('location'=> $ location));






$ b现在这个代码工作正常,只有一个复选框被选中。但是,如果选中多个复选框,则失败。它只是什么都没有显示。

我知道我可以使用 OR 操作符来修改SQL查询来添加其他位置所以:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 'OR'p_location` ='Madrid')

然而,我无法弄清楚我可以如何首先检查所有复选框是否被选中,然后创建SQL语句,当选择了多个城市时,将过滤结果。



我也想知道这是实现这样或者没有的最好的方法。我开放的替代方法实现这个范围内的PHP / SQL。

解决方案

未经测试,但应该给你一个想法

答案已修订!

  $标记= str_repeat('?,',count($ _ POST ['check_list']) -  1)。 ?; 

$ query = $ conn-> prepare('SELECT * FROM`people` where'p_location` IN('。$ markers。')');
$ query-> execute($ _ POST ['check_list']);


I have been trying to figure this out for a while, but am not getting anywhere with it. I have read a lot of the other questions posted here and across the internet but am not being able to find a solution. Specially in the case of using multiple checkboxes.

Please bear with me as I try to explain the problem I am facing.

On a page I have a list of People with their corresponding location which i pull from a database using php/sql. What I am trying to achieve is to be able to filter the list according to location using checkboxes.

This is the code I have currently for the checkboxes.

<ul>
    <li><input type="checkbox" name="check_list[]" value="sydney">Sydney</li>
    <li><input type="checkbox" name="check_list[]" value="durban">Durban</li>
    <li><input type="checkbox" name="check_list[]" value="delhi">Delhi</li>
    <li><input type="checkbox" name="check_list[]" value="cairo">Cairo</li>
    <li><input type="checkbox" name="check_list[]" value="madrid">Madrid</li>
    <li><input type="submit"></li>
</ul>

This is the code I am using to check if a checkbox is checked and accordingly modify the SQL query

if (empty ($_POST['check_list'])) {

    $SQLquery = 'SELECT * FROM `people`';
    $query = $conn->query($SQLquery);

} elseif (!empty($_POST['check_list'])) {

    foreach($_POST['check_list'] as $check)

    $location = $check;

    $query = $conn->prepare('SELECT * FROM `people` WHERE `p_location` = :location');
    $query->execute(array('location' => $location));

}

Now this code works fine when only one checkbox is checked. However, when multiple checkboxes are checked this fails. It just shows nothing.

I know i can modify the SQL query using the OR operator to add other locations like so:

$query = $conn->prepare('SELECT * FROM `people` WHERE `p_location` = 'Delhi' OR `p_location` = 'Madrid')

However, I am unable to figure out how I can first check which all checkboxes are checked and then create the SQL statement in a way which will filter the results when there is more than one city selected.

I am also wondering if this is the best way to achieve something like this or not. I am open to alternative ways of achieving this within the scope of php/sql.

解决方案

Untested, but should give you an idea of what you should do.

ANSWER REVISED!

$markers = str_repeat('?, ', count($_POST['check_list']) - 1) . '?';

$query = $conn->prepare('SELECT * FROM `people` WHERE `p_location` IN (' . $markers . ')');
$query->execute($_POST['check_list']);

这篇关于PHP中有多个复选框过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆