help with my logic for building a query from inputs
<p>I have 4 input fields and its driving me crazy because i can't figure out when to dynamically add the AND condition</p>

<p>right now my raw query looks like this:</p>

<pre><code>SELECT *
FROM `staff`
" . $where . $location . "
" . $and_1 . $expertise . "
" . $and_2 . $education . "
" . $and_3 . $salary . "
ORDER BY 
`created_on` DESC LIMIT " . $limit;
</code></pre>

<p>because I want to allow for blank fields to add less conditions to the query I have been trying to figure out the logic that would add <code>WHERE</code> and <code>AND</code> to each condition depending on the amount of conditions being requested.</p>

<p>My attemps:</p>

<pre><code>($location == '' ? $location = '' : $location = '`location` = "'.$location.'"');
($expertise == '' ? $expertise = '' : $expertise = '`expertise` = "' . $expertise . '"');
($education == '' ? $education = '' : $education = '`education` = "' . $education . '"');
($location != '' &amp;&amp; ($expertise != '' || $education != '') ? $and_1 = 'AND ' : $and_1 = '');
($location != '' &amp;&amp; ($and_1 != '') ? $and_2 = 'AND ' : $and_2 = '');
($and_1 != '' || $and_2 != '' &amp;&amp; $salary != 'no preference' ? $salary : '');
($and_1 == '' || $and_2 == '' &amp;&amp; $salary != 'no preference' ? '' : $salary);
</code></pre>

<p>I think I'm just getting lost in my own mental logic as I'm coding that I'm not even understanding my own code logic lol ;(</p>
