Field search

Asked

Viewed 56 times

1

Hi, I made a page that searches the registered products and then appears the results on the results page.php results.

I would like that on this page results.php later could make a more accurate query as for lower price appear first, have the option to choose the new or used product, appear physical or legal person.

I posted my code on Pastebin: http://pastebin.com/PVkBvA9R

<?php include 'header.php'; ?>

<header class="docs-top">
    <div class="container docs-header">
   Todos os resultados
 </div>
</header>
<section>
  <div class="container">

    <?php 
    require 'conexao.php';

    $produto = $_POST['produto'];
    $sql = mysql_query("SELECT * FROM usuario where produto LIKE '%".$produto."%' ");

    // SELECT preco FROM usuario ORDER BY preco ASC

    $row = mysql_num_rows($sql);
    if ($row > 0) {
      while ($linha = mysql_fetch_array($sql)) {
        $produto = $linha['produto'];
        $bairro = $linha['bairro'];
        $preco = $linha['preco'];
        $telefone = $linha['telefone'];
        $nome = $linha['nome'];
        $estado = $linha['estado'];
      }
    } else {
      echo "Nenhum resultado encontrado";
    }
    ?>


    <div class="well recomentadion clearfix">
      <h1>Pesquisar</h1>
      <form action="" method="POST">     
        <fieldset>
          <div class="form-group col-md-3">
            <label class="" for="advertisement">Título do Anúncio </label>                
            <div class="control mr-30">
              <input type="text" id="title" name="title" class="form-control" value="" placeholder="Título" data-original-title="" title="">
            </div>
          </div>                  

          <div class="form-group col-md-3">
            <label class="" for="advertisement">Novo/Usado</label>                
            <div class="control mr-30">
              <select class="form-control">                               
                <option>Novo</option>
                <option>Usado</option>
              </select> 
            </div>
          </div>

          <div class="form-group col-md-3">
            <label class="" for="advertisement">Empresa/P. fisica</label>                
            <div class="control mr-30">
             <select class="form-control">
              <option>Todos</option>
              <option>Empresa</option>
              <option>Pessoa fisica</option>
            </select> 
          </div>
        </div>

        <div class="form-group col-md-3">
          <label class="" for="advertisement">Ordernar por Preço</label>                
          <div class="control mr-30">
            <select class="form-control">
              <option>Menor preço</option>
              <option>Maior preço</option>                               
            </select>  
          </div>
        </div>  

        <div class="clear"></div> 
        <input type="submit" style="margin-top:20px;" name="submit" class="btn btn-primary pull-right" value="Pesquisar"> 
      </fieldset>
    </form>
  </div>

  <!-- produto -->

  <div class="bs-docs-example">
    <table class="table table-bordered">
      <tbody>
        <tr>
          <td align="center">:<img src="http://wcloudstatic.s3.amazonaws.com/img/no_image.jpeg" width="150" height="150" /></td>                 
          <td><?php echo ".$produto"; ?> <br><br> <?php echo ".$bairro"; ?> - <?php echo ".$estado"; ?><br> <?php echo ".$nome"; ?> - <?php echo ".$telefone"; ?></td>
          <td><?php echo ".$preco"; ?></td>
        </tr>          
      </tbody>
    </table>    
  </div>

  <!-- /produto -->

</div>

</section>

<?php include 'footer.php'; ?>
  • Always post the code or excerpt of the code you are doubtful here, to facilitate the analysis of colleagues site.

  • 1

    Okay, you said your goal, but you didn’t say what’s stopping you or what the problem/difficulty is. From what I’ve seen, your code should be displaying only the last record and not the whole list.

1 answer

0

Do you want to take the product in the user table? You have to model the database correctly to better implement the querys.

You did the right thing in the query structure to display the product and didn’t do it with the price. Just a little bit of attention.

Following your example, I’ll put everything in a query.

$sql = mysql_query("SELECT * FROM usuario where produto LIKE '%".$produto."%' order by preco asc");

$row = mysql_num_rows($sql);
if ($row > 0) {

  while ($linha = mysql_fetch_array($sql)) {
    $produto = $linha['produto'];
    $bairro = $linha['bairro'];
    $preco = $linha['preco'];
    $telefone = $linha['telefone'];
    $nome = $linha['nome'];
    $estado = $linha['estado'];
  }

} else {
  echo "Nenhum resultado encontrado";
}
?>

Or you can use Wordpress to choose the display mode, by price, product, individual or legal person, etc.

I recommend studying the relations of tables and their keys.

  • But this price should only appear later when the user uses select to better select the search, you know? Just like the other fields

  • So I still don’t understand. Select in the query will return everything in the User table. You will treat the data as you like. In fact, they will be ordered increasing. Just didn’t get it when you say it will show the price when the user selects. In this case, will it only display the price when the user chooses a product? To facilitate, give an example with data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.