Link and query problems

Asked

Viewed 71 times

3

Hello, I’m making a website where I need to change the query when I click on a link. For example, I have a product, a ring, and I want to divide it into a ring of gold and silver. At the top 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 were 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 first the last query, selecting only the silver products. And I wanted it at first to display all of them, and when I clicked on the gold link, for example, it updated the page with just the golden rings.

  • Why don’t you do it with jquery would be easier ?

  • Your code makes no sense. PHP runs on the server. After it runs, the resulting page is sent to the user. Only then does Javascript run. You are simply assigning a string to $sql three times, and only using the latter. Also, your "onclick" goes empty for the client, because PHP is not "printing" anything inside it. It would be nice if you devoted yourself to simpler tests until you understand PHP, then skip to this step.

1 answer

5


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

Basically the is running is 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 to say, $sql at all times 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 pick up 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: (something like that)

<?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!

Browser other questions tagged

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