Create a price filter

0

I have already created a search engine with PHP in which you select a letter or word and look for it in the database. I wanted to know how to filter the data that has been extracted from my database by price, date etc ...

The code I use for my PHP search engine:

$connect = mysqli_connect("localhost", "root", "root", "restaurantes");
$output = '';
$record_per_page = 5;  
if(isset($_POST['query'])){
  $page = $_POST['query'];
  $start_from = ($page - 1)*$record_per_page; 

  $sql = "SELECT * FROM restaurantes_resultado WHERE nombre   
  LIKE '%".$_POST["query"]."%' 
  OR precio LIKE'%".$_POST["query"]."%' 
  OR fecha LIKE'%".$_POST["query"]."%' 
 LIMIT 0 , 5 ";

$result = mysqli_query($connect, $sql);

if(mysqli_num_rows($result) > 0)
{
  while($row = mysqli_fetch_array($result))
{
  $output .= '
   <div class="resultado margenes">
   <div class="imagen col">
   <a href="#" tittle="Imagen1" class="thumbnail"><img src="'.$row["fotos"].'"/></a>
   </div>
  <div class="contenido-medio col">
  <div id="no-margin">

  <h3 class="no-margin">'.$row["nombre"].'</h3> 

  <p> zona:'.$row["precio"].' </p>
  </div>

</div>
    <div class ="barra-vertical col">
    <p> '.$row["fecha"].' </p>
    </div>

    </div>
';} 
 echo $output;  
}else{
echo 'Data Not Found';
 }

}
    
asked by Who iscoo 15.10.2017 в 16:07
source

2 answers

0

It's simple, you just have to add " ORDER BY nombre_columna " in the MySQL statement so that the results are sorted according to the indicated column.

Example:

SELECT * FROM 'usuarios' ORDER BY 'nombre'

What this SQL request would do is list the users, sorted alphabetically by name.

    
answered by 15.10.2017 в 17:28
0
  

but what I want is to put a filter with a bar so that it is the user who indicates what he wants to order, that is, a price interval between X and X + 1

Then, you would have to generate 2 variables (minimum and maximum, in the case of price), and then with PHP, when listing the data with MySQL, verify if the value of the price is GREATER, LESS or EQUAL to the given values .

Example:

$p = (object) $_POST;
$precio = array( $p->pcio_min, $p->pcio_max );

// Código SQL
// Fin Código SQL

// Filtro
while ($data = mysql_fetch_assoc($query)) {

    if ( ($data["precio"] > $precio[0] && $data["precio"] < $precio[1]) 
    || $data["precio"] == $precio[0] || $data["precio"] == $precio[1] ) {

        echo $data["precio"];

    }

}

It's just an example, you can modify it according to your need.

    
answered by 15.10.2017 в 19:09