PHP按类别过滤搜索结果

I'm trying to have an advanced sidebar that filters the results when someone uses the search bar. I have the regular search working and also made it display the count of items in a category on another sidebar (i.e. price range, brand, condition) but now i want the user to click a category and narrow down the results like what you see on newegg.com.

This is my search query:

 function build_query($product_search) {
$search_query = "SELECT * FROM tbl_product p INNER JOIN tbl_brand b ON p.br_id = b.br_id";
$clean_search = str_replace(',', ' ', $product_search);
$search_word = explode(' ', $clean_search);
$final_search_words = array();
if (count($search_word) > 0) {
    foreach ($search_word as $word) {
        if (!empty($word)) {
            $final_search_words[] = $word;
        }
    }
}
$where_list = array();
if (count($final_search_words) > 0) {
    foreach($final_search_words as $word) {
        $where_list[] = "p.pd_name LIKE '%$word%'";
    }
}
$where_clause = implode(' OR ', $where_list);
if(!empty($where_clause)) {
    $search_query .= " WHERE $where_clause";
}

Same thing for my sidebar that counts the categories:

function narrow_query($product_search) {
$search_query = "SELECT p.pd_name, p.pd_con, b.br_name AS name, c.cat_name AS cat,
    COUNT(p.pd_con) AS con, COUNT(b.br_name) AS brand, COUNT(c.cat_name) AS cat_c,
    COUNT(CASE WHEN p.pd_price >= '00.01' AND p.pd_price <= '99.99' THEN 1 END) AS cnt1,
    COUNT(CASE WHEN p.pd_price >= '100.00' AND p.pd_price <= '199.99' THEN 1 END) AS cnt2,
    COUNT(CASE WHEN p.pd_price >= '200.00' AND p.pd_price <= '299.99' THEN 1 END) AS cnt3,
    And so on and so on...
                FROM tbl_product p JOIN tbl_brand b ON b.br_id = p.br_id
                JOIN tbl_category c ON c.cat_id = p.cat_id";
$clean_search = str_repl...
    (Same word filter code as above)

Now i had something going that kinda worked by grabbing the $_GET:

"<a href=\"" . $_SERVER['PHP_SELF'] . "?productsearch=" . $product_search . "&narrow=" . $row['cat'] . "$link=1\">"

and using case in my functions:

function any_query($product_search, $link, $narrow) {
previous info from above...
  if(!empty($link)) {
    switch($link)
    case 1:
      $search_query .= " AND b.br_name = '$narrow'";
      break;
    default:
    }
    }

but it's not doing the job and I'm just lost at this point.

Is there a better way or a different method to achieve this? It doesn't have to use case or $_GET. Thank you for your time.

The problem is you need to add a parentheses around your OR statements. I would do this:

$search_query .= ' WHERE 1';

if (!empty($where_clause)) {
    $search_query .= " AND ($where_clause)";
}

if (!empty($narrow)) {
    $search_query .= " AND $narrow";
}