samedi 19 septembre 2020

PHP if else statement returning at first statement and not continuing to 3rd statement. Search Filtering in PHP

The problem is that I can't reach the 3rd "else if statement" where the scenario is like this:
I have 3 textboxes in HTML -> minimum value, maximum value and search keyword. if the 2 textbox has values I want to reach the 3rd if I put values in 2 textboxes, but what's happening is it's only returning to the 1st if statement.

 <?php
    $price_min = $_POST['price_min'];
    $price_max = $_POST['price_max'];
    $keyword = $_POST['keyword'];
    $sql = 'SELECT * FROM products ';
    
    if (!empty($price_min)) {
      $sql .='WHERE price >='.$price_min;
    } else if (!empty($price_max)) {
      $sql .='WHERE price <='.$price_max;
    } else if (!empty($price_min) && !empty($price_max)) {
      $sql .='WHERE price BETWEEN '.$price_min.' AND '.$price_max;
    } else if (!empty($keyword) && !empty($price_min)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price >='.$price_min.')';
    } else if (!empty($keyword) && !empty($price_max)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price <='.$price_max.')';
    } else if (!empty($keyword) && (!empty($price_min) AND !empty($price_max)) {
      $sql .='WHERE (tags LIKE "%'.$keyword.'%") AND (price BETWEEN '.$price_min.' AND '.$price_max.')';
    } else {
      echo "all are empty// ";
    }
  ?>

Aucun commentaire:

Enregistrer un commentaire