D
D
Danil2016-06-02 10:43:19
MySQL
Danil, 2016-06-02 10:43:19

How to organize a multiple OR query with a dynamic number of conditions?

There is a search, in it the user can add a number of conditions and I need to display the database in accordance with what he has chosen. For example, he chose 3 checkboxes and I have to select in the database:

... OR checkbox1=value1 OR checkbox2=value2 OR checkbox3=value3 ...

How to form such a request? A if (условие)lot of code comes out through and if there are 20+ points, then in general it is not realistic to describe all combinations of checkboxes. How to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2016-06-02
@Veneomin

sequentially check the items in the loop, write the conditions to the array, at the end combine it into a string and collect the request.

D
DarkMatter, 2016-06-02
@darkmatter

If points > 2 then:

$InString = '';
        foreach ($_POST["checkbox"] as &$value) {
            $value = intval($value);
            $InString .= $value.",";
        }
        $InString = preg_replace('/,$/ism', '', $InString);

SELECT FROM `indexation_job` WHERE id IN ('.$InString.')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question