-paginarempleados.php:
Código PHP:
<?// Conectamos a la Base de Datos
$usuario="Pepe";
$password="Hola";
$base_de_datos="mydb";
$Conn = mysql_connect("localhost", $usuario, $password);
mysql_select_db("$base_de_datos");
// Obtenemos la página actual, por el método que más nos guste
// Por defecto, la página se propaga por la variable $pagina
$pagina = $HTTP_GET_VARS["pagina"];
// Incluimos la Clase Paginado
include("C:/asp/php/class.paginado.php");
$rs = new paginado($Conn); // instanciamos un objeto
$rs->pagina($pagina); // Le indicamos en que página estamos - 1 por defecto
$rs->porPagina(10); // Le decimos cuantos registros por página queremos - 20 por defecto
//$rs->propagar("forum"); // Le decimos las variables que queremos propagar en los links
if(!$rs->query("SELECT * FROM empleados")) // Y ejecutamos nuestra consulta.
{
die( $rs->error() ); // Si Query devolvió falso, hubo un error y lo mostramos.
}
echo "Mostrando ".$rs->desde()." - ".$rs->hasta()." de un total de ".$rs->total()."<br>";
// Recorremos todos los resultados y los mostramos.
while($row = $rs->obtenerArray())
{
echo $row["codigo"].", ".$row["nombre"]."<br>";
echo "<hr>";
} // while
// Finalmente mostramos los medios para la navegación entre los resultados.
echo $rs->anterior()." - ".$rs->nroPaginas()." - ".$rs->siguiente();?>
Al ejecutar me da el siguiente error:
Warning: Invalid argument supplied for foreach() in C:\asp\php\class.paginado.php on line 462
Notice: Undefined variable: ret in C:\asp\php\class.paginado.php on line 464
-class.paginado.php:
<?php
class paginado
{
var $_conn;
var $_error;
var $_pagina;
var $_porPagina = 20;
var $_query;
var $_rs;
var $_total;
var $_totalPaginas;
var $_registros;
var $_siguiente = "Siguiente >";
var $_anterior = "< Anterior";
function paginado($Conn)
{
$this->conn($Conn);
}
function conn()
{
switch (func_num_args())
{
case 1:
$this->_conn = func_get_arg(0);
break;
default:
return $this->_conn;
break;
}
} // function
/**
* Método para acceder a $_error
*
* @access public
* @since 25/02/2002 05:30:39 p.m.
**/
function error()
{
switch (func_num_args())
{
case 1:
$this->_error = func_get_arg(0);
break;
default:
return $this->_error;
break;
}
} // function
/**
* Método para acceder a $_pagina
*
* @access public
* @since 25/02/2002 05:57:18 p.m.
**/
function pagina()
{
switch (func_num_args())
{
case 1:
$this->_pagina = func_get_arg(0);
$this->_pagina = empty($this->_pagina)?1:$this->_pagina;
break;
default:
return $this->_pagina;
break;
}
} // function
/**
* Método para acceder a $_porPagina
*
* @access public
* @since 25/02/2002 05:31:31 p.m.
**/
function porPagina()
{
switch (func_num_args())
{
case 1:
$this->_porPagina = func_get_arg(0);
break;
default:
return $this->_porPagina;
break;
}
} // function
/**
* Método para acceder a $_total
*
* @access public
* @since 26/02/2002 11:13:19 a.m.
**/
function total()
{
switch (func_num_args())
{
case 1:
$this->_total = func_get_arg(0);
break;
default:
return $this->_total;
break;
}
} // function
/**
* Método para acceder a $_totalPaginas
*
* @access public
* @since 26/02/2002 12:22:59 p.m.
**/
function totalPaginas()
{
switch (func_num_args())
{
case 1:
$this->_totalPaginas = func_get_arg(0);
break;
default:
return $this->_totalPaginas;
break;
}
} // function
/**
* Método para acceder a $_rs
*
* En caso de ser un link inválido, el método retorna FALSE.
* @access public
* @since 25/02/2002 05:55:15 p.m.
**/
function rs()
{
switch (func_num_args())
{
case 1:
$this->_rs = func_get_arg(0);
if(!$this->_rs)
{
return false;
}// Fin If
return true;
break;
default:
return $this->_rs;
break;
}
}
//Método para acceder a $_registros
function registros()
{
switch (func_num_args())
{
case 1:
$this->_registros = func_get_arg(0);
break;
default:
return $this->_registros;
break;
}
}
function desde()
{
return (($this->pagina()-1)*$this->porPagina())+1;
}
function hasta()
{
return ($this->desde()-1)+$this->registros();
}
function query($query)
{
$query_count = eregi_replace("select (.*) from", "SELECT COUNT(*) FROM",$query);
if(!$this->rs( @mysql_query($query_count, $this->conn()) ))
{
$this->error("Ocurrió un error al ejecutar el query <i><b>\"$query_count\"</b></i>. La base dijo : <b>".mysql_error()."</b>.");
return false;
}// Fin If
$this->total( mysql_result($this->rs(), 0) );
$this->totalPaginas(ceil($this->total() / $this->porPagina()));
// Comprobamos que no se intenta acceder a una página que no existe.
if( $this->pagina() > $this->totalPaginas() )
{
$this->error("No exite la página ".$this->pagina()." de resutados. Hay solo un total de ".$this->totalPaginas());
return false;
}// Fin If
// Ahora modificamos el Query del usuario, para poder agregarle
// los límites para realizar la paginación
$query .= " LIMIT ".($this->desde()-1).",".$this->porPagina();
if(!$this->rs( @mysql_query($query, $this->conn()) ))
{
$this->error("Ocurrió un error al ejecutar el query \"$query\". La base dijo : ".mysql_error());
return false;
}
$this->registros( mysql_num_rows( $this->rs() ));
return true;
}
function obtenerArray()
{
return mysql_fetch_array( $this->rs() );
}
function siguiente()
{
switch (func_num_args())
{
case 1:
$this->_siguiente = func_get_arg(0);
default:
if($this->hasta() < $this->total())
{
return "<a href=\"?pagina=".($this->pagina()+1).$this->propagar()."\">".$this->_siguiente."</a>";
}
break;
}
}
function anterior()
{
switch (func_num_args())
{
case 1:
$this->_anterior = func_get_arg(0);
default:
if($this->pagina() != 1)
{
return "<a href=\"?pagina=".($this->pagina()-1).$this->propagar()."\">".$this->_anterior."</a>";
}
break;
}
}
function nroPaginas()
{
for($i = 1; $i <= $this->totalPaginas() ; $i++)
{
$temp[$i] = "<a href=\"?pagina=$i".$this->propagar()."\">$i</a>";
}
$temp[$this->pagina()] = "<b>".$this->pagina()."</b>";
return implode(" | ", $temp);
}
function propagar()
{
switch(func_num_args()){
case 0:
[COLOR=red]foreach($this->_variables as $var)[/COLOR] [COLOR=red]-->462[/COLOR]
$ret.= "&$var=".$GLOBALS[$var];
[COLOR=red]return $ret;[/COLOR] [COLOR=red]-->464[/COLOR]
break;
default:
for($i = 0; $i < func_num_args(); $i++)
{
$this->_variables[] = func_get_arg($i);
}
break;
} }
}
?>