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.
$filter = isset($_POST['filter']) ? $_POST['filter'] : null;
$status = isset($_POST['status']) ? $_POST['status'] : null;
if(empty($filter) && empty($status)) {
//echo 'ALL query';
$sql = 'SELECT * FROM _product JOIN _module_type ON module_type = module_id';
$sth = $link->prepare($sql);
$sth->execute(array());
$result = $sth->fetchall();
} else {
// display result if filter AND status are selected
if(!empty($filter) && !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->prepare($sql);
$sth->execute(array(':filter' => $filter, ':status' => $status));
$result = $sth->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->prepare($sql);
$sth->execute(array(':filter' => $filter, ':status' => $status));
$result = $sth->fetchall();
}
}
}
//test sql
echo $sql.'<br />';
$bgcolor = '';
foreach($result as $key => $value) {
if(($bgcolor=='#ffffff') ? $bgcolor='#f1f1f1' : $bgcolor='#ffffff') {
echo '<tr bgcolor="'.$bgcolor.'">';
echo '<td>'.$value['product_id'].'</td>';
echo '<td>'.$value['product_name'].'</td>';
echo '<td>'.$value['product_type'].'</td>';
echo '<td>'.$value['module_name'].'</td>';
echo '<td>'.$value['product_price'].'</td>';
echo '<td>'.$value['product_status'].'</td>';
echo '</tr>';
}
}
echo '</table>';
And My Second thought:
It needs testing and as truth said you might not need the OR but that is upto exactly how your form and filtering works tbh, but I am gonna leave it at that it should give you pointers to move to the next stage.
Edit again:
Couldn’t help myself, here is a third possibility: