Good day,
correct the logic if the request for several parameters is incorrect. there is a form with many checkboxes, they are divided into categories: name, color, price. There is a table with all these values ​​for each product:

<input type="checkbox" name="name[]" value="Top"/> <input type="checkbox" name="name[]" value="Sun"/> <input type="checkbox" name="name[]" value="Mic"/> <input type="checkbox" name="name[]" value="Auf"/> <input type="checkbox" name="name[]" value="Seu"/> <input type="checkbox" name="color[]" value="red"/> <input type="checkbox" name="color[]" value="green"/> <input type="checkbox" name="color[]" value="yellow"/> <input type="checkbox" name="color[]" value="black"/> <input type="checkbox" name="cena[]" value="100"/> <input type="checkbox" name="cena[]" value="200"/> <input type="checkbox" name="cena[]" value="300"/>` 

I choose checkboxes, pass them $ _POST. In the case of the 1st selected parameter, everything is easy to check and put in the request

 if(isset($_POST['cena']){ foreach( $_POST['cena'] as $cena){ $price = mysql_real_escape_string($cena); .... while ($row = mysqli_fetch_assoc($result)) { $query='SELECT * FROM `table` WHERE `price`>="'.$price.'" AND `price`<="'. ($price+100).'" ORDER by `price`'; ...... echo $row['name']; } } } 

everything is in order, we sort as necessary. But if I choose two or three requests, which I do wrong, sorting by price goes by each name, and not by all lines + I think there are unnecessary requests,
how to do it right?

 `if(isset($_POST['cena'] && isset($_POST['color']) && isset($_POST['name'])){ foreach( $_POST['cena'] as $cena){ $price = mysql_real_escape_string($cena); foreach( $_POST['color'] as $color){ $col=$color; foreach( $_POST['name'] as $name){ $nam=$name; .... while ($row = mysqli_fetch_assoc($result)) { $query='SELECT * FROM table WHERE name="'.$nam.'" AND color="'.$color.'" AND price>="'.$price.'" AND price<="'.($price+100).'" ORDER by price'; echo $row['name']; echo $row['price']; } } } } } 

Thank you in advance!

  • Xardas spelled like this Xardas : 33 Gautoman: 33 - k0mar
  • But this is in the bourgeois version :) - ksardas

1 answer 1

 if(isset($_POST['cena'] && isset($_POST['color']) && isset($_POST['name'])){ ........................................... $colorlist =(isset($_REQUEST['color']))?implode($_REQUEST['color'],','):''; $colorListFilter = ($colorList!='')?' and color in ('.$colorList.')':''; /* ! $name аналогично делаете $colorListFilter */ $min = min($_POST['cena']); $max=(count($_POST['cena'])==1)?($min+100):(max($_POST['cena'])); $sql = 'SELECT * FROM table WHERE price BETWEEN '.$min.'" AND "'.$max.'" '.$colorListFilter.' ORDER by price'; $result = $mysqli->query($sql); while ($row = $result->fetch_assoc()) { ............................................ } } 
  • Wow, thank you so much, did, you have to learn the correct code, I really don’t know where. otherwise you are the city of: D - ksardas