<?php
function amoneda($numero, $moneda){
$punto = substr($numero, -1,1); $punto2 = substr($numero, 0,1); $separador = ".";
if($punto == "."){
$numero = substr($numero, 0,$longitud-1); }
if($punto2 == "."){
$numero = "0".$numero;
}
$num_entero = strpos ($numero, $separador); $centavos = substr ($numero, ($num_entero)); if($l_cent == 2){$centavos = $centavos."0";}
elseif($l_cent == 3){$centavos = $centavos;}
elseif($l_cent > 3){$centavos = substr($centavos, 0,3);} $entero = substr($numero, -$longitud,$longitud-$l_cent); if(!$num_entero){
$num_entero = $longitud;
$centavos = ".00";
$entero = substr($numero, -$longitud,$longitud); }
$start = floor($num_entero/3); $res = $num_entero-($start*3);
if($res == 0){$coma = $start-1; $init = 0;}else{$coma = $start; $init = 3-$res;}
$d= $init; $i = 0; $c = $coma;
while($i <= $num_entero){
if($d == 3 && $c > 0){$d = 0; $sep = ","; $c = $c-1;}else{$sep = "";}
$final .= $sep.$entero[$i];
$i = $i+1; // todos los digitos
$d = $d+1; // poner las comas
}
if($moneda == "pesos") {$moneda = "$";
return $moneda." ".$final.$centavos;
}
elseif($moneda == "dolares"){$moneda = "USD";
return $moneda." ".$final.$centavos;
}
elseif($moneda == "euros") {$moneda = "EUR";
return $final.$centavos." ".$moneda;
}
}
?>
<link href="../css/paginacion.css" type="text/css" rel="stylesheet">
<?
include('../config/db.php');
$conn=get_db_conn();
//AL PRINCIPIO COMPRUEBO SI HICIERON CLICK EN ALGUNA PÁGINA
if(isset($_GET['page'])){ $page= $_GET['page'];
}else{
//SI NO DIGO Q ES LA PRIMERA PÁGINA
$page=1;
}
//ACA SE SELECCIONAN TODOS LOS DATOS DE LA TABLA
if($_GET)
{
//verifico si se envio la variable y creo un filtro
if(isset($_GET['marca'])) {
$marca=$_GET['marca'];
{
$filtro= "marca LIKE '%$marca%'";
}
}
//idem
if(isset($_GET['categoria'])) {
$categoria=$_GET['categoria'];
//si no fue enviada vacia
{
//verifico si filtro esta vacio
if($filtro=="")
{
//si esta vacio, genero un like independiente
$filtro= "categoria LIKE '%$categoria%'";
}
else
{
//caso contrario uno el like de marca con este
$filtro.= "OR categoria LIKE '%$categoria%'";
}
}
}
//idem al anterior
{
$key=$_GET['key'];
{
if($filtro=="")
{
$filtro= "descripcion LIKE '%$key%'";
}
else
{
$filtro.= "OR descripcion LIKE '%$key%'";
}
}
}
//genero un consulta y le agrego el resultado del $filtro
//siempre que no este vacio
if(!$filtro=="")
{
$consulta = "SELECT * FROM productos WHERE $filtro";
//saber cuantos elementos hay o fueron encontrados
}
}
else
{
echo "debes enviar una consulta para activar el buscador";
}
//MIRO CUANTOS DATOS FUERON DEVUELTOS
//ACA SE DECIDE CUANTOS RESULTADOS MOSTRAR POR PÁGINA , EN EL EJEMPLO PONGO 15
$rows_per_page= 9;
//CALCULO LA ULTIMA PÁGINA
$lastpage= ceil($num_rows / $rows_per_page);
//COMPRUEBO QUE EL VALOR DE LA PÁGINA SEA CORRECTO Y SI ES LA ULTIMA PÁGINA
$page=(int)$page;
if($page > $lastpage){
$page= $lastpage;
}
if($page < 1){
$page=1;
}
//CREO LA SENTENCIA LIMIT PARA AÑADIR A LA CONSULTA QUE DEFINITIVA
$limit= 'LIMIT '. ($page -1) * $rows_per_page . ', ' .$rows_per_page;
//REALIZO LA CONSULTA QUE VA A MOSTRAR LOS DATOS (ES LA ANTERIO + EL $limit)
$consulta .=" $limit";
if(!$peliculas){
//SI FALLA LA CONSULTA MUESTRO ERROR
echo $consulta;
}else{
//SI ES CORRECTA MUESTRO LOS DATOS
?> <p> </p><div style="width:660px;">
?>
<div style="width:215px; float:left; margin:2px; height:130px ">
<table width="100%" cellspacing="0" style="border:dashed #CCCCCC 1px; font-family: Verdana, Geneva, sans-serif; ">
<tr>
<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>
<td height="110" align="center" style="font-family:Arial, Helvetica, sans-serif font-size:9px; color:#666;"><? echo $row['descripcion']; ?>
<p style="color:#2e6ab1"> <? echo amoneda($row['precio'], pesos) ; ?> MXN <br />
<a href="details.php?id=<? echo $row['id']; ?>"><img src="../images/masdetails.png" width="93" height="19" border="0" /></a></p></td>
</tr>
</table>
</div>
<? } ?>
</div>
<p> </p>
<table width="300" border="0" align="center">
<tr>
<td align="center"><?
//UNA VEZ Q MUESTRO LOS DATOS TENGO Q MOSTRAR EL BLOQUE DE PAGINACIÓN SIEMPRE Y CUANDO HAYA MÁS DE UNA PÁGINA
if($num_rows!=0){
$nextpage= $page +1;
$prevpage= $page -1;
?><ul id="pagination-digg"><?
//SI ES LA PRIMERA PÁGINA DESHABILITO EL BOTON DE PREVIOUS, MUESTRO EL 1 COMO ACTIVO Y MUESTRO EL RESTO DE PÁGINAS
if ($page == 1) {
?>
<li class="previous-off">« Anterior</li>
<li class="active">1</li> <?
for($i= $page+1; $i<= $lastpage ; $i++){?>
<li><a href="index.php?page=<? echo $i;?>"><? echo $i;?></a></li>
<? }
//Y SI LA ULTIMA PÁGINA ES MAYOR QUE LA ACTUAL MUESTRO EL BOTON NEXT O LO DESHABILITO
if($lastpage >$page ){?>
<li class="next"><a href="index.php?page=<? echo $nextpage;?>" >Siguiente »</a></li><?
}else{?>
<li class="next-off">Siguiente »</li>
<? }
} else {
?>
<li class="previous"><a href="index.php?page=<? echo $prevpage;?>" >« Anterior</a></li><?
for($i= 1; $i<= $lastpage ; $i++){
//COMPRUEBO SI ES LA PÁGINA ACTIVA O NO
if($page == $i){
?> <li class="active"><? echo $i;?></li><?
}else{
?> <li><a href="index.php?page=<? echo $i;?>" ><? echo $i;?></a></li><?
}
}
//SI NO ES LA ÚLTIMA PÁGINA ACTIVO EL BOTON NEXT
if($lastpage >$page ){ ?>
<li class="next"><a href="index.php?page=<? echo $nextpage;?>">Siguiente »</a></li><?
}else{
?> <li class="next-off">Siguiente »</li><?
}
}
?></ul></div><?
}
}
?></td>
</tr>
</table>