Paginador en PHP

Hola a todos, alguién sabe com hacer un páginador en PHP
De ante mano muchas gracias.
Atte.
nagg

1 Respuesta

Respuesta
1
Manda de nuevo la pregunta porque no te pude mandar la respuesta.
Hi,
Mira yo tengo uno hecho que funciona muy bien. Lo unico que vas a tener que hacer es examinar el codigo y sacar las cosas que no te sirven, yo ya te saque mucho codigo HTML que no te iba a servir (puede haber errores en HTML pero en PHP no).
La estetica la manejas vos a traves del HTML y la cantidad de paginas cambia automaticamente dependiendo la cantidad de registros de la consulta. Hay unas variables (alturadelinea, tablerows, etc) que yo las utilizo para mostrar imagenes de distinto tamaño lo que creo que a vos no te va a servir.
La consulta esta hecha en MySQL pero se puede utilizar a cualquier motor de base de datos ya que las consultas por lo general se guardan en Arrays y de ahi se utiliza un While.
Pienso que te conviene mirar el HTML para orientarte y despues generar el tuyo propio.
El script funciona ya sea en PHP version 3 o 4.
Espero que te sirva el script y cualquier consulta que tengas no dudes en preguntarme.
**************************************************
********EMPIEZA EL SCRIPT *************************
**************************************************
<html>
<head>
<title>Busqueda de Productos - ComproBien.com</title>
<style type="text/css">
<!--
.texto { font-family: Arial, Helvetica, sans-serif; font-size: 12px; text-decoration: none;}
.texton { font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; text-decoration: none;}
.titulo { font-family: Arial, sans-serif; font-size: 15px; font-weight: bold; text-decoration: none;}
.envio { font-family: Arial, sans-serif; font-size: 10px; color: #ff0000; font-weight: bold; text-decoration: none;}
.dato { font-family: Arial, sans-serif; font-size: 13px; font-weight: bold; text-decoration: none;}
.titulito { font-family: Arial, sans-serif; font-size: 3px; font-weight: bold; text-decoration: none;}
.produtit { font-family: Arial, sans-serif; font-size: 12px; font-weight: bold; text-decoration: none;}
.produtitsf { font-family: Arial, sans-serif; font-size: 13.5px; font-weight: bold; text-decoration: none;}
.descrip { font-family: Arial, Helvetica, sans-serif; font-size: 11px; text-decoration: none;}
.precioc { font-family: Arial, Helvetica, sans-serif; font-size: 12.8px; font-weight: bold; text-decoration: none;}
.preciog { font-family: Arial, Helvetica, sans-serif; font-size: 18.0px; font-weight: bold; text-decoration: none;}
-->
</style>
<script languaje = "JavaScript">
<!--
browserVer = parseInt(navigator.appVersion);
// if (browserVer >= 3) version = "n3"
if (browserVer >= 3) version = "n3"
else version = "noScript";
exrubro = "";
if (version == "n3") {
menum0dn = new Image(); menum0dn.src = "../images/home/ofertas2.gif";
menum0up = new Image(); menum0up.src = "../images/home/ofertas1.gif";
menum1dn = new Image(); menum1dn.src = "../images/home/productos2.gif";
menum1up = new Image(); menum1up.src = "../images/home/productos1.gif";
menum2dn = new Image(); menum2dn.src = "../images/home/negocios2.gif";
menum2up = new Image(); menum2up.src = "../images/home/negocios1.gif";
menum3dn = new Image(); menum3dn.src = "../images/home/servicios2.gif";
menum3up = new Image(); menum3up.src = "../images/home/servicios1.gif";
menum4dn = new Image(); menum4dn.src = "../images/home/registrese2.gif";
menum4up = new Image(); menum4up.src = "../images/home/registrese1.gif";
menum5dn = new Image(); menum5dn.src = "../images/home/carrito2.gif";
menum5up = new Image(); menum5up.src = "../images/home/carrito1.gif";
menut0dn = new Image(); menut0dn.src = "../images/home/b_buscar2.gif";
menut0up = new Image(); menut0up.src = "../images/home/b_buscar1.gif";
menut1dn = new Image(); menut1dn.src = "../images/home/b_anterior2.gif";
menut1up = new Image(); menut1up.src = "../images/home/b_anterior1.gif";
menut2dn = new Image(); menut2dn.src = "../images/home/b_proxima2.gif";
menut2up = new Image(); menut2up.src = "../images/home/b_proxima1.gif";
menut3dn = new Image(); menut3dn.src = "../images/home/b_anterior2.gif";
menut3up = new Image(); menut3up.src = "../images/home/b_anterior1.gif";
menut4dn = new Image(); menut4dn.src = "../images/home/b_proxima2.gif";
menut4up = new Image(); menut4up.src = "../images/home/b_proxima1.gif";
menub0dn = new Image(); menub0dn.src = "../images/rubros/bodega2.gif";
menub0up = new Image(); menub0up.src = "../images/rubros/bodega1.gif";
menub0me = new Image(); menub0me.src = "../images/rubros/bodega0.gif";
menub1dn = new Image(); menub1dn.src = "../images/rubros/champa2.gif";
menub1up = new Image(); menub1up.src = "../images/rubros/champa1.gif";
menub2dn = new Image(); menub2dn.src = "../images/rubros/vinos2.gif";
menub2up = new Image(); menub2up.src = "../images/rubros/vinos1.gif";
menub3dn = new Image(); menub3dn.src = "../images/rubros/otros2.gif";
menub3up = new Image(); menub3up.src = "../images/rubros/otros1.gif";
menuz0dn = new Image(); menuz0dn.src = "../images/rubros/belle2.gif";
menuz0up = new Image(); menuz0up.src = "../images/rubros/belle1.gif";
menuz0me = new Image(); menuz0me.src = "../images/rubros/belle0.gif";
menuz1dn = new Image(); menuz1dn.src = "../images/rubros/cosme2.gif";
menuz1up = new Image(); menuz1up.src = "../images/rubros/cosme1.gif";
menuz2dn = new Image(); menuz2dn.src = "../images/rubros/perfu2.gif";
menuz2up = new Image(); menuz2up.src = "../images/rubros/perfu1.gif";
menuz3dn = new Image(); menuz3dn.src = "../images/rubros/otros2.gif";
menuz3up = new Image(); menuz3up.src = "../images/rubros/otros1.gif";
menue0dn = new Image(); menue0dn.src = "../images/rubros/electro2.gif";
menue0up = new Image(); menue0up.src = "../images/rubros/electro1.gif";
menue0me = new Image(); menue0me.src = "../images/rubros/electro0.gif";
menue1dn = new Image(); menue1dn.src = "../images/rubros/microo2.gif";
menue1up = new Image(); menue1up.src = "../images/rubros/microo1.gif";
menue2dn = new Image(); menue2dn.src = "../images/rubros/minicom2.gif";
menue2up = new Image(); menue2up.src = "../images/rubros/minicom1.gif";
menue3dn = new Image(); menue3dn.src = "../images/rubros/helade2.gif";
menue3up = new Image(); menue3up.src = "../images/rubros/helade1.gif";
menue4dn = new Image(); menue4dn.src = "../images/rubros/televi2.gif";
menue4up = new Image(); menue4up.src = "../images/rubros/televi1.gif";
menue5dn = new Image(); menue5dn.src = "../images/rubros/otros2.gif";
menue5up = new Image(); menue5up.src = "../images/rubros/otros1.gif";
menuh0dn = new Image(); menuh0dn.src = "../images/rubros/hogar2.gif";
menuh0up = new Image(); menuh0up.src = "../images/rubros/hogar1.gif";
menuh0me = new Image(); menuh0me.src = "../images/rubros/hogar0.gif";
menuh1dn = new Image(); menuh1dn.src = "../images/rubros/decora2.gif";
menuh1up = new Image(); menuh1up.src = "../images/rubros/decora1.gif";
menuh2dn = new Image(); menuh2dn.src = "../images/rubros/ferre2.gif";
menuh2up = new Image(); menuh2up.src = "../images/rubros/ferre1.gif";
menuh3dn = new Image(); menuh3dn.src = "../images/rubros/jardi2.gif";
menuh3up = new Image(); menuh3up.src = "../images/rubros/jardi1.gif";
menuh4dn = new Image(); menuh4dn.src = "../images/rubros/otros2.gif";
menuh4up = new Image(); menuh4up.src = "../images/rubros/otros1.gif";
menuu0dn = new Image(); menuu0dn.src = "../images/rubros/musica2.gif";
menuu0up = new Image(); menuu0up.src = "../images/rubros/musica1.gif";
menuu0me = new Image(); menuu0me.src = "../images/rubros/musica0.gif";
menuu1dn = new Image(); menuu1dn.src = "../images/rubros/cds2.gif";
menuu1up = new Image(); menuu1up.src = "../images/rubros/cds1.gif";
menuu2dn = new Image(); menuu2dn.src = "../images/rubros/combos2.gif";
menuu2up = new Image(); menuu2up.src = "../images/rubros/combos1.gif";
menuu3dn = new Image(); menuu3dn.src = "../images/rubros/equipo2.gif";
menuu3up = new Image(); menuu3up.src = "../images/rubros/equipo1.gif";
menuu4dn = new Image(); menuu4dn.src = "../images/rubros/otros2.gif";
menuu4up = new Image(); menuu4up.src = "../images/rubros/otros1.gif";
menur0dn = new Image(); menur0dn.src = "../images/rubros/repues2.gif";
menur0up = new Image(); menur0up.src = "../images/rubros/repues1.gif";
menur0me = new Image(); menur0me.src = "../images/rubros/repues0.gif";
menur1dn = new Image(); menur1dn.src = "../images/rubros/autos2.gif";
menur1up = new Image(); menur1up.src = "../images/rubros/autos1.gif";
menur2dn = new Image(); menur2dn.src = "../images/rubros/maqui2.gif";
menur2up = new Image(); menur2up.src = "../images/rubros/maqui1.gif";
menur3dn = new Image(); menur3dn.src = "../images/rubros/motos2.gif";
menur3up = new Image(); menur3up.src = "../images/rubros/motos1.gif";
menur4dn = new Image(); menur4dn.src = "../images/rubros/otros2.gif";
menur4up = new Image(); menur4up.src = "../images/rubros/otros1.gif";
}
function poner(menu, rubro) {
if (exrubro != rubro) {
if (exrubro != "") fuera(exrubro);
imgMe = eval("menu" + rubro + "me.src");
document [rubro].src = imgMe;
exrubro = rubro;
}
arriba(menu);
}
function sacar(menu) {
fuera(exrubro);
fuera(menu);
exrubro = "";
}
function arriba(menu) {
if (version == "n3") {
imgDn = eval("menu" + menu + "dn.src");
document [menu].src = imgDn;
}
}
function fuera(menu) {
if (version == "n3") {
imgUp = eval("menu" + menu + "up.src");
document [menu].src = imgUp;
}
}
function checkvacios() {
if ((document.formu.producto.value == '') && (document.formu.rubro.selectedIndex == 0)) {
alert('Se han encontrado demasiados productos. Seleccione alguna de las opciones de busqueda e ingrese el Nombre del Producto.\n');
document.formu.producto.focus();
} else document.formu.submit();
}
//-->
</script>
</head>
<body marginwidth=0 marginheight=0 leftmargin=0 topmargin=0 rightmargin=0 bgcolor=009191 text=000000 link=000000 vlink=000000>
<!-- *************************************************************************************************************** -->
<!-- ***************************************** PAGINA PRINCIPAL *********************************************** -->
<!-- *************************************************************************************************************** -->
<?
$host = "localhost";
$user = "co1079";
$pass = "password";
$database = $user;
/* ##################################################################### */
mysql_connect($host,$user,$pass);
$numrows = 0;
if ($primera != 'si') {
if (strlen($precio) == 0) {
$mayora = intval(substr($precio, 0, 3));
$menora = intval(substr($precio, 3, 3));
if ($menora == 999) {
$menora = 1000000;
};
} else if (strlen($precio) == 6) {
$mayora = intval(substr($precio, 0, 3));
$menora = intval(substr($precio, 3, 3));
} else if (strlen($precio) == 7) {
$mayora = intval(substr($precio, 0, 3));
$menora = intval(substr($precio, 3, 3));
if ($menora == 999) {
$menora = 1000000;
};
};
if ($rubro == "") {
$resultpro = mysql($database,"select * from Productos where NOMBRE like '%$producto%' AND PRECIO >= $mayora AND PRECIO <= $menora AND STOCK > 0 ORDER BY NOMBRE");
} else {
$resultpro = mysql($database,"select * from Productos where NOMBRE like '%$producto%' AND RUBRO=$rubro AND PRECIO >= $mayora AND PRECIO <= $menora AND STOCK > 0 ORDER BY NOMBRE");
};
$numrows = mysql_num_rows($resultpro);
};
$resultrub = mysql($database,"select * from Rubros");
$numrowsrub = mysql_num_rows($resultrub);
if ($numrows >= 1) {
// Do the actual Query;
/* ######################################################
###################################################### */
$tablerows = $numrows + 3;
$alturalinea = ($numrows * 120) + 25;
$paginaprox = "<font class=\"titulito\"><br></font>";
$paginaante = "<font class=\"titulito\"><br></font>";
if ($pagina != 1) {
$anterior = $pagina - 1;
$paginaante = "<a href=\"/phpfiles/buscarproductos.php3?pagina=$anterior&producto=$producto&rubro=$rubro&precio=$precio\" onMouseOver=\"arriba('t1');window.status='Pagina Anterior';return true\" onMouseOut=\"fuera('t1');return true\">
<img name=\"t1\" src=\"../images/home/b_anterior1.gif\" align=top border=0></a>";
$paginaantea = "<a href=\"/phpfiles/buscarproductos.php3?pagina=$anterior&producto=$producto&rubro=$rubro&precio=$precio\" onMouseOver=\"arriba('t3');window.status='Pagina Anterior';return true\" onMouseOut=\"fuera('t3');return true\">
<img name=\"t3\" src=\"../images/home/b_anterior1.gif\" align=top border=0></a>";
};
if ($numrows >= (($pagina+1) * 10) - 9) {
$proxima = $pagina + 1;
$paginaprox = "<a href=\"/phpfiles/buscarproductos.php3?pagina=$proxima&producto=$producto&rubro=$rubro&precio=$precio\" onMouseOver=\"arriba('t2');window.status='Proxima Pagina';return true\" onMouseOut=\"fuera('t2');return true\">
<img name=\"t2\" src=\"../images/home/b_proxima1.gif\" align=top border=0></a>";
$paginaproxa = "<a href=\"/phpfiles/buscarproductos.php3?pagina=$proxima&producto=$producto&rubro=$rubro&precio=$precio\" onMouseOver=\"arriba('t4');window.status='Proxima Pagina';return true\" onMouseOut=\"fuera('t4');return true\">
<img name=\"t4\" src=\"../images/home/b_proxima1.gif\" align=top border=0></a>";
};
echo "
<center>
<font class=\"texto\">A continuación se listan los productos que corresponden a los
datos que Ud. ha ingresado.<br>Si la búsqueda arrojo <font class=\"texton\">más de 10
productos</font> se podrá avanzar y/o retroceder de pagina con<br><font
class=\"texton\">los botones \"Anteriores\" y \"Próximos\"</font></font>.
<br><br>
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td bgcolor=ffba00><img src=\"../images/home/puntana.gif\"><img src=\"../images/home/negro1x1.gif\" width=592 height=2 align=top></td>
</tr>
<tr>
<td valign=top rowspan=2>
<table border=0 width=600 cellpadding=0 cellspacing=0>
<tr>
<td valign=top><img src=\"../images/home/negro1x1.gif\" width=2 height=38></td>
<td bgcolor=ffba00 valign=top><center>$paginaante</td>
<td bgcolor=ffba00 colspan=3 valign=top><center><font class=\"titulo\" color=000000><center>Resultado de la Busqueda<br>($numrows productos)</td>
<td bgcolor=ffba00 colspan=3 valign=top><p align=right>$paginaprox     </td>
</td>
</tr>
";
$j=($pagina-1)*10;
$j_ant = $j;
while (($j<$numrows) && ($j - $j_ant < 10)) {
// While loop to print each row returned;
$idpro = mysql_result($resultpro, $j, "ID_PRODUCTO");
$imagenpro = strval(mysql_result($resultpro, $j, "IMAGEN")) . "_sm.gif";
$nombrepro = mysql_result($resultpro, $j, "NOMBRE");
$descrippro = mysql_result($resultpro, $j, "DESCRIP");
$preciopro = mysql_result($resultpro, $j, "PRECIO");
$preciopro = str_replace(".",",",$preciopro);
$idneg = mysql_result($resultpro, $j, "ID_NEGOCIO");
$imagenneg = $idneg . "_sm.gif";
$resultneg = mysql($database,"select * from Negocios where ID_NEGOCIO=$idneg");
$nombreneg = mysql_result($resultneg, 0, "NOMBRE");
$bg = "beffff";
if ($imagenpro != "_sm.gif") {
$bg = "beffff";
echo "
<tr>
<td valign=top width=2 rowspan=2><img src=\"../images/home/negro1x1.gif\" width=2 height=135></td>
<td width=180 valing=top><font class=\"produtit\" color=ffffff>
<center><a href=\"/phpfiles/mostrarproducto.php3?producto=$idpro\"><img src=\"../images/productos/$imagenpro\" border=0><font class=\"produtit\" color=000000><br>$nombrepro</a></font>
</td>
<td width=30><center><img src=\"../images/home/negro1x1.gif\" width=1 height=105>
</td>
<td width=400 valing=top><font class=\"produtit\" color=000000>$descrippro</font></td>
<td width=30><center><img src=\"../images/home/negro1x1.gif\" width=1 height=105></td>
<td width=80 valing=top><p align=right><font class=\"preciog\" color=000000>$$preciopro</font><br>
<a href=\"javascript:ventanaNueva('/phpfiles/calcularenvio.php3?producto=$producto','Calculo\sde\sEnvío\sdel\sProducto','scrollbars=no,width=340,height=250');\" onMouseOver=\"window.status='Calcular Gastos de Envio';return true\" onMouseOut=\"return true\" class=\"envio\">+ envío.</a></td>
<td width=30><center><img src=\"../images/home/negro1x1.gif\" width=1 height=105></td>
<td width=80 valing=top><font class=\"produtit\" color=ffffff>
<center><a href=\"/phpfiles/mostrarnegocio.php3?negocio=$idneg\"><img src=\"/images/negocios/$imagenneg\" border=0><font class=\"produtit\" color=000000><br></a></font></td>
</tr>
<tr><td><font class=\"titulito\"><br></td></tr>
";
} else {
$bg = "abe0d1";
echo "
<tr>
<td valign=top width=2><img src=\"../images/home/negro1x1.gif\" width=2 height=55></td>
<td width=150 valing=top height=1% bgcolor=$bg><font class=\"produtit\" color=ffffff>
<center><a href=\"/phpfiles/mostrarproducto.php3?producto=$idpro\"><font class=\"produtitsf\" color=000000>$nombrepro</a></font>
</td>
<td width=20 bgcolor=$bg><center><img src=\"../images/home/negro1x1.gif\" width=1 height=40>
</td>
<td width=150 valing=top bgcolor=$bg><font class=\"produtit\" color=000000>$descrippro$nombrepro</font></td>
<td width=20 bgcolor=$bg><center><img src=\"../images/home/negro1x1.gif\" width=1 height=40></td>
<td width=80 valing=top bgcolor=$bg><p align=right><font class=\"preciog\" color=000000>$$preciopro</font></td>
<td width=20 bgcolor=$bg><center><img src=\"../images/home/negro1x1.gif\" width=1 height=40>
<td width=80 bgcolor=$bg valing=top><font class=\"produtit\" color=ffffff>
<a href=\"/phpfiles/mostrarnegocio.php3?negocio=$idneg\"><img src=\"/images/negocios/$imagenneg\" border=0><font class=\"produtit\" color=000000><br></a></font></td>
</tr>
";
};
$j++;
};
echo "
</table>
</td>
</tr>
</table>
<br>$paginaantea ";
if (($paginaante != "<font class=\"titulito\"><br></font>") || ($paginaprox != "<font class=\"titulito\"><br></font>")) { echo "<font class=\"titulo\">Productos "; };
echo "$paginaproxa
</td>
</tr>
<tr>
<td bgcolor=beffff><p align=lefrt><img src=\"../images/home/puntaabi.gif\" align=bottom></td>
</tr>
</table>
</td>
</tr>
</table>
";
} else {
/* ###########################################################
############ EMPIESA EXCEPCION ##################
########################################################### */
if ($primera == 'si') {
$texto = "
<font class=\"texto\">En <font class=\"texton\">ComproBien.com</font> Ud.
puede buscar el producto que necesita con amplias opciones de búsqueda. Utilice el
siguiente formulario para indicarnos el tipo de producto que desea.
";
} else {
$texto = "<center><font class=\"titulo\">No se ha encontrado ningún producto.<br> Por favor pruebe utilizando nuestras opciones de búsqueda.</font>";
};
echo "
<table border=0 width=100% cellpadding=0 cellspacing=0>
<tr>
<td width=10 rowspan=4></td>
<td valign=top>
<table border=0 width=100% cellpadding=2 cellspacing=0>
<tr>
<td valign=top width=798>
$texto<br><br>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign=top>
<center>
<form name=\"formu\" action=\"/phpfiles/buscarproductos.php3?pagina=1\" method=post>
<!-- ***************************** CUADRO BUSQUEDA *************************************** -->
<table border=0 cellpadding=0 cellspacing=0>
<tr>
<td bgcolor=ffba00><img src=\"../images/home/puntana.gif\"><img src=\"../images/home/negro1x1.gif\" width=355 height=2 align=top></td>
</tr>
<tr>
<td valign=top rowspan=2>
<table border=0 width=100% cellpadding=0 cellspacing=0>
<tr>
<td valign=top rowspan=2><img src=\"../images/home/negro1x1.gif\" width=2 height=105></td>
<td bgcolor=ffba00><center><font class=\"titulo\" color=000000>Búsqueda de Productos</td>
</tr>
<tr>
<td bgcolor=beffff>
<table border=0 cellpadding=4 cellspacing=0>
<tr>
<td><font class=\"dato\" color=000000>Producto</td>
<td valign=top colspan=3><input type=text name=\"producto\" value=\"$producto\" size=38 maxlength=50></td>
</tr>
<tr>
<td><font class=\"dato\" color=000000>Rubro</td>
<td valign=top>
<select name=\"rubro\">
<option value=\"\">Todos</option>
";
$j=0;
while ($j<$numrowsrub) {
// While loop to print each row returned;
$idrub = intval(mysql_result($resultrub, $j, "ID_RUBRO"));
$nombrerub = mysql_result($resultrub, $j, "NOMBRE");
echo "<option value=\"$idrub\">$nombrerub</option>
";
$j++;
};
echo "
</select>
</td>
<td><font class=\"dato\" color=000000>Precio</td>
<td valign=top>
<select name=\"precio\">
<option value=\"000999\">Todos</option>
<option value=\"010100\">$10 - $100</option>
<option value=\"100250\">$100 - $250</option>
<option value=\"250500\">$250 - $500</option>
<option value=\"500999\">$500 - Más</option>
</select>
</td>
</tr>
<tr>
<td colspan=6><center>
<a href=\"javascript:checkvacios();\" onMouseOver=\"arriba('t0');window.status='Buscar Producto';return true\" onMouseOut=\"fuera('t0');return true\"><img name=\"t0\" src=\"../images/home/b_buscar1.gif\" border=0></a>
</td>
</tr>
</table>
</tr>
</table>
</td>
</tr>
</table>
</form>
<!-- ***************************** FIN CUADRO BUSQUEDA *************************************** -->
</td>
</tr>
<tr>
<td><center><br><br><img src=\"../images/home/negro1x1.gif\" width=450 height=1><br></center></td>
</tr>
<tr>
<td valign=top bgcolor=beffff>
<center><br>
<table border=0 width=1% cellpadding=20 cellspacing=0>
<tr>
<td valign=top>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor=beffff><p align=left><img src=\"../images/home/puntaabi.gif\" align=bottom></td>
</tr>
</table>
</td>
</tr>
</table>
";
};
?>
</body>
</html>
*************************************
************************************

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas