Problems with link and query

3

Hello, I'm doing a site where I need to change the query when I click on a link. For example, I have a product, ring, and I want to split into gold and silver ring. At the beginning of the page I made a query:

$sql= "SELECT * FROM produtos WHERE tipo='aneis' ORDER BY relevancia DESC";

there in the content of the page I created the links:

<ul>
            <li ><a onclick="<?php $sql= "SELECT * FROM produtos WHERE tipo='aneis' ORDER BY relevancia DESC"; ?>">Todos</a></li>
            <li ><a onclick="<?php $sql= "SELECT * FROM produtos WHERE tipo='aneis' AND classificacao='ouro' ORDER BY relevancia DESC"; ?>">Ouro</a></li>
            <li ><a onclick="<?php $sql= "SELECT * FROM produtos WHERE tipo='aneis' AND classificacao='prata' ORDER BY relevancia DESC"; ?>">Prata</a></li>
        </ul>  

And my images are below, like this:

$sql1=mysql_query($sql);
                while($dados=mysql_fetch_array($sql1)){
                echo "<li><a href=".$dados['foto']." onclick='".mysql_query($res)."' style=' margin-left:0' data-lightbox='image-1' data-title=".$aux2.">
            <img  style=' width:200px; height:150px;  ' border='0' alt='image 02' src=".$dados['foto']." />
            <figcaption>".$dados['nome']." - ".$dados['codigo']."</figcaption></a></li>     ";}

However, he performs, at the beginning the last query, selecting only the silver products. And I wanted it to show everyone at first, and when I clicked on the gold link, for example, it would refresh the page just with the gold rings.

    
asked by anonymous 05.03.2015 / 01:15

1 answer

5

That's not how it works. onclick is an HTML attribute which runs a Javascript-side client side. When you call 3 times $sql = ... , you are actually running PHP code on the server side and discarding the content that was passed the previous two times.

Basically you're running this:

$sql= "SELECT * FROM produtos WHERE tipo='aneis' ORDER BY relevancia DESC"; 
$sql= "SELECT * FROM produtos WHERE tipo='aneis' AND classificacao='ouro' ORDER BY relevancia DESC";
$sql= "SELECT * FROM produtos WHERE tipo='aneis' AND classificacao='prata' ORDER BY relevancia DESC";

That is, $sql will always will contain the last value passed.

One way to get the desired operation is to pass a parameter to PHP through the URL and then mount the SQL accordingly.

You can get parameters passed in the URL through the global $_GET .

Example:

HTML:

<ul>
   <li ><a href="seu_script.php?filtro=todos">Todos</a></li>
   <li ><a href="seu_script.php?filtro=ouro">Ouro</a></li>
   <li ><a href="seu_script.php?filtro=prata">Prata</a></li>
</ul> 

PHP:

<?php
$extra = '';

if(!empty($_GET['filtro'])) {
  $filtro = mysql_real_escape_string($_GET['filtro']);

  // caso exista um parâmetro "filtro" na URL, a query vai conter o conteúdo extra "AND classificacao='[VALOR-DO-FILTRO-PASSADO]'"
  // caso o filtro seja igual a "todos" (ou não seja passado), nada será adicionado ao SQL e a query vai retornar todos os itens normalmente    
  if($filtro != 'todos') {
      $extra =  " AND classificacao='{$filtro}'";
  }
}

$sql = "SELECT * FROM produtos WHERE tipo='aneis' {$extra} ORDER BY relevancia DESC";

// aqui a query já está pronta!
    
05.03.2015 / 01:32