Foros del Web » Programando para Internet » PHP »

Mejorar paginacion

Estas en el tema de Mejorar paginacion en el foro de PHP en Foros del Web. Hola, buenas he echo una paginacion en mi web,pero me gustaria que si ahi por ejemplo 100 paginas, que no salgan las 100. Código PHP: ...
  #1 (permalink)  
Antiguo 30/05/2009, 09:56
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Mejorar paginacion

Hola, buenas he echo una paginacion en mi web,pero me gustaria que si ahi por ejemplo 100 paginas, que no salgan las 100.

Código PHP:
<?php
        
//ANTERIOR
    
if($pagActual 1){
        echo 
'<a href="index.php?pag='.($pagActual-1).$GetXPagina.$q.'"> &lt;&lt;&lt; Anterior</a>&nbsp;';
        }else{
            echo 
'&lt;&lt;&lt; Anterior &nbsp;';
    }
    
//NUMERACION
    
for($i=1;$i<=$numPags;$i++){
        if(
$i==$pagActual){
            echo 
"<b>$i</b> ";
            
            }else{
            echo 
'<a href="index.php?pag='.$i.$GetXPagina.$q.'">'.$i.'</a> ';
        }
    
    }
    
//SIGUIENTE
    
if($pagActual $numPags){
    echo 
'&nbsp;<a href="index.php?pag='.($pagActual+1).$q.'">Siguiente &gt;&gt;&gt; </a>';
    }else{
        echo 
'&lt;&lt;&lt; Siguiente &nbsp;';
    }
    
    
    
?>
  #2 (permalink)  
Antiguo 30/05/2009, 10:10
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Si quieres puedes usar tambien este

Código PHP:
<?php 
class paginationClass
    var 
$page_num 1
    var 
$page_pagination
    var 
$r_comment
    var 
$row_num
    var 
$numofpages
    var 
$range_min
    var 
$range_max;  
    var 
$page_min;  
    var 
$page_max;  

    var 
$range
    var 
$rowsPerPage
    var 
$s_comment
    var 
$s_comment_n
     
    function 
paginationClass($r$rPP$sc$scn){ 
        
$this->range $r
        
$this->rowsPerPage $rPP
        
$this->s_comment $sc
        
$this->s_comment_n $scn
    } 

    private function 
setComment(){ 
        
$this->page_num = (empty($this->page) ? $this->page_num $this->page); 
        
$offset = ($this->page_num 1) * $this->rowsPerPage
        
$this->s_comment $this->s_comment " LIMIT $offset, " $this->rowsPerPage
        
$this->r_comment mysql_query($this->s_comment) or die(mysql_error()); 
        
$r_comment_n mysql_query($this->s_comment_n) or die(mysql_error()); 
        
$this->row_num mysql_num_rows($r_comment_n); 
    } 

    private function 
setValues(){ 
        
$this->numofpages ceil($this->row_num/$this->rowsPerPage); 
        if(
$this->numofpages 1){ 
            
$this->range_min = ($this->range &#37; 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2; 
            
$this->range_max = ($this->range == 0) ? $this->range_min $this->range_min;  
            
$this->page_min $this->page_num $this->range_min;  
            
$this->page_max $this->page_num $this->range_max;  

            
$this->page_min = ($this->page_min 1) ? $this->page_min;  
            
$this->page_max = ($this->page_max < ($this->page_min $rthis->ange 1)) ? $this->page_min $this->range $this->page_max;  
            if (
$this->page_max $this->numofpages){  
                
$this->page_min = ($this->page_min 1) ? $this->numofpages $this->range 1;  
                
$this->page_max $this->numofpages;  
            } 
            
$this->page_min = ($this->page_min 1) ? $this->page_min;  
        } 
    } 

    private function 
setPagePagination(){ 
        
$self $_SERVER["PHP_SELF"]; 
        if(
$this->numofpages 1){ 
            
$setPP "<div>"

            
//[ « ] 
            
if (($this->page_num > ($this->range $this->range_min)) && ($this->numofpages $this->range)){  
                
$setPP .= "[ <a title='First' href='".$self."?page=1' class='title'><b>&laquo;</b></a> ]\n";  
            } 

            
// [ Previous # ] 
            
if ($this->page_num != 1) {  
                
$setPP .= "[ <a title='First' href='".$self."?page=".($this->page_num-1)."' class='title'><b>Anteriores " $this->rowsPerPage "</b></a> ]\n";  
            }  

            
// ... 
            
if (($this->page_num > ($this->range $this->range_min)) && ($this->numofpages $this->range)){  
                
$setPP .= " <b>...</b> ";  
            } 

            
// [ # ] 
            
for ($i $this->page_min$i <= $this->page_max$i++) {  
                if (
$i == $this->page_num)  
                    
$setPP .= "[ <b><font color=red>" $i "</font></b> ]\n";  
                else  
                    
$setPP .= "[ <a href='" .$self"?page=" .$i"' class='title'>".$i."</a> ]\n";  
            }  

            
// ... 
            
if (($this->page_num< ($this->numofpages $this->range_max)) && ($this->numofpages $this->range)) {  
                
$setPP .= " <b>...</b> ";  
            } 

            
//[ Next # ] 
            
if ($this->page_num $this->numofpages) {  
                
$setPP .= "[ <a href='".$self."?page=".($this->page_num 1) . "' class='title'><b>Siguientes " $this->rowsPerPage "</b></a> ]\n";  
            }  

            
//[ » ] 
            
if (($this->page_num< ($this->numofpages $this->range_max)) && ($this->numofpages $this->range)) {  
                
$setPP .= "[ <a title='Last Info' href='".$self."?page=".$this->numofpages"' class='title'><b>&raquo;</b></a> ]\n";  
            } 

            
$setPP .= "</div>"

            
$this->page_pagination $setPP
        } 
    } 

    function 
getPagePagination(){ 
        
$this->setComment(); 
        
$this->setValues(); 
        
$this->setPagePagination(); 
        return array(
'r_comment' => $this->r_comment,  
            
'row_num' => $this->row_num,  
            
'page_pagination' => $this->page_pagination);  
    } 


function 
pagination($r$rPP$sc$scn){ 
    
$p = new paginationClass($r$rPP$sc$scn); 
    foreach(
$p->getPagePagination() as $k => $v){ 
        if(
trim($v)){ 
            global $
$k
            $
$k $v
        } 
    } 

?>

Código PHP:
sql_que_quiero_verificar "SELECT * FROM TABLA"

$sql_que_verificara_numero_de_lineas "SELECT * FROM TABLA"


/* 
El primero es para que me muestre el rango que quiero mostrar 
El segundo es para que me muestre cuantos "Rows" quiero que muestre 
El tercero es mi sql principal 
El cuarto es para ver cuantos "Rows" tiene la tabla seleccionada 
*/ 
pagination(1010$sql_que_quiero_verificar$sql_que_verificara_numero_de_lineas); 

while(
$row mysql_fetch_assoc($r_comment)){ 
  
//codigo que quieras usar 
  
echo $page_pagination

  #3 (permalink)  
Antiguo 30/05/2009, 10:15
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

no entiendo como funciona,tengo que rellenar los href solo de ahi?
  #4 (permalink)  
Antiguo 30/05/2009, 10:23
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Escribe solamente los query que tu quieres verificar en la base de datos en las dos variables que dicen

sql_que_quiero_verificar = "SELECT * FROM TABLA";

$sql_que_verificara_numero_de_lineas = "SELECT * FROM TABLA";
  #5 (permalink)  
Antiguo 30/05/2009, 10:27
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Creas un archivo que diga por ejemplo

paginar.php

ahi escribes el codigo de class

luego en la pagina que quieras escribes

Código PHP:
include_once("paginar.php");

sql_que_quiero_verificar "SELECT * FROM TABLA"

$sql_que_verificara_numero_de_lineas "SELECT * FROM TABLA"


/* 
El primero es para que me muestre el rango que quiero mostrar 
El segundo es para que me muestre cuantos "Rows" quiero que muestre 
El tercero es mi sql principal 
El cuarto es para ver cuantos "Rows" tiene la tabla seleccionada 
*/ 
pagination(1010$sql_que_quiero_verificar$sql_que_verificara_numero_de_lineas); 

//esto es solamente un ejemplo el $r_comment es parte del class asi que cuando quieras ver el query lo que tienes que hacer es algo asi 

while($row mysql_fetch_assoc($r_comment)){ 
  
//codigo que quieras usar 
  
echo $page_pagination

$page_pagination es la variable que despliega las paginas

<< ... 3 4 5 ... >>
  #6 (permalink)  
Antiguo 30/05/2009, 10:51
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

no me funciona ,mira entra a mi web en mi firma.
tiene algunos fallos.
  #7 (permalink)  
Antiguo 30/05/2009, 10:54
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

pero cual es tu web?
  #8 (permalink)  
Antiguo 30/05/2009, 11:01
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

www.mira-divx.com
  #9 (permalink)  
Antiguo 30/05/2009, 11:09
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

por cierto en sql_que_quiero_verificar = "SELECT * FROM TABLA"; le falta el $ no?
  #10 (permalink)  
Antiguo 30/05/2009, 11:55
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Si, asi es, perdon no se lo escribi
  #11 (permalink)  
Antiguo 30/05/2009, 12:39
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

pero no funciona bien?
  #12 (permalink)  
Antiguo 30/05/2009, 15:16
 
Fecha de Ingreso: mayo-2009
Mensajes: 37
Antigüedad: 15 años
Puntos: 0
Respuesta: Mejorar paginacion

Trate de echar andar el ejemplo que pusiste arriba, adapté el query a uno para mi pero solo me muestra los enlaces y no me muestra los datos que le consulté, no se si tenga que hacer otra cosa. Además en el método "setValues" habia un error, bueno eso me decia mi editor, así que le quite la parte que yo creí estaba mal, le quite esta parte: #37;

Código PHP:
$this->range_min = ($this->range &#37; 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2; 
Y me quedo así...

Código PHP:
$this->range_min = ($this->range == 0) ? ($this->range 2) -: ($this->range 1) / 2
  #13 (permalink)  
Antiguo 30/05/2009, 16:10
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

this->range_min = ($this->range % 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2;

ponle porciento es que este sistema cambia el porciento por el hexadecimal
  #14 (permalink)  
Antiguo 30/05/2009, 16:13
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Los datos que consultaste tienes que buscarlos luego con el ejemplo que te di del while

Ese ejemplo tienes que dejarlo como esta porque yo llamo esa variable dentro del class. Toda la informción del query esta en esa variable o sea en la variable que dice

$row = mysql_fetch_assoc($r_comment)

por lo tanto dejala en el while y escribe debajo del while los nombres de los campos de la base de datos

Código PHP:
while($row mysql_fetch_assoc($r_comment)){  
  
//codigo que quieras usar  
$row["Nombre_de_la_columna_base_de_datos"];
$row["Nombre_de_la_columna_base_de_datos"];
$row["Nombre_de_la_columna_base_de_datos"];
$row["Nombre_de_la_columna_base_de_datos"];
$row["Nombre_de_la_columna_base_de_datos"];

  echo 
$page_pagination
  #15 (permalink)  
Antiguo 30/05/2009, 17:56
 
Fecha de Ingreso: octubre-2006
Mensajes: 471
Antigüedad: 17 años, 6 meses
Puntos: 1
Respuesta: Mejorar paginacion

ya esta solucionado ,muchas gracias.
  #16 (permalink)  
Antiguo 30/05/2009, 21:54
 
Fecha de Ingreso: mayo-2009
Mensajes: 37
Antigüedad: 15 años
Puntos: 0
Pregunta Respuesta: Mejorar paginacion

:s A mi no me quedo!!! Ya hice lo que dijiste pero sigue sin mostrarme el query, modifique la parte del metodo del class y me quedo así como diiste:

Código PHP:
$this->range_min = ($this->range == 0) ? ($this->range 2) -: ($this->range 1) / 2
y agregue la parte de codigo dentro del "while", me kedo así:

Código PHP:
<?php
require('basededatos.php');
include_once(
"paginacion.php");

$sql_que_quiero_verificar "SELECT msg FROM logs"
$sql_que_verificara_numero_de_lineas "SELECT msg FROM logs"

pagination(5,1$sql_que_quiero_verificar$sql_que_verificara_numero_de_lineas); 
while(
$row mysql_fetch_assoc($r_comment)){ 
  
$row["msg"];
  echo 
$page_pagination
}  
?>
Y aun asi no veo el query y los link que me aperecen no se mueven, aunque le de en "siguientes", que podra ser??? Ya verifique que la columna que stoy consultando exista y todo eso... :s
  #17 (permalink)  
Antiguo 30/05/2009, 22:03
 
Fecha de Ingreso: mayo-2009
Mensajes: 37
Antigüedad: 15 años
Puntos: 0
Respuesta: Mejorar paginacion

Ya empece por sacar el
Código PHP:
 echo $page_pagination
del while y por poner "echo".

Código PHP:
<?php
require('basededatos.php');
include_once(
"paginacion.php");

$sql_que_quiero_verificar "SELECT * FROM logsfg"
$sql_que_verificara_numero_de_lineas "SELECT * FROM logsfg"

pagination(10,10$sql_que_quiero_verificar$sql_que_verificara_numero_de_lineas); 
while(
$row mysql_fetch_assoc($r_comment)){ 
  echo 
"<td>".$row["fuente"]."</td><br>";
  } 
  echo 
$page_pagination;  
?>
Ya veo el query, pero solo veo los primeros 10, si le doy click en los enlaces para ver los otros registros, no me manda a ningun lado, osea no veo los demas registros... :s, al script del class no le moví nada lo unico q modifique fue este del ejemplo. Por favor ayuda.

Última edición por axel_ito; 30/05/2009 a las 22:14
  #18 (permalink)  
Antiguo 31/05/2009, 00:15
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

hiciste primero el cambio de esto
this->range_min = ($this->range % 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2;


lo tienes en el class así
this->range_min = ($this->range & 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2;


y debe estar como el primero
  #19 (permalink)  
Antiguo 31/05/2009, 00:19
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: Mejorar paginacion

Perdona ya vi donde esta el error se me olvido escribirte una linea cambia esta funcion en el class por esta

Código PHP:
function pagination($r$rPP$sc$scn){ 
    
$p = new paginationClass($r$rPP$sc$scn); 
    
$p->page $_REQUEST["page"];
    foreach(
$p->getPagePagination() as $k => $v){ 
        if(
trim($v)){ 
            global $
$k
            $
$k $v
        } 
    } 

y tambien cambia el & por el % en esta linea
this->range_min = ($this->range & 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2;

debe ser
this->range_min = ($this->range % 2 == 0) ? ($this->range / 2) -1 : ($this->range - 1) / 2;
  #20 (permalink)  
Antiguo 31/05/2009, 01:45
 
Fecha de Ingreso: mayo-2009
Mensajes: 37
Antigüedad: 15 años
Puntos: 0
Respuesta: Mejorar paginacion

perfecto, ya funcionó, muchas gracias abimaelrc... XD
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 17:24.