Multiple Checkbox Filter in PHP

≯℡__Kan透↙ 提交于 2019-12-25 07:51:25

问题


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.


回答1:


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']);



回答2:


Do something like this

where p_location in (the values for your checkboxes)



回答3:


Try this way:

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

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

} else if (!empty($_POST['check_list'])) {
    $params = implode(",", array_fill(0, count($_POST['check_list']), "?"));

    $sql = 'SELECT * FROM `people` WHERE `p_location` IN ($params)';

    $stmt   = $conn->prepare($sql);

    $stmt->execute($_POST['check_list']);
}


来源:https://stackoverflow.com/questions/16123826/multiple-checkbox-filter-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!