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 ?
– César Sousa
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.
– Bacco