PHP IF if two conditions are met search

0

I'm doing a search engine, with 3 variables, these are with Ajax but I can not get the result of joining two of them. I pass the code to see if you can give me a hand, thank you very much.

<?php
require("php/db.php");
///////// parametros que recibo del formulario /////////////
$constante ="";
$priceRange = $_POST['price_range'];
$marcas = $_POST['marcas'];
$cat = $_POST['cat'];

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

if(!empty($_POST['marcas']))
    {
    $constante = "WHERE marca_pro = $marcas";
    }
else if(!empty($_POST['cat']))
    {
    $constante = "WHERE cat_pro = $cat";
    }
else if(!empty($_POST["cat"]) && !empty($_POST["marcas"]))
    {
    $constante = "WHERE cat_pro = $cat AND marca_pro = $marcas";
    }
else{
    $constante = "";
}
////////// consulta ///////////
$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\">
$marcas
$cat
    <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 03.12.2018 в 17:38
source

2 answers

0

is an error in logic:

since the first match is valid. in this case, I would only take the first one in the order osea marcas and omit the following since there was already a true and it should not because continue evaluating if you get cat only evaluate up to cat !
Solution:

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 = "";
}
    
answered by 03.12.2018 в 17:49
0

What your partner tells you is absolutely right. As a rule, when you apply several possible criteria on the same data, always first express the most "demanding" criteria.

Also, be careful that the values of $_POST (or $_GET , if that's what we're about) arrive, and that they are not empty strings, or only with blank spaces (it's happened to me). For example, you can put:

$cat_pro = (isset($_POST('cat_pro'))?trim($_POST('cat_pro')):'';
$marca_pro = (isset($_POST('marca_pro'))?trim($_POST('marca_pro')):'';
if ($cat_pro > '' && $marca_pro > '') {
   $constante = "WHERE cat_pro = $cat_pro AND marca_pro = $marca_pro "; 
} elseif ($cat_pro > '') {
   $constante = "WHERE cat_pro = $cat_pro "; 
} elseif ($marca_pro > '') {
   $constante = "WHERE marca_pro = $marca_pro ";
} else {
   $constante = '';
}

It's more cumbersome, but everything is more secure. Greetings.

    
answered by 03.12.2018 в 18:08