Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/05/2008, 23:30
vmac179
 
Fecha de Ingreso: julio-2007
Ubicación: /home/victor
Mensajes: 114
Antigüedad: 16 años, 9 meses
Puntos: 0
Sonrisa Paginar Consulta

Hola muchachos tengo un problema con esta consulta que estaba implementada para mysql y tratando de implementarla para postgres, les dejo el codigo:

index.php
Código PHP:
<?


$Conn 
="host=localhost port=5432 user=user password=pass dbname=base";

$conn=pg_connect($Conn);


// 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 $_GET["pagina"];



// Incluimos la Clase Paginado

include("class.pag.php");

$rs = new paginado($conn); // instanciamos un objeto

$rs->pagina($pagina); // Le indicamos en que página estamos - 1 por defecto

$rs->porPagina(5); // 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 socios")) // 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($datos $rs->obtenerArray())

{

     echo 
"$datos[nombresocio] $datos[apellido1socio] $datos[apellido2socio]";

    

// while

// Finalmente mostramos los medios para la navegación entre los resultados.

echo $rs->anterior()." - ".$rs->nroPaginas()." - ".$rs->siguiente();

?>
class.pag.php
Código PHP:
<?php



/** 

 * Clase Paginado

 *  

 * Clase que permite la consulta a bases de datos

 * mientras que ofrece un sistema de paginado y 

 * navegación de resultados de manera automática.

 *

 * @author Webstudio <[email protected]>

 * @version 0.1

 **/

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

    
function conn()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_conn func_get_arg(0);

            break;

            default:

                return 
$this->_conn;

            break;

        }

    } 
// function

    
function error()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_error func_get_arg(0);

            break;

            default:

                return 
$this->_error;

            break;

        }

    } 
// function

    
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



    
function porPagina()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_porPagina func_get_arg(0);

            break;

            default:

                return 
$this->_porPagina;

            break;

        }

    } 
// function

    

    
function total()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_total func_get_arg(0);

            break;

            default:

                return 
$this->_total;

            break;

        }

    } 
// function

    

    
function totalPaginas()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_totalPaginas func_get_arg(0);

            break;

            default:

                return 
$this->_totalPaginas;

            break;

        }

    } 
// function

    

    
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;

        }

    } 
// function

    

    
function registros()

    {

        switch (
func_num_args())

        {

            case 
1:

                
$this->_registros func_get_arg(0);

            break;

            default:

                return 
$this->_registros;

            break;

        }

    } 
// function



    
function desde()

    {

        return ((
$this->pagina()-1)*$this->porPagina())+1;

    } 
// function

    

    
function hasta()

    {

        return (
$this->desde()-1)+$this->registros();

    } 
// function

    

    
function query($query)

    {

        
// Primero modificamos el query para averiguar la cantidad total

        // de registros que devuelve el query.

        
$query_count eregi_replace("select (.*) from""SELECT COUNT(*) FROM",$query);

        if(!
$this->rs(@pg_query($this->conn(),$query_count) ))

        {

            
$this->error("Ocurrió un error al ejecutar el query <i><b>\"$query_count\"</b></i>. La base dijo : <b>".pg_result_error()."</b>.");

            return 
false;

        }
// Fin If

        
$this->total(pg_query($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();

        
// SELECT * FROM tabla LIMIT 10 OFFSET 5 ORDER BY id;

        
if(!$this->rs(@pg_query($this->conn(),$query)))

        {

            
$this->error("Ocurrió un error al ejecutar el query \"$query\". La base dijo : ".pg_result_error());

            return 
false;

        }
// Fin If

        
$this->registros(   pg_num_rows($this->rs()));

        return 
true;

    } 
// function



    
function obtenerArray()

    {

        return   
pg_fetch_array($this->rs());

    } 
// function



    
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>";

                }
// Fin If

            
break;

        }

    } 
// function    

    



    
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>";

                }
// Fin If

            
break;

        }

    } 
// function

    



    
function nroPaginas()

    {

        for(
$i 1$i <= $this->totalPaginas() ; $i++)

        {

            
$temp[$i] = "<a href=\"?pagina=$i".$this->propagar()."\">$i</a>";

        } 
// for

        
$temp[$this->pagina()] = "<b>".$this->pagina()."</b>";

        return 
implode(" | "$temp);

    } 
// function

    



    
function propagar()

    {

        switch(
func_num_args()){

            case 
0

                foreach(
$this->_variables as $var)

          
//          $ret.= "&$var=".$GLOBALS[$var];

                
return $ret;

                break;

            default:

                for(
$i 0$i func_num_args(); $i++)

                {

                    
$this->_variables[] = func_get_arg($i);

                } 
// for

                
break;

        } 
// switch

    
// function





// end of class

?>