PHP - Paginación

 
Vista:

Paginación

Publicado por Pepe (3 intervenciones) el 12/06/2007 22:01:40
Hola a todos tengo un problema tengo una página donde debo ir desplegando cierta cantidad de registros obtenidos de una consulta a la BD, uso esto "SELECT * FROM tabla ORDER BY Fecha DESC LIMIT $inicio, $registros"; mi problema es como hago para que cuando yo presione sigte me muestre los sigtes registros en otras páginas, solo me muestran los actuales y no pasa nada con los siguientes valores ?
ya busque en todos los foros códigos de paginación así que no me envien direcciones de páginas porque no me han resultado.
espero su ayuda
saludos

Código
<?
If (!$pagina)
{
$inicio = 0;
$pagina = 1;
}
Else
{
$inicio = ($pagina - 1) * $registros;
}

$consulta ="SELECT Cod FROM tabla";
$resultados = mysql_query($consulta, $coneccion);
$total_registros = mysql_num_rows($resultados);
$consulta = "SELECT * FROM tabla ORDER BY Fecha DESC LIMIT $inicio, $registros";
$resultados = mysql_query($consulta, $coneccion);
$total_paginas = ceil($total_registros / $registros);

If($total_registros)
{
While($articulo=mysql_fetch_array($resultados))
{
echo "<b>".$articulo["Cod"]."</b><br>";
}
}
Else
{
echo "<font color='darkgray'>(sin resultados)</font>";
}

mysql_free_result($resultados);

if($total_registros) {

echo "<center>";

If(($pagina - 1) > 0)
{
echo "<a href='paginacion.php?pagina=".($pagina-1)."'>< Anterior</a> ";
}

for ($i=1; $i<=$total_paginas; $i++)
{
If ($pagina == $i)
echo "<b>".$pagina."</b> ";
Else
echo "<a href='paginacion.php?pagina=$i'>$i</a> ";
}

If(($pagina + 1)<=$total_paginas)
{
echo " <a href='paginacion.php?pagina=".($pagina+1)."'>Siguiente ></a>";
}

echo "</center>";

}
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Paginación

Publicado por basnek (63 intervenciones) el 13/06/2007 10:23:17
Aca te paso un ejemplo rapido con DW
Esto sería para el dump de pruebas

-- ----------------------------
-- Table structure for productos
-- ----------------------------
CREATE TABLE `productos` (
`productoID` int(11) NOT NULL auto_increment,
`producto` varchar(255) default NULL,
`descripcion` text,
`url` varchar(255) default NULL,
`modelo` varchar(255) default NULL,
`imagen` varchar(255) default NULL,
`pu` double(15,4) NOT NULL default '0.0000',
`visible` char(1) default '1',
`destacar` char(1) default NULL,
PRIMARY KEY (`productoID`)
) TYPE=MyISAM;

Luego aqui el codigo para que pongas en algun pirulo.php. Así que Éxitos

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_cnx = "localhost";
$database_cnx = "pruebas";
$username_cnx = "usuario";
$password_cnx = "clave";
$cnx = mysql_pconnect($hostname_cnx, $username_cnx, $password_cnx) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$maxRows_rs = 10;
$pageNum_rs = 0;
if (isset($_GET['pageNum_rs'])) {
$pageNum_rs = $_GET['pageNum_rs'];
}
$startRow_rs = $pageNum_rs * $maxRows_rs;

mysql_select_db($database_cnx, $cnx);
$query_rs = "SELECT productoID, producto, pu FROM productos ORDER BY producto ASC";
$query_limit_rs = sprintf("%s LIMIT %d, %d", $query_rs, $startRow_rs, $maxRows_rs);
$rs = mysql_query($query_limit_rs, $cnx) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);

if (isset($_GET['totalRows_rs'])) {
$totalRows_rs = $_GET['totalRows_rs'];
} else {
$all_rs = mysql_query($query_rs);
$totalRows_rs = mysql_num_rows($all_rs);
}
$totalPages_rs = ceil($totalRows_rs/$maxRows_rs)-1;

$queryString_rs = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_rs") == false &&
stristr($param, "totalRows_rs") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_rs = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_rs = sprintf("&totalRows_rs=%d%s", $totalRows_rs, $queryString_rs);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
<title>Documento Sin título</title>
</head>

<body>
<p> 
Registros <?php echo ($startRow_rs + 1) ?> a <?php echo min($startRow_rs + $maxRows_rs, $totalRows_rs) ?> de <?php echo $totalRows_rs ?> </p>
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td>productoID</td>
<td>producto</td>
<td>pu</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_rs['productoID']; ?></td>
<td><?php echo $row_rs['producto']; ?></td>
<td><?php echo $row_rs['pu']; ?></td>
</tr>
<?php } while ($row_rs = mysql_fetch_assoc($rs)); ?>
</table>
<table border="0">
<tr>
<td align="center"><?php if ($pageNum_rs > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_rs=%d%s", $currentPage, 0, $queryString_rs); ?>"><img src="First.gif" border=0></a>
<?php } // Show if not first page ?>
</td>
<td align="center"><?php if ($pageNum_rs > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_rs=%d%s", $currentPage, max(0, $pageNum_rs - 1), $queryString_rs); ?>"><img src="Previous.gif" border=0></a>
<?php } // Show if not first page ?>
</td>
<td align="center"><?php if ($pageNum_rs < $totalPages_rs) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_rs=%d%s", $currentPage, min($totalPages_rs, $pageNum_rs + 1), $queryString_rs); ?>"><img src="Next.gif" border=0></a>
<?php } // Show if not last page ?>
</td>
<td align="center"><?php if ($pageNum_rs < $totalPages_rs) { // Show if not last page ?>
<a href="<?php printf("%s?pageNum_rs=%d%s", $currentPage, $totalPages_rs, $queryString_rs); ?>"><img src="Last.gif" border=0></a>
<?php } // Show if not last page ?>
</td>
</tr>
</table>
</p>
</body>
</html>
<?php
mysql_free_result($rs);
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar