Jump to content

Recommended Posts

Posted

Boas tardes pessoal

Tenho uma duvida e empanquei.

estou a criar uma página de produtos.

o cliente pode pesquisar os produtos e depois deverao aparecer filtros, como ordenar por: nome, ranking, preço, etc..

a pesquisa de produtos já está a funcar.

como posso criar estes filtros?

Posted (edited)

Boas,

Com if's.

$sql = "SELECT * FROM produtos";

if( isSet( $_GET['filtro'] ) && $_GET['filtro'] == 'nome' ) {

$sql .= " ORDER BY nome";

} elseif( ... ) {

...

}
Edited by Lfscoutinho
Posted

mas como é uma pesquisa já fez uma select antes.

De cada vez que o utilizador clicar num dos filtros a página é recarregada e, com isto, voltas a fazer um select e aplicas a condição do filtro.Basta isto para que funcione.

"Que inquieto desejo vos tortura, Seres elementares, força obscura? Em volta de que ideia gravitais?" >> Anthero de Quental

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Linuxando.com | ...

Posted (edited)

e dou o mesmo nome a todos os filtros?

estou a fazer através de inputs type imagem

<form name="filtro" id="filtro" method="get" action="">
<table align="center" width="100%">
<tr>
 <td colspan="4" align="center" class="style1">
Ordenar por:
	</td>
	</tr>
<tr>
 <td class="style30" valign="top" align="center">
 Descrição<input type="image" src="images/arrow-down.png" name="ddsc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="dasc" width="25" height="25"/>
	</td>
 <td class="style30" valign="top" align="center">
 Preço<input type="image" src="images/arrow-down.png" name="pdsc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="pasc" width="25" height="25"/>
	</td>
	<td class="style30" valign="top" align="center">
 Referência<input type="image" src="images/arrow-down.png" name="rdsc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="rasc" width="25" height="25"/>
	</td>
	<td class="style30" valign="top" align="center">
 Marca<input type="image" src="images/arrow-down.png" name="mdsc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="masc" width="25" height="25"/>
	</td>
</tr>
</table>
</form>

como faço neste caso

Edited by JFernandesVR
Posted

e dou o mesmo nome a todos os filtros?

Porquê? Podes seguir o exemplo do @Lfscoutinho e passar por Get cada filtro...

nome, preço, cor, ...

"Que inquieto desejo vos tortura, Seres elementares, força obscura? Em volta de que ideia gravitais?" >> Anthero de Quental

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Linuxando.com | ...

Posted

eu não colocaria um form aí e faria assim:

Descrição
<a href="[url]/?filtro=descricao-down"><img src="images/arrow-down.png" alt="dispor por bla bla"></a>
<a href="[url]/?filtro=descricao-up"><img src="images/arrow-up.png" alt="dispor por bla bla"></a>

Preço
<a href="[url]/?filtro=maisbarato"><img src="images/arrow-down.png" alt="dispor por preço mais baixo"></a>
<a href="[url]/?filtro=maiscaro"><img src="images/arrow-up.png" alt="dispor por preço mais alto"></a>

Referência
<a href="[url]/?filtro=ref-down"><img src="images/arrow-down.png" alt="bla bla bla "></a>
<a href="[url]/?filtro=ref-up"><img src="images/arrow-up.png" alt="blabla bla"></a>

Marca
<a href="[url]/?filtro=marca-down"><img src="images/arrow-down.png" alt="bla bla bla "></a>
<a href="[url]/?filtro=marca-up"><img src="images/arrow-up.png" alt="blabla bla"></a>

depois na página seguia a sugestão do Lfscoutinho, ou seja:

if (isset $_GET['filtro']) {
  $filtro=$_GET['filtro'];
   if ($filtro=="descricao-down")	{
   $sql="a tua query a ordenar por filtro descrição down";
   }
   elseif ($filtro=="descricao-up") {
   $sql="a tua query a ordenar por filtro descrição up";
   }
   elseif ($filtro=="maisbarato") {
   $sql="a tua query a ordenar por preço";
   }
   elseif ($filtro=="maiscaro") {
   $sql="a tua query a ordenar por preço DESC";
   }
   elseif ($filtro=="ref-up") {
   $sql="a tua query a ordenar por ref up";
   }
   elseif ($filtro=="ref-down") {
   $sql="a tua query a ordenar por ref down";
   }
   elseif ($filtro=="marca-down") {
   $sql="a tua query a ordenar por marca down";
   }
   elseif ($filtro=="marca-up") {
   $sql="a tua query a ordenar por marca up";
   }
   else {
   //se não for nada do que é suposto ser, reencaminha para o index (play safe!)
   }
}
//NEXT>>>>
//executa a tua query e mostra os resultados.
//se não quiseres recarregar a página usas AJAX. Há uns quantos tópicos aqui no p@p sobre AJAX

certamente haverá outras formas de resolver a tua questão, mas se percebi bem, era assim que fazia.

"Que inquieto desejo vos tortura, Seres elementares, força obscura? Em volta de que ideia gravitais?" >> Anthero de Quental

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Linuxando.com | ...

Posted (edited)

eu não colocaria um form aí e faria assim:

Descrição
<a href="[url]/?filtro=descricao-down"><img src="images/arrow-down.png" alt="dispor por bla bla"></a>
<a href="[url]/?filtro=descricao-up"><img src="images/arrow-up.png" alt="dispor por bla bla"></a>

Preço
<a href="[url]/?filtro=maisbarato"><img src="images/arrow-down.png" alt="dispor por preço mais baixo"></a>
<a href="[url]/?filtro=maiscaro"><img src="images/arrow-up.png" alt="dispor por preço mais alto"></a>

Referência
<a href="[url]/?filtro=ref-down"><img src="images/arrow-down.png" alt="bla bla bla "></a>
<a href="[url]/?filtro=ref-up"><img src="images/arrow-up.png" alt="blabla bla"></a>

Marca
<a href="[url]/?filtro=marca-down"><img src="images/arrow-down.png" alt="bla bla bla "></a>
<a href="[url]/?filtro=marca-up"><img src="images/arrow-up.png" alt="blabla bla"></a>

depois na página seguia a sugestão do Lfscoutinho, ou seja:

if (isset $_GET['filtro']) {
  $filtro=$_GET['filtro'];
   if ($filtro=="descricao-down")	{
   $sql="a tua query a ordenar por filtro descrição down";
   }
   elseif ($filtro=="descricao-up") {
   $sql="a tua query a ordenar por filtro descrição up";
   }
   elseif ($filtro=="maisbarato") {
   $sql="a tua query a ordenar por preço";
   }
   elseif ($filtro=="maiscaro") {
   $sql="a tua query a ordenar por preço DESC";
   }
   elseif ($filtro=="ref-up") {
   $sql="a tua query a ordenar por ref up";
   }
   elseif ($filtro=="ref-down") {
   $sql="a tua query a ordenar por ref down";
   }
   elseif ($filtro=="marca-down") {
   $sql="a tua query a ordenar por marca down";
   }
   elseif ($filtro=="marca-up") {
   $sql="a tua query a ordenar por marca up";
   }
   else {
   //se não for nada do que é suposto ser, reencaminha para o index (play safe!)
   }
}
//NEXT>>>>
//executa a tua query e mostra os resultados.
//se não quiseres recarregar a página usas AJAX. Há uns quantos tópicos aqui no p@p sobre AJAX

certamente haverá outras formas de resolver a tua questão, mas se percebi bem, era assim que fazia.

ja tentei de varias maneiras e nao funca.

isto é so para ser numa so página, certo.

basicamente o que deveria acontecer é fazer uma pesquisa avançada por vários campos.

e só depois de se fazer a pesquisa é que devem aparecer os filtros para se poder ordenar.

se eu fizer como fizeste "virneto" nao acontece nada pois pesquisavancada.php?filtro=descricao-down nao conhece

vou postar o meu código

pesquisaavancada.php

<?php
mysql_connect("localhost","****","*****") or die ("Impossivel ligar a base de dados");
mysql_select_db("*****");
$pesq=$_POST['pesq'];
$pesq2=$_POST['pesq2'];
$pesq3=$_POST['pesq3'];
$ref=$_POST['ref'];
$mar=$_POST['marca'];
$fam=$_POST['fam'];
$subfam=$_POST['subfam'];
?>
<p align="center" class="style1">Pesquisa Avançada</p>
<form action="" enctype="multipart/form-data" method="post" name="pesqavanc" id="pesqavanc" onsubmit="return verifica(this)">
<table align="center" width="100%">
 <tr>
<td width="14%" class="style1" valign="top" align="right">Descrição :	1<p align="right">2</p><p align="right">3</p>
</td>
<td width="86%" valign="top" class="style1">
<input type="text" name="pesq" id="pesq" size="40" value="<?php echo $pesq ?>" /><br />
<input type="text" name="pesq2" id="pesq2" size="40" value="<?php echo $pesq2 ?>" /><br />
<input type="text" name="pesq3" id="pesq3" size="40" value="<?php echo $pesq3 ?>" /><br />
<br />
 <br />
</td>
 </tr>
 <tr>
<td class="style1">Referência :</td>
<td><input type="text" name="ref" id="ref" size="40" value="<?php echo $ref ?>" /></td>
 </tr>
 <tr>
<td class="style1">Marca :</td>
<td>
<select name="marca" id="marca">
<option value="<?php echo $mar ?>"><?php echo $mar ?></option>
<option value="">-- Seleccione uma marca --</option>

<?php

$sql3 = "SELECT * FROM marcas ORDER BY Nome ASC";
$qr3 = mysql_query($sql3) or die(mysql_error());
while($ln3 = mysql_fetch_assoc($qr3))
 {
?>
<option value="<?php echo $ln3['Nome'] ?>"><?php echo $ln3['Nome'] ?></option>
 <?php
 }
?>
 </select>
 </td>
 </tr>
 <tr>
<td class="style1">Família :</td>
<td>
<?php
$sql = "SELECT * FROM familias ORDER BY Nome";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
for ($i = 0; $i < $num; $i++)
 {
  $dados = mysql_fetch_array($res);
  $arrFams[$dados['Nome']] = $dados['Nome'];
 }
?>
 <select name="fam" id="fam" onchange="buscar_subfams()">
 <option value=""></option>
 <?php
 foreach ($arrFams as $value => $name)
  {
 echo "<option value='{$value}'>{$name}</option>";
}
?>
 </select>


</td>
 </tr>
 <tr>
<td class="style1">SubFamília :</td>
<td>
<div id="load_subfams">
<select name="subfam" id="subfam">
  <option value=""></option>
  </select>
</div>
</td>
 </tr>
 <tr>
<td></td>
<td>
<input name="Reset" type="button" onclick="limpardados()" value="  Reset  " />
<input type="submit" name="Enviar" id="Enviar" value="  Pesquisar  " />
</td>
 </tr>
</table>
</form>

<?php
$pesq=$_POST['pesq'];
$pesq2=$_POST['pesq2'];
$pesq3=$_POST['pesq3'];
$ref=$_POST['ref'];
$mar=$_POST['marca'];
$fam=$_POST['fam'];
$subfam=$_POST['subfam'];

if ($_REQUEST['Enviar'])
{

 //vai buscar pesq.php para fazer a pesquisa e lista os dados nesta página
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
include 'pesq.php';
  }
}

?>

pesq.php


<table align="center" width="100%">
<tr>
 <td colspan="4" align="center" class="style1">
Ordenar por:
	</td>
	</tr>
<tr>
 <td class="style30" valign="top" align="center">
 Descrição<a href="pesquisavancada.php?filtro=descricao-down"><img src="images/arrow-down.png" width="25" height="25" alt="dispor por descrição descendente" /></a>
	<a href="pesquisavancada.php?filtro=descricao-up"><img src="images/arrow-up.png" width="25" height="25" alt="dispor por descrição ascendente" /></a>
	</td>
 <td class="style30" valign="top" align="center">
 Preço
	<a href="pesquisavancada.php?filtro=mais-caro"><img src="images/arrow-down.png" width="25" height="25" alt="dispor por preço descendente" /></a>
	<a href="pesquisavancada.php?filtro=mais-barato"><img src="images/arrow-up.png" width="25" height="25" alt="dispor por preço ascendente" /></a>
	</td>
	<td class="style30" valign="top" align="center">
 Referência
	<a href="pesquisavancada.php?filtro=ref-down"><img src="images/arrow-down.png" width="25" height="25" alt="dispor por referencia descendente" /></a>
	<a href="pesquisavancada.php?filtro=ref-up"><img src="images/arrow-up.png" width="25" height="25" alt="dispor por referencia ascendente" /></a>
	</td>
	<td class="style30" valign="top" align="center">
 Marca
	<a href="pesquisavancada.php?filtro=marca-down"><img src="images/arrow-down.png" width="25" height="25" alt="dispor por marca descendente" /></a>
	<a href="pesquisavancada.php?filtro=marca-up"><img src="images/arrow-up.png" width="25" height="25" alt="dispor por marca ascendente" /></a>
	</td>
</tr>
</table>


<?php
if(isset($_GET['filtro']))
{
 $filtro=$_GET['filtro'];
 if ($filtro=="descricao-down")
  {
$ordenar="ORDER BY DescricaoPT DESC";
  }
 elseif ($filtro=="descricao-up")
  {
$ordenar="ORDER BY DescricaoPT ASC";
  }
 elseif ($filtro=="mais-barato")
  {
$ordenar="ORDER BY PrecoPVP ASC";
  }
 elseif ($filtro=="mais-caro")
  {
$ordenar="ORDER BY PrecoPVP DESC";
  }
 elseif ($filtro=="ref-up")
  {
$ordenar="ORDER BY Referencia ASC";
  }
 elseif ($filtro=="ref-down")
  {
$ordenar="ORDER BY Referencia DESC";
  }
 elseif ($filtro=="marca-down")
  {
$ordenar="ORDER BY Marca DESC";
  }
 elseif ($filtro=="marca-up")
  {
$ordenar="ORDER BY Marca ASC";
  }
 else
  {
//se não for nada do que é suposto ser, reencaminha para o index (play safe!)
  }
}
?>



<div id="conteudo-master">
<div id="conteudo">
<table cellpadding="8" cellspacing="10" width="100%">
<?php
$sql2= "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' '".$ordenar."'";
$result = mysql_query($sql2);
$resultado2=mysql_num_rows($result);
$i = 0;
while ($list = mysql_fetch_array($result))
{
 $imgart=$list['Imagem'];
 $refart=$list['Referencia'];
 $descart=$list['DescricaoPT'];
 $pvpart=$list['PrecoPVP'];
 $revart=$list['PrecoREV'];
 $empart=$list['PrecoEMP'];
 $marcaart=$list['Marca'];
 ++$i;
 echo ($i == 0) ? '<tr>' :null;
 echo "<td align=\"center\" valign=\"top\" width=\"180\" height=\"180\" bgcolor=\"#FFFFFF\">";
 if (!$imgart)
  {
echo "<a href=\"Artigo.php?".$SID."&id=".$refart."\"><img src=\"images/semimagempq.jpg\" title=\"$refart\" alt=\"$refart\" width=\"160\" height=\"120\"/></a>";
echo "<p class=\"style55\" align=\"center\">$descart</p>";
if(isset($_SESSION['login_status']))
 {
  $tipo = ($_SESSION['tipo']);
  if ($tipo == 'ClientePVP')
   {
	 echo "
	   <div id=\"wrap\">
		<div class=\"left\"><img src=\"images/cart2.png\"></div>
		<div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	   </div>
	  ";
   }
  elseif ($tipo == 'Revenda')
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$revart €</p></div>
	 </div>
	 ";
   }
  else
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$empart €</p></div>
	 </div>
	 ";
   }

 }
  else
{
 echo "
   <div id=\"wrap\">
	<div class=\"left\"><img src=\"images/cart2.png\"></div>
	<div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
   </div>
  ";
}

  }
 else
  {
echo "<a href=\"Artigo.php?".$SID."&id=".$refart."\"><img src=\"images/artigos/$imgart\" width=\"160\" height=\"120\" title=\"$refart\" alt=\"$refart\" /></a>";
echo "<p class=\"style55\" align=\"center\">$descart</p>";
if(isset($_SESSION['login_status']))
 {
  $tipo = ($_SESSION['tipo']);
  if ($tipo == 'ClientePVP')
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	 </div>
	 ";
   }
  elseif ($tipo == 'Revenda')
   {
	echo "
	  <div id=\"wrap\">
	   <div class=\"left\"><img src=\"images/cart2.png\"></div>
	   <div class=\"right\"><p class=\"style30\">$revart €</p></div>
	  </div>
	 ";
   }
  else
   {
	echo "
	  <div id=\"wrap\">
	   <div class=\"left\"><img src=\"images/cart2.png\"></div>
	   <div class=\"right\"><p class=\"style30\">$empart €</p></div>
	  </div>
	 ";

   }

 }
else
 {
  echo "
	<div id=\"wrap\">
	 <div class=\"left\"><img src=\"images/cart2.png\"></div>
	 <div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	</div>
   ";
 }
  }

 echo "</td>";
 if ($i ==3)
  {
$i = 0;
echo "</tr>";
  }

}
?>
</table>
</div>
</div>
Edited by JFernandesVR
Posted

Na consulta verifica o seguinte:

$sql2 = "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' '".$ordenar. "'";
No fim da consulta não deveria ser assim:
$sql2= "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' ".$ordenar;
Verifica e vê se assim já funciona.

Abraço,

Rafael.

Posted
Em 9/19/2013 às 11:31, Pivot disse:

Na consulta verifica o seguinte:


$sql2 = "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' '".$ordenar. "'";
No fim da consulta não deveria ser assim:

$sql2= "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' ".$ordenar;
Verifica e vê se assim já funciona.

Abraço,

Rafael.

continua a nao funcionar

Posted (edited)

aparece isto:

SELECT * FROM artigos WHERE DescricaoPT LIKE '%%' AND DescricaoPT LIKE '%%' AND DescricaoPT LIKE '%%' AND Referencia LIKE '%%' AND Marca LIKE '%%' AND FamiliaPT LIKE '%%' AND SubfamiliaPT LIKE '%%'

nao aparece $ordenar

Edited by JFernandesVR
Posted (edited)

tentei fazer doutra maneira e mesmo assim nao funca

<table align="center" width="100%">
<tr>
 <td colspan="4" align="center" class="style1">
	Ordenar por:
			</td>
			</tr>
	<tr>
	 <td class="style30" valign="top" align="center">
 Descrição<input type="image" src="images/arrow-down.png" name="descdesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="descasc" width="25" height="25"/>
			</td>
	 <td class="style30" valign="top" align="center">
 Preço<input type="image" src="images/arrow-down.png" name="precodesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="precoasc" width="25" height="25"/>
			</td>
			<td class="style30" valign="top" align="center">
 Referência<input type="image" src="images/arrow-down.png" name="referdesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="referasc" width="25" height="25"/>
			</td>
			<td class="style30" valign="top" align="center">
 Marca<input type="image" src="images/arrow-down.png" name="marcadesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="marcaasc" width="25" height="25"/>
			</td>
</tr>
</table>

if($filtrar = $_POST['descdesc'] OR $filtrar = $_POST['descasc'] OR $filtrar = $_POST['precodesc'] OR $filtrar = $_POST['precoasc'] OR $filtrar = $_POST['referdesc'] OR $filtrar = $_POST['referasc'] OR $filtrar = $_POST['marcadesc'] OR $filtrar = $_POST['marcaasc'])
{
  switch ($filtrar)
  {
case $filtrar = $_POST['descdesc'];
$ordenar = 'ORDER BY DescricaoPT DESC';
break;
case $filtrar = $_POST['descasc'];
$ordenar = 'ORDER BY DescricaoPT ASC';
break;
case $filtrar = $_POST['precodesc'];
$ordenar = 'ORDER BY PrecoPVP DESC';
break;
case $filtrar = $_POST['precoasc'];
$ordenar = 'ORDER BY PrecoPVP ASC';
break;
case $filtrar = $_POST['referdesc'];
$ordenar = 'ORDER BY Marca DESC';
break;
case $filtrar = $_POST['referasc'];
$ordenar = 'ORDER BY Marca ASC';
break;
case $filtrar = $_POST['referdesc'];
$ordenar = 'ORDER BY Referencia DESC';
break;
case $filtrar = $_POST['referasc'];
$ordenar = 'ORDER BY Referencia ASC';
break;
case $filtrar = '';
$ordenar = "";
break;
  }
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
include 'pesq.php';
  }
}

pesq.php

<div id="conteudo-master">
<div id="conteudo">
<table cellpadding="8" cellspacing="10" width="100%">
<?php
$sql2= "SELECT * FROM artigos WHERE DescricaoPT LIKE '%$pesq%' AND DescricaoPT LIKE '%$pesq2%' AND DescricaoPT LIKE '%$pesq3%' AND Referencia LIKE '%$ref%' AND Marca LIKE '%$mar%' AND FamiliaPT LIKE '%$fam%' AND SubfamiliaPT LIKE '%$subfam%' $ordenar";
$result = mysql_query($sql2);
$resultado2=mysql_num_rows($result);
$i = 0;
while ($list = mysql_fetch_array($result))
{
 $imgart=$list['Imagem'];
 $refart=$list['Referencia'];
 $descart=$list['DescricaoPT'];
 $pvpart=$list['PrecoPVP'];
 $revart=$list['PrecoREV'];
 $empart=$list['PrecoEMP'];
 $marcaart=$list['Marca'];
 ++$i;
 echo ($i == 0) ? '<tr>' :null;
 echo "<td align=\"center\" valign=\"top\" width=\"180\" height=\"180\" bgcolor=\"#FFFFFF\">";
 if (!$imgart)
  {
echo "<a href=\"Artigo.php?".$SID."&id=".$refart."\"><img src=\"images/semimagempq.jpg\" title=\"$refart\" alt=\"$refart\" width=\"160\" height=\"120\"/></a>";
echo "<p class=\"style55\" align=\"center\">$descart</p>";
if(isset($_SESSION['login_status']))
 {
  $tipo = ($_SESSION['tipo']);
  if ($tipo == 'ClientePVP')
   {
	 echo "
	   <div id=\"wrap\">
		<div class=\"left\"><img src=\"images/cart2.png\"></div>
		<div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	   </div>
	  ";
   }
  elseif ($tipo == 'Revenda')
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$revart €</p></div>
	 </div>
	 ";
   }
  else
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$empart €</p></div>
	 </div>
	 ";
   }

 }
  else
{
 echo "
   <div id=\"wrap\">
	<div class=\"left\"><img src=\"images/cart2.png\"></div>
	<div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
   </div>
  ";
}

  }
 else
  {
echo "<a href=\"Artigo.php?".$SID."&id=".$refart."\"><img src=\"images/artigos/$imgart\" width=\"160\" height=\"120\" title=\"$refart\" alt=\"$refart\" /></a>";
echo "<p class=\"style55\" align=\"center\">$descart</p>";
if(isset($_SESSION['login_status']))
 {
  $tipo = ($_SESSION['tipo']);
  if ($tipo == 'ClientePVP')
   {
	echo "
	 <div id=\"wrap\">
	  <div class=\"left\"><img src=\"images/cart2.png\"></div>
	  <div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	 </div>
	 ";
   }
  elseif ($tipo == 'Revenda')
   {
	echo "
	  <div id=\"wrap\">
	   <div class=\"left\"><img src=\"images/cart2.png\"></div>
	   <div class=\"right\"><p class=\"style30\">$revart €</p></div>
	  </div>
	 ";
   }
  else
   {
	echo "
	  <div id=\"wrap\">
	   <div class=\"left\"><img src=\"images/cart2.png\"></div>
	   <div class=\"right\"><p class=\"style30\">$empart €</p></div>
	  </div>
	 ";

   }

 }
else
 {
  echo "
	<div id=\"wrap\">
	 <div class=\"left\"><img src=\"images/cart2.png\"></div>
	 <div class=\"right\"><p class=\"style30\">$pvpart €</p></div>
	</div>
   ";
 }
  }





 echo "</td>";
 if ($i ==3)
  {
$i = 0;
echo "</tr>";
  }

}
?>
</table>
</div>
</div>
Edited by JFernandesVR
Posted (edited)

O switch não deve ser dessa forma...

O switch deverá ser feito assim:

<? switch ($filtrar)
  {
       case $_POST['descdesc']:
        $ordenar = 'ORDER BY DescricaoPT DESC';
        break;
       case $_POST['descasc']:
        $ordenar = 'ORDER BY DescricaoPT ASC';
        break;
       case $_POST['precodesc']:
        $ordenar = 'ORDER BY PrecoPVP DESC';
        break;
       case $_POST['precoasc']:
        $ordenar = 'ORDER BY PrecoPVP ASC';
        break;
       case $_POST['referdesc']:
        $ordenar = 'ORDER BY Marca DESC';
        break;
       case $_POST['referasc']:
        $ordenar = 'ORDER BY Marca ASC';
        break;
       case $_POST['referdesc']:
        $ordenar = 'ORDER BY Referencia DESC';
        break;
       case $_POST['referasc']:
        $ordenar = 'ORDER BY Referencia ASC';
        break;
       case '':
        $ordenar = "";
        break;
  } ?>

Experimenta lá...

Isto é um código básico mas acho que estás a complicar demasiado, mas de qualquer forma experimenta!

Cumps 😉

Edited by Pivot
Posted

O switch não deve ser dessa forma...

O switch deverá ser feito assim:

<? switch ($filtrar)
  {
	case $_POST['descdesc']:
	 $ordenar = 'ORDER BY DescricaoPT DESC';
	 break;
	case $_POST['descasc']:
	 $ordenar = 'ORDER BY DescricaoPT ASC';
	 break;
	case $_POST['precodesc']:
	 $ordenar = 'ORDER BY PrecoPVP DESC';
	 break;
	case $_POST['precoasc']:
	 $ordenar = 'ORDER BY PrecoPVP ASC';
	 break;
	case $_POST['referdesc']:
	 $ordenar = 'ORDER BY Marca DESC';
	 break;
	case $_POST['referasc']:
	 $ordenar = 'ORDER BY Marca ASC';
	 break;
	case $_POST['referdesc']:
	 $ordenar = 'ORDER BY Referencia DESC';
	 break;
	case $_POST['referasc']:
	 $ordenar = 'ORDER BY Referencia ASC';
	 break;
	case '':
	 $ordenar = "";
	 break;
  } ?>

Experimenta lá...

Isto é um código básico mas acho que estás a complicar demasiado, mas de qualquer forma experimenta!

Cumps 😉

nao funciona.

quando carrego num input desaparece tudo

Posted (edited)

o problema aqui nao ta no switch, ao que me parece.

tenho um form, com input type=images

quando carrego numa, é suposto ordenar pelo que escolhio a pesquisa feita anteriormente

estes inputs estao inseridos no form de pesquisa

<table align="center" width="100%">
<tr>
 <td colspan="4" align="center" class="style1">
	Ordenar por:
			</td>
			</tr>
	<tr>
	 <td class="style30" valign="top" align="center">
 Descrição<input type="image" src="images/arrow-down.png" onchange="form.submit()" name="descdesc" width="25" height="25"/><input type="image" onchange="form.submit()" src="images/arrow-up.png" name="descasc" width="25" height="25"/>
			</td>
	 <td class="style30" valign="top" align="center">
 Preço<input type="image" src="images/arrow-down.png" onchange="form.submit()" name="precodesc" width="25" height="25"/><input type="image" onchange="form.submit()" src="images/arrow-up.png" name="precoasc" width="25" height="25"/>
			</td>
			<td class="style30" valign="top" align="center">
 Referência<input type="image" src="images/arrow-down.png" onchange="form.submit()" name="referdesc" width="25" height="25"/><input type="image" onchange="form.submit()" src="images/arrow-up.png" name="referasc" width="25" height="25"/>
			</td>
			<td class="style30" valign="top" align="center">
 Marca<input type="image" src="images/arrow-down.png" onchange="form.submit()" name="marcadesc" width="25" height="25"/><input type="image" onchange="form.submit()" src="images/arrow-up.png" name="marcaasc" width="25" height="25"/>
			</td>
</tr>
</table>
Edited by JFernandesVR
Posted

ja resolvi o problema.

o problema nao estava nos switch ou nas condiçoes.

o problema estavam nos inputs

pus assim e ja funcionou

<table align="center" width="100%">
<tr>
 <td colspan="4" align="center" class="style1">
    Ordenar por:
		    </td>
		    </tr>
    <tr>
	 <td class="style30" valign="top" align="center">
	 Descrição<input type="image" src="images/arrow-down.png" value="descdesc" name="descdesc" alt="descdesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="descasc" value="descasc" alt="descasc" width="25" height="25"/>
		    </td>
	 <td class="style30" valign="top" align="center">
 Preço<input type="image" src="images/arrow-down.png" name="precodesc" value="precodesc" alt="precodesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" name="precoasc" value="precoasc" alt="precoasc" width="25" height="25"/>
		    </td>
		    <td class="style30" valign="top" align="center">
 Referência<input type="image" src="images/arrow-down.png" name="referdesc" value="referdesc" alt="referdesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" value="referasc" alt="referasc" name="referasc" width="25" height="25"/>
		    </td>
		    <td class="style30" valign="top" align="center">
 Marca<input type="image" src="images/arrow-down.png" name="marcadesc" value="marcadesc" alt="marcadesc" width="25" height="25"/><input type="image" src="images/arrow-up.png" value="marcaasc" alt="marcaasc" name="marcaasc" width="25" height="25"/>
	  </td>
</tr>
</table>

$pesq=$_POST['pesq'];
$pesq2=$_POST['pesq2'];
$pesq3=$_POST['pesq3'];
$ref=$_POST['ref'];
$mar=$_POST['marca'];
$fam=$_POST['fam'];
$subfam=$_POST['subfam'];

if(isset($_POST['descdesc']))
{
 $ordenar = 'ORDER BY DescricaoPT DESC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['descasc']))
{
 $ordenar = 'ORDER BY DescricaoPT ASC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['precodesc']))
{
 $ordenar = 'ORDER BY PrecoPVP DESC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['precoasc']))
{
 $ordenar = 'ORDER BY PrecoPVP ASC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['referdesc']))
{
 $ordenar = 'ORDER BY Referencia DESC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['referasc']))
{
 $ordenar = 'ORDER BY Referencia ASC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['marcadesc']))
{
 $ordenar = 'ORDER BY Marca DESC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
if(isset($_POST['marcaasc']))
{
 $ordenar = 'ORDER BY Marca ASC';
 //pesquisa campo 1, 2 e referencia
 if (isset($pesq, $pesq2, $pesq3, $ref, $mar, $fam, $subfam))
  {
   include 'pesq.php';
  }
}
Posted

Boas,

O essencial não é resolver os problemas, é entender a sua resolução.

Vou colocar duas soluções mais completas para o caso de teres interesse em perceber.

$filtro = isSet( $_GET['filtro'] ) ? $_GET['filtro'] : '';
$order  = isSet( $_GET['order'] ) ? $_GET['order'] : '';

$sql	= "SELECT * FROM produtos";

if( $filtro ) {

$sql .= ' ORDER BY ';
switch( $filtro ) {
	case 'nome' :
		$sql .= 'campo_nome';
			break;
	case 'desc' :
		$sql .= 'campo_desc';
		break;
	default	 :
		$sql  .= 'campo_nome';
		break;
}

if( $order == 'desc' ) {

	$sql .= ' DESC';

} else {

	$sql .= ' ASC';

}

}

$filtro = isSet( $_GET['filtro'] ) ? $_GET['filtro'] : '';
$filtro = explode( '-', $filtro );


$sql	= "SELECT * FROM produtos";

if( $filtro ) {

$sql .= ' ORDER BY ';
switch( $filtro[0] ) {
	case 'nome' :
		$sql .= 'campo_nome';
			break;
	case 'desc' :
		$sql .= 'campo_desc';
		break;
	default	 :
		$sql  .= 'campo_nome';
		break;
}

if( isSet( $filtro[1] ) && $filtro[1] == 'desc' ) {

	$sql .= ' DESC';

} else {

	$sql .= ' ASC';

}

}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.