Ver Mensaje Individual
  #9 (permalink)  
Antiguo 26/01/2012, 13:59
jyopi
 
Fecha de Ingreso: enero-2004
Ubicación: Ocotlan
Mensajes: 57
Antigüedad: 20 años, 4 meses
Puntos: 2
Respuesta: Consultas para hacer motor de busqueda

Te paso el codigo completo y te explico cual es el problema...

Código PHP:
Ver original
  1. <?php
  2. function amoneda($numero, $moneda){  
  3. $longitud = strlen($numero);  
  4. $punto = substr($numero, -1,1);  
  5. $punto2 = substr($numero, 0,1);  
  6. $separador = ".";  
  7. if($punto == "."){  
  8. $numero = substr($numero, 0,$longitud-1);  
  9. $longitud = strlen($numero);  
  10. }  
  11. if($punto2 == "."){  
  12. $numero = "0".$numero;  
  13. $longitud = strlen($numero);  
  14. }  
  15. $num_entero = strpos ($numero, $separador);  
  16. $centavos = substr ($numero, ($num_entero));  
  17. $l_cent = strlen($centavos);  
  18. if($l_cent == 2){$centavos = $centavos."0";}  
  19. elseif($l_cent == 3){$centavos = $centavos;}  
  20. elseif($l_cent > 3){$centavos = substr($centavos, 0,3);}  
  21. $entero = substr($numero, -$longitud,$longitud-$l_cent);  
  22. if(!$num_entero){  
  23.     $num_entero = $longitud;  
  24.     $centavos = ".00";  
  25.     $entero = substr($numero, -$longitud,$longitud);  
  26. }  
  27.  
  28. $start = floor($num_entero/3);  
  29. $res = $num_entero-($start*3);  
  30. if($res == 0){$coma = $start-1; $init = 0;}else{$coma = $start; $init = 3-$res;}  
  31. $d= $init; $i = 0; $c = $coma;  
  32.     while($i <= $num_entero){  
  33.         if($d == 3 && $c > 0){$d = 0; $sep = ","; $c = $c-1;}else{$sep = "";}  
  34.         $final .=  $sep.$entero[$i];  
  35.         $i = $i+1; // todos los digitos  
  36.         $d = $d+1; // poner las comas  
  37.     }  
  38.     if($moneda == "pesos")  {$moneda = "$";  
  39.     return $moneda." ".$final.$centavos;  
  40.     }  
  41.     elseif($moneda == "dolares"){$moneda = "USD";  
  42.     return $moneda." ".$final.$centavos;  
  43.     }  
  44.     elseif($moneda == "euros")  {$moneda = "EUR";  
  45.     return $final.$centavos." ".$moneda;  
  46.     }  
  47. }
  48. ?>
  49. <link href="../css/paginacion.css" type="text/css" rel="stylesheet">
  50. <?
  51. include('../config/db.php');
  52. $conn=get_db_conn();
  53.  
  54. //AL PRINCIPIO COMPRUEBO SI HICIERON CLICK EN ALGUNA PÁGINA
  55. if(isset($_GET['page'])){
  56.     $page= $_GET['page'];
  57. }else{
  58. //SI NO DIGO Q ES LA PRIMERA PÁGINA
  59.     $page=1;
  60. }
  61.  
  62. //ACA SE SELECCIONAN TODOS LOS DATOS DE LA TABLA
  63. if($_GET)
  64. {
  65.     //verifico si se envio la variable y creo un filtro
  66.     if(isset($_GET['marca']))
  67.     {
  68.         $marca=$_GET['marca'];
  69.        
  70.         if(!empty($marca))
  71.         {
  72.                 $filtro= "marca LIKE '%$marca%'";    
  73.         }
  74.  
  75.     }
  76.    
  77.    
  78.     //idem
  79.     if(isset($_GET['categoria']))
  80.     {
  81.         $categoria=$_GET['categoria'];
  82.        
  83.         //si no fue enviada vacia
  84.         if(!empty($categoria))
  85.         {
  86.             //verifico si filtro esta vacio
  87.             if($filtro=="")
  88.             {
  89.                 //si esta vacio, genero un like independiente
  90.                 $filtro= "categoria LIKE '%$categoria%'";
  91.             }
  92.             else
  93.             {
  94.                 //caso contrario uno el like de marca con este
  95.                 $filtro.= "OR categoria LIKE '%$categoria%'";
  96.             }
  97.         }
  98.  
  99.     }
  100.    
  101.    
  102.     //idem al anterior
  103.     if(isset($_GET['key']))
  104.     {
  105.         $key=$_GET['key'];
  106.        
  107.         if(!empty($key))
  108.         {
  109.             if($filtro=="")
  110.             {
  111.                 $filtro= "descripcion LIKE '%$key%'";
  112.             }
  113.             else
  114.             {
  115.                 $filtro.= "OR descripcion LIKE '%$key%'";
  116.             }
  117.         }
  118.  
  119.     }
  120.    
  121.    
  122.    
  123.     //genero un consulta y le agrego el resultado del $filtro
  124.     //siempre que no este vacio
  125.     if(!$filtro=="")
  126.     {
  127.         $consulta = "SELECT * FROM productos WHERE $filtro";
  128.        
  129.        $datos=mysql_query($consulta,$conn);
  130.        
  131.        
  132.         //saber cuantos elementos hay o fueron encontrados
  133.         $num= mysql_num_rows($datos);
  134.     }
  135.    
  136. }
  137. else
  138. {
  139.     echo "debes enviar una consulta para activar el buscador";
  140. }
  141.  
  142.  
  143.  
  144.  
  145. //MIRO CUANTOS DATOS FUERON DEVUELTOS
  146. $num_rows=mysql_num_rows($datos);
  147.  
  148. //ACA SE DECIDE CUANTOS RESULTADOS MOSTRAR POR PÁGINA , EN EL EJEMPLO PONGO 15
  149. $rows_per_page= 9;
  150.  
  151. //CALCULO LA ULTIMA PÁGINA
  152. $lastpage= ceil($num_rows / $rows_per_page);
  153.  
  154. //COMPRUEBO QUE EL VALOR DE LA PÁGINA SEA CORRECTO Y SI ES LA ULTIMA PÁGINA
  155. $page=(int)$page;
  156. if($page > $lastpage){
  157.     $page= $lastpage;
  158. }
  159. if($page < 1){
  160.     $page=1;
  161. }
  162.  
  163. //CREO LA SENTENCIA LIMIT PARA AÑADIR A LA CONSULTA QUE DEFINITIVA
  164. $limit= 'LIMIT '. ($page -1) * $rows_per_page . ', ' .$rows_per_page;
  165.  
  166. //REALIZO LA CONSULTA QUE VA A MOSTRAR LOS DATOS (ES LA ANTERIO + EL $limit)
  167. $consulta .=" $limit";
  168. $peliculas=mysql_query($consulta,$conn);
  169.  
  170. if(!$peliculas){
  171.         //SI FALLA LA CONSULTA MUESTRO ERROR
  172.         echo $consulta;
  173.  die('Invalid query: ' . mysql_error());
  174. }else{
  175.       //SI ES CORRECTA MUESTRO LOS DATOS
  176.       ?> <p>&nbsp;</p><div style="width:660px;">
  177.       <? while($row = mysql_fetch_assoc($peliculas)) {  
  178.      
  179.       list($ancho, $altura, $tipo, $atr) = getimagesize('images/'.$row['imagen']);
  180.      
  181.       ?>
  182.       <div style="width:215px; float:left; margin:2px; height:130px ">
  183.       <table width="100%"  cellspacing="0" style="border:dashed #CCCCCC 1px;  font-family: Verdana, Geneva, sans-serif; ">
  184.   <tr>
  185.     <td width="100"><a href="images/<? echo $row['imagen']; ?>" id="example5" title="<? echo $row['descripcion']; ?>"><img src="images/<? echo $row['imagen']; ?>" alt="example5"  <?php if ($altura > $ancho) { echo 'height="100" ';  } else { echo 'width="100"'; }?> border="0" /></a></td>
  186.     <td height="110" align="center" style="font-family:Arial, Helvetica, sans-serif font-size:9px; color:#666;"><? echo $row['descripcion']; ?>
  187.       <p style="color:#2e6ab1">&nbsp;<? echo amoneda($row['precio'], pesos) ; ?> MXN <br />
  188.         <a href="details.php?id=<? echo $row['id']; ?>"><img src="../images/masdetails.png" width="93" height="19" border="0" /></a></p></td>
  189.   </tr>
  190. </table>
  191. </div>
  192. <?  } ?>      
  193.       </div>
  194.       <p>&nbsp;</p>
  195.       <table width="300" border="0" align="center">
  196.         <tr>
  197.           <td align="center"><?
  198. //UNA VEZ Q MUESTRO LOS DATOS TENGO Q MOSTRAR EL BLOQUE DE PAGINACIÓN SIEMPRE Y CUANDO HAYA MÁS DE UNA PÁGINA
  199.  
  200. if($num_rows!=0){
  201.    $nextpage= $page +1;
  202.    $prevpage= $page -1;
  203. ?><ul id="pagination-digg"><?
  204. //SI ES LA PRIMERA PÁGINA DESHABILITO EL BOTON DE PREVIOUS, MUESTRO EL 1 COMO ACTIVO Y MUESTRO EL RESTO DE PÁGINAS
  205.  if ($page == 1) {
  206.     ?>
  207.       <li class="previous-off">&laquo; Anterior</li>
  208.       <li class="active">1</li> <?
  209.     for($i= $page+1; $i<= $lastpage ; $i++){?>
  210.             <li><a href="index.php?page=<? echo $i;?>"><? echo $i;?></a></li>
  211.  <? }
  212.        //Y SI LA ULTIMA PÁGINA ES MAYOR QUE LA ACTUAL MUESTRO EL BOTON NEXT O LO DESHABILITO
  213.     if($lastpage >$page ){?>      
  214.       <li class="next"><a href="index.php?page=<? echo $nextpage;?>" >Siguiente &raquo;</a></li><?
  215.     }else{?>
  216.       <li class="next-off">Siguiente &raquo;</li>
  217. <?  }
  218.  } else {
  219.     ?>
  220.    
  221.       <li class="previous"><a href="index.php?page=<? echo $prevpage;?>"  >&laquo; Anterior</a></li><?
  222.       for($i= 1; $i<= $lastpage ; $i++){
  223.                        //COMPRUEBO SI ES LA PÁGINA ACTIVA O NO
  224.             if($page == $i){
  225.         ?>  <li class="active"><? echo $i;?></li><?
  226.             }else{
  227.         ?>  <li><a href="index.php?page=<? echo $i;?>" ><? echo $i;?></a></li><?
  228.             }
  229.       }
  230.          //SI NO ES LA ÚLTIMA PÁGINA ACTIVO EL BOTON NEXT    
  231.       if($lastpage >$page ){    ?>
  232.       <li class="next"><a href="index.php?page=<? echo $nextpage;?>">Siguiente &raquo;</a></li><?
  233.       }else{
  234.     ?> <li class="next-off">Siguiente &raquo;</li><?
  235.       }
  236.  }  
  237. ?></ul></div><?
  238. }
  239.  }  
  240. ?></td>
  241.         </tr>
  242.       </table>

si la url no tiene definido ningun valor... osea http://digitalhome.mx/productos/
me da este error
Código:
debes enviar una consulta para activar el buscador
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/digi2709/public_html/productos/busquedas.php on line 146
LIMIT 0, 9Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 0, 9' at line 1
y el otro problema que tengo es al combinar la busqueda.... por ejemplo si mando valor de marca y categoria solo toma en cuenta un valor... agradezco si me pueden ayudar