PHP IF if there are three conditions, search engine can not filter for prices

2

I still have problems, forgive my ignorance, and try by all means and I can not filter through the price range:

This is the code Ajax that is in the home

 <script>

    function filterProducts() {

    var marcas = $('.valor_marca').val();

    var cat = $('.valor_cat').val();

    var price_range = $('.price_range').val();

    $.ajax({

        type: 'POST',

        url: 'buscar.php',

        data: 'price_range='+price_range+'&marcas='+marcas+'&cat='+cat,

        beforeSend: function () {

            $('.container').css("opacity", ".5");

        },

        success: function (html) {

            $('#productContainer').html(html);

            $('.container').css("opacity", "");

        }

    });

}

</script>

this is the code of the search.php page

<?php

require("php/db.php");

///////// parametros que recibo del formulario /////////////

$constante ="";

$marcas = $_POST['marcas'];

$cat = $_POST['cat'];

$priceRange = $_POST['price_range'];


if(!empty($_POST['price_range']))

    {

    $priceRangeArr = explode(',', $priceRange);

    $constante = "WHERE precio_pro BETWEEN '".$priceRangeArr[0]."' AND '".$priceRangeArr[1]."'";

    }

if(!empty($_POST["cat"]) && !empty($_POST["marcas"])) 

    {

    $constante = "WHERE cat_pro = $cat AND marca_pro = $marcas"; 

    }

else if(!empty($_POST['cat']))

    {

    $constante = "WHERE cat_pro = $cat";

    }

else if(!empty($_POST['marcas']))

    {

    $constante = "WHERE marca_pro = $marcas";

    }

else{

    $constante = "";

}

////////// condiciones ///////////

$consulta1 = mysqli_query($con, "SELECT * FROM productos Left Join marcas ON productos.marca_pro = marcas.id_marcas Left Join combo ON productos.combo_pro = combo.id_combo $constante");

while($row = mysqli_fetch_array($consulta1, MYSQL_ASSOC)) {

echo "<!-- combo --><div class=\"card mb-4\">


    <img class=\"card-img-top\" src=\"images/combos/".$row['foto_combo']."\" alt=\"Card image cap\">

    <div class=\"card-body\">

    <div class=\"row\">


<!-- productos --><div class=\"col-xl-6 col-lg-6\">

    <div class=\"marca\">".$row['nombre_marcas']."</div>

    <div class=\"desc\">".$row['nombre_pro']."</div>

    <div class=\"precio\">$ ".$row['precio_pro']."</div>

</div><!-- end productos -->

</div>

    </div>

  </div><!-- end combo -->";

}

?>
    
asked by Alejandro Arla 04.12.2018 в 00:17
source

1 answer

0

Of course you can not filter by price, you are overwriting their value all the time.

Try this way:

$constante = ' WHERE 1 ';

if(!empty($_POST['price_range']))  {
    $priceRangeArr = explode(',', $priceRange);
    $constante .= " AND precio_pro BETWEEN '".$priceRangeArr[0]."' AND '".$priceRangeArr[1]."'";
}

if(!empty($_POST['cat'])) {
    $constante .= " AND cat_pro = $cat";
}

if(!empty($_POST['marcas'])){
    $constante .= " AND marca_pro = $marcas";
}

In this way you avoid else , it is not practical to write code for each of the possibilities, imagine when you have 20 fields!

We start by giving a default value that is neutral, in this case WHERE 1 , which is valid for any value (that is, acts as if there was no WHERE ).

And now we will only concatenate more values of the filtering as they exist, otherwise we do not add anything.

At the end we have in the chain $constante the correct value according to whether or not parameters arrive.

    
answered by 04.12.2018 / 02:07
source