Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/08/2003, 05:02
Avatar de lical
lical
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: $PWD
Mensajes: 1.267
Antigüedad: 22 años, 4 meses
Puntos: 0
Clase para paginar.

Hola a todos.

Sé que este tema ya se ha comentado en más de una ocasión y que se han propuesto varios códigos. Yo tenía uno y lo he reescrito recientemente, pero no he tenido tiempo de probarlo todavía (es posible que tenga algún fallo). Por si a alguien le sirve o si lo quiere mejorar/modificar, aquí lo pongo:

Código PHP:
<?php
//////// class_pages.php ////////
// Class for handling page sets

// Author: Ricardo Cervera Navarro (lical)
// <[email protected]>
// Created: 28.08.02, Rewritten: 22.08.03

// This code is (C) Ricardo Cervera
// This code is under the terms of the
// GNU General Public License in its last version.

// Thanks:
// -> Ferdy, helped me starting in OOP
// -> Moi, advice with last_page() function

class pages {

    
// Stores the number of the actual page
    
var $current_page;

    
// Stores how many items are displayed in each page
    
var $items_each_page;

    
// Stores the number of the previous page
    // If there's no prev. page, it will contain (bool) false
    
var $previous_page;

    
// Stores the number of the next page
    // If there's no prev. page, it will contain (bool) false
    
var $next_page;

    
// Number of total items to be handled
    
var $num_total_items;

    
// This array contents the first matching item and the last
    // for the current page, normally, starting counting at 1
    // $limit[0] is the first item and $limit[1] is the last item
    
var $limit = array();

    
// This array's content is explained in compute_limits() function
    // $limit[0] is the first item and $limit[1] is the last item
    
var $lmysql = array();

    
// Stores the number of the last page available
    
var $last_page;

    
// Class constructor, the arguments are: current page number,
    // number of items displayed in each page, and
    // number of total results to be handled (starting counting at 1)
    
function pages($actual_page$items_each_page$num_total_items)
    {
        
$this->actual_page = (int) $actual_page;
        
$this->items_each_page $items_each_page;
        
$this->num_total_items $num_total_items;
        
$this->previous_page $this->exists_page($this->actual_page-1);
        
$this->next_page $this->exists_page($this->actual_page+1);
        
$this->last_page $this->last_page();
        
        if ( 
exists_page($actual_page) == false ) {
            return 
false;
        }
        
        
$this->limit compute_limits();
        
$this->lmysql compute_limits("mysql");
    }

    
// This function computes the last page available
    // (according to the number of total items to handle)
    
function last_page()
    {
        
$last_page $this->num_total_items $this->items_each_page;
        
$last_page ceil($last_page);
        
        if ( 
$this->num_total_items == ) {
            return 
1;
        } else {
            return 
$last_page;
        }
    }

    
// This function gets the number of the first item and the last item
    // for the current page.
    //
    // It takes "mysql" as argument if you want to count
    // for MySQL LIMIT function (this means the first item for this
    // page starting counting them at 0 and last item is
    // [items a page]), otherwise (for normal count
    // and starting counting at 1) it takes no argument
    
function compute_limits($starting_value="normal")
    {
        switch(
$starting_value) {
        
        case 
"mysql":
            
$limit[0] = $this->items_each_page $this->actual_page
                        
$this->items_each_page;
            
$limit[1] = $this->items_each_page;
            
            return 
$limit;
            
            break;
        case 
"normal":
            
$limit[0] = $this->items_each_page $this->actual_page
                        
$this->items_each_page 1;
            if ( 
$this->next_page == false ) {
                  
$limit[1] = $this->num_total_items;
            } else {
                  
$limit[1] = $limit[0] + $this->items_each_page 1;
            }
            
            return 
$limit;
            
            break;
        default:
            return 
false;
            break;
        }
    }
        
    
// This function checks whether a page exists or not
    
function exists_page($page_number)
    {
        
$page_number = (int) $page_number;
        if ( 
$page_number >= &&
             
$page_number <= $this->last_page ) {
            return 
$page_number;
        } else {
            return 
false;
        }
    }
}
?>
Lo siento por lo del inglés, pero es una costumbre que tengo cuando escribo code.


Un saludo,
__________________
lical-> Usuario registrado de Linux #254225

ZonaSiete.ORG - GNU/Linux eminentemente práctico

Última edición por lical; 23/08/2003 a las 05:17