PDO multi-Filter sql query
<p>Is there a simpler way for me to code this and perform the same thing without having to have so many queries? Im trying to add pagination (not included in code here) and it works on my ALL query, but the AND / OR queries give funny results and is becoming a headache. Other than that the filtering of results works perfect just need pagination.</p>

<pre><code>$filter = isset($_POST['filter']) ? $_POST['filter'] : null;
$status = isset($_POST['status']) ? $_POST['status'] : null;

if(empty($filter) &amp;&amp; empty($status)) {

    //echo 'ALL query';
    $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id';
    $sth = $link-&gt;prepare($sql);
    $sth-&gt;execute(array());
    $result = $sth-&gt;fetchall();

} else {
    // display result if filter AND status are selected
    if(!empty($filter) &amp;&amp; !empty($status)) {

        //echo 'AND query';
        $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id WHERE module_type = :filter AND product_status = :status';
        $sth = $link-&gt;prepare($sql);
        $sth-&gt;execute(array(':filter' =&gt; $filter, ':status' =&gt; $status));
        $result = $sth-&gt;fetchall();

    } else {
        // display result if filter OR status are selected
        if(!empty($filter) || !empty($status)) {

            //echo 'OR query';
            $sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id WHERE module_type = :filter OR product_status = :status';
            $sth = $link-&gt;prepare($sql);
            $sth-&gt;execute(array(':filter' =&gt; $filter, ':status' =&gt; $status));
            $result = $sth-&gt;fetchall();

        }
    }
}

//test sql
echo $sql.'&lt;br /&gt;';

$bgcolor = '';
foreach($result as $key =&gt; $value) {

    if(($bgcolor=='#ffffff') ? $bgcolor='#f1f1f1' : $bgcolor='#ffffff') {

    echo '&lt;tr bgcolor="'.$bgcolor.'"&gt;';
    echo '&lt;td&gt;'.$value['product_id'].'&lt;/td&gt;';
    echo '&lt;td&gt;'.$value['product_name'].'&lt;/td&gt;';
    echo '&lt;td&gt;'.$value['product_type'].'&lt;/td&gt;';
    echo '&lt;td&gt;'.$value['module_name'].'&lt;/td&gt;';
    echo '&lt;td&gt;'.$value['product_price'].'&lt;/td&gt;';
    echo '&lt;td&gt;'.$value['product_status'].'&lt;/td&gt;';
    echo '&lt;/tr&gt;';
    }
}
    echo '&lt;/table&gt;';
</code></pre>
