aca va la funcion
Código PHP:
function paginacion3($perPage, &$page, &$from, $extraVars, $table, $sqlWhere, $title, $idField = "", $showPages = TRUE) {
// OJO: $page y $from se pasa por referencia
if (!$page) { $page = 1; }
if ($page < 1) { $page = 1; }
if ($title) { $title = $title . ": "; }
if ($extraVars) { $extraVars = "&" . $extraVars; }
if (!$idField) { $idField = "id"; }
$from = ($page - 1) * $perPage;
$sql = "SELECT count(DISTINCT $idField) as n_registros FROM $table $sqlWhere";
$result = mysql_query($sql);
if ($line = mysql_fetch_array($result)) {
$cuantos = $line[n_registros];
$pages = ceil($cuantos / $perPage);
if ($page > 1) {
$previous = "<a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($page - 1)
. "$extraVars\"><b>< Anterior</b></a> | ";
$desde = $perPage * ($page - 1) + 1;
}else{
$desde = 1;
}
if ($desde == $cuantos OR $cuantos < $perPage + $desde) {
$hasta = $cuantos;
}else{
$next = " | <a href=\"" . $_SERVER['PHP_SELF'] . "?page=" . ($page + 1)
. "$extraVars\"><b>Siguiente ></b></a>";
$hasta = $perPage * $page;
}
if ($pages > 0) {
$header = "<div>$previous$title<b>$desde-$hasta</b> de <b>$cuantos</b>$next</div>";
if ($showPages) {
$intermedias = "<div>Paginas: ";
for ($n = 1; $n <= $pages; $n++){
if ($n == $page){
$intermedias .= "$sep<b>$n</b>";
}else{
$intermedias .= "$sep<a href=\"" . $_SERVER['PHP_SELF'] . "?page=$n$extraVars\">$n</a>";
}
$sep = " ";
}
$intermedias .= "</div>";
}
}
//$page = ($page - 1) * $perPage;
}
$header .= $intermedias;
return $header;
}
Código:
<table width="550" cellpadding="2" cellspacing="1"> <? $perPage = 10; $page = $_GET[page]; $paging = paginacion2($perPage, $page, $from, "", "Tabla", "", "Descripcion Tabla", "id_principal_tabla"); mysql_query("SET NAMES utf8"); $sql = "SELECT campo1, campo2 FROM tabla ORDER BY id DESC "; $result = mysql_query($sql); if ($row = mysql_fetch_array($result)) { do { ?> <tr> <td align="center"><?=$row[campo1])?></td> <td align="center"><?=$row[campo2])?></td> </tr> <? } while($row = mysql_fetch_array($result)); } else { ?> <tr> <td colspan="2" id="noRows"><b>No se han encontrado registros</b></td> </tr> <? } ?> </table> <div id="pages"><?=$paging?></div>