I am currently creating a multiple dropdown query where the user can query by (3) factors for returning results, my issue is how can I do this in an efficient manner so I am not obscurely writing multiple possible MySQL Queries.
<select name="class">
<OPTION VALUE='any'>Choose Class
<option value="a">Block A</option>
<option value="b">Block B</option>
<option value="c">Block C</option>
</select>
<select name="category">
<OPTION VALUE='any'>Choose Type
<option value="math">Math</option>
<option value="science">Science</option>
<option value="history">History</option>
</select>
How can I successfully create a MySQL query that will correctly select the right parameters in the case that a variable is missing. In example, if they choose to do the first dropdown and only search for the “class” and not choose the second dropdown. I want to be able to do the first query, the second query or both of them. I have the PHP, Ajax written, I’m just stumped as to the correct structure of the MySQL query.
Put your “WHERE” clause conditions in one array. I would do like this:
You may want to initialize the
$wherearray with something more interesting than “TRUE” (which is there in case the user does not filter by class neither category, because we don’t want an empty $where array reaching the last line). For example: