Hola, he visto muchos mensajes preguntando acerca de un paginador para los resultados de sus registros, pues aqui le pongo un codigo que les puede ser util, sin embargo, como dice el titulo es basico, pues para que cada quien le agregue o le quite las cosas convenientes.
Espero les sea util y agreguen mas lineas de codigo para tener un buen resultado. Espero les guste.
Paginador.php
Código PHP:
<?php
// Paginador desarrollado por Jossmorenn, claro, con mucha ayuda de dreamweaver
require_once('conn.php'); // aqui se incluye la conexion
?>
<?php
$currentPage = $_SERVER["PHP_SELF"];
$maxRows_Rs = 1;// este numero es el numero de resultados que quieren que se vean por pagina pueden poner algo asi: $maxRows_Rs = 6; para ver paginas con 6 resultados
$pageNum_Rs = 0;
if (isset($_GET['pageNum_Rs'])) {
$pageNum_Rs = $_GET['pageNum_Rs'];
}
$startRow_Rs = $pageNum_Rs * $maxRows_Rs;
mysql_select_db($database_conn, $conn);
$query_Rs = "SELECT * FROM news_news order by idnoticia desc";//aqui iria el query a la base de datos
$query_limit_Rs = sprintf("%s LIMIT %d, %d", $query_Rs, $startRow_Rs, $maxRows_Rs);
$Rs = mysql_query($query_limit_Rs, $conn) 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 HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Paginador</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>Paginador:</p>
<table width="411" border="0">
<?php do { ?>
<tr>
<td><?php echo $row_Rs['titulo']; ?></td>
</tr>
<?php } while ($row_Rs = mysql_fetch_assoc($Rs)); ?>
</table>
<br>
<br>
<table width="410" border="0">
<tr>
<td width="77"><?php if ($pageNum_Rs > 0) { // Show if not first page ?>
<a href="<?php printf("%s?pageNum_Rs=%d%s", $currentPage, 0, $queryString_Rs); ?>">Primero</a>
<?php } // Show if not first page ?></td>
<td width="55"><?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); ?>">Anterior</a>
<?php } // Show if not first page ?></td>
<td width="144"><div align="center">
<?php
$last=$totalPages_Rs+1;
$current=$pageNum_Rs+1;
for ($i = 1; $i <= $last; $i++) {
if($current==$i){
echo $i." .";
}else{?>
<a href="<?php printf("%s?pageNum_Rs=%d%s", $currentPage, min($totalPages_Rs, $i-1), $queryString_Rs); ?>"><? echo $i;?></a>
<?
}
}
?>
</div></td>
<td width="72"><div align="right">
<?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); ?>">Siguiente</a>
<?php } // Show if not last page ?>
</div></td>
<td width="40"><div align="right">
<?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); ?>">Ultimo</a>
<?php } // Show if not last page ?>
</div></td>
</tr>
</table>
<br>
<br>
<table width="413" border="0">
<tr>
<td width="407"><div align="right"><strong>Pag:</strong>
<?
if($totalPages_Rs==0){
echo "0/0";
}else{
echo $pageNum_Rs+1;
echo "/";
echo $totalPages_Rs+1;
}
?> </div></td>
</tr>
</table>
<p> </p>
<p> </p>
</body>
</html>
<?php
mysql_free_result($Rs);
?>
conn.php
Código PHP:
<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_conn = "localhost";
$database_conn = "database";
$username_conn = "user";
$password_conn = "pass";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>