0

Users select some parameters with checkboxes from an HTML form:

Example: they choose eggs, carrots and beans from 100 possible ingredients.

These selected parameters are stored in an array such as $ingredients['eggs','carrots','beans'];

A mysql query has to input name results where eggs=1, carrots=1, and beans=1

    $db->query("SELECT name, FROM ingredients WHERE eggs=1, carrots=1, beans=1")->fetchall();

But how if I have $ingredients has a different size?

Thanks for your help.

3
  • generate string for where condition through foreach loop and concat with query. Commented Sep 24, 2015 at 9:44
  • You haven't given us much to go on. What is your database structure? Commented Sep 24, 2015 at 9:48
  • he just want a where part of the query like he asked?? why do you need database stucture???? @Kuya Commented Sep 24, 2015 at 10:44

1 Answer 1

3
$where = "WHERE 1 ";
foreach($ingredients as $key=>$value){
    $where.=" AND {$value}=1";
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.