Foros del Web » Programando para Internet » PHP »

Paginar resultados no funciona

Estas en el tema de Paginar resultados no funciona en el foro de PHP en Foros del Web. Hola, espero puedan ayudarme a corregir este codigo, puesto que si muestra la paginacion, pero al darle click sobre el link para cambiar de pagina, ...
  #1 (permalink)  
Antiguo 23/05/2009, 15:23
 
Fecha de Ingreso: enero-2006
Ubicación: Cancún
Mensajes: 39
Antigüedad: 18 años, 3 meses
Puntos: 0
Paginar resultados no funciona

Hola, espero puedan ayudarme a corregir este codigo, puesto que si muestra la paginacion, pero al darle click sobre el link para cambiar de pagina, solamente muestra los primeros 10 resultados, no avanza en los otros resultados que se tengan. Desde ya gracias =)
Código PHP:
<?
$registros 
10;
if (!
$pagina) { 
    
$inicio 0
    
$pagina 1

else { 
    
$inicio = ($pagina 1) * $registros

    
$c="SELECT * FROM ciudades ORDER BY ciudad";
    
$qC=mysql_query($c,$dbh) or die ("ERROR L112: ".mysql_error());
    echo 
" | ";
    while (
$Ciudades=mysql_fetch_array($qC)) {
        echo 
"<a href='?op=".base64_encode('ciudad')."&ciudad=".base64_encode($Ciudades[id])."'>".$Ciudades[ciudad]."</a>";
        echo 
" | ";
    }
    if (
base64_decode($_GET[ciudad])=="") { 
        echo 
"<br>Seleccione la ciudad a desplegar<br><br>";
    } else {

    
$resultados mysql_query("SELECT * FROM datos WHERE idCiudad='".$_GET[ciudad]."'");
    
$total_registros mysql_num_rows($resultados); 
    
$resultados mysql_query("SELECT * FROM datos WHERE idCiudad='".$_GET[ciudad]."' ORDER BY nUnidad LIMIT ".$inicio.",".$registros);
    
$total_paginas ceil($total_registros $registros);                       
?>
<br><br>
<?
    $t
="SELECT * FROM ciudades WHERE id=".base64_decode($_GET[ciudad]);
    
$qT=mysql_query($t,$dbh) or die ("ERROR L130: ".mysql_error());
    
$Titulo=mysql_fetch_array($qT);
    echo 
"<center><h3>".$Titulo[ciudad]."</h3></center>";
?>
<table width="100%">
    <tr>
        <td align="center"><strong>Unidad</strong></td>
        <td align="center"><strong>Fecha<br />
        Reparación</strong></td>
        <td align="center"><strong>Orden de<br />
Servicio</strong></td>
            <td colspan="2" align="center">&nbsp;</td>
        </tr>
<?    if($total_registros) { while ($Datos=mysql_fetch_array($resultados)) { ?>
            <tr class="opt" id="seleccion" onclick="">
                <td align="center"><? echo $Datos[nUnidad];?></td>
                <td align="center"><? echo $Datos[fechaReparacion];?></td>
                <td align="center"><? echo $Datos[ordenServicio];?></td>
                <td align="center"><a href="ciudadDetalles.php?op=<? echo base64_encode("ciudad");?>&ciudad=<? echo $_GET[ciudad];?>&id=<? echo base64_encode($Datos[id]);?>"><img src="../images/b_browse.png" alt="Unidad: <? echo $Datos[nUnidad];?> Orden de Compra: <? echo $Datos[ordenServicio];?>" width="16" height="16" /></a></td>
            </tr>
<?     ?>
</table>            
<?
        
    
} else { echo "<font color='darkgray'>(sin resultados)</font>"; }
    echo 
"<br>";
    
mysql_free_result($resultados);                
    if(
$total_registros) {
        echo 
"<center>";
        if((
$pagina 1) > 0) {
            echo 
"<a href='ciudad.php?pagina=".($pagina-1)."&op=".base64_encode('ciudad')."&ciudad=".$_GET[ciudad]."'>< Anterior</a> ";
        }
        for (
$i=1$i<=$total_paginas$i++){
            if (
$pagina == $i) {
                echo 
"<b>".$pagina."</b> "
            } else {
                echo 
"<a href='ciudad.php?pagina=".$i."&op=".base64_encode('ciudad')."&ciudad=".$_GET[ciudad]."'>$i</a> "
            }    
        }
        if((
$pagina 1)<=$total_paginas) {
            echo 
" <a href='ciudad.php?pagina=".($pagina+1)."&op=".base64_encode('ciudad')."&ciudad=".$_GET[ciudad]."'>Siguiente ></a>";
        }
        echo 
"</center>";
    } }
?>
__________________
[FONT="Book Antiqua"]Me llaman Castillo, Lex Castillo...[/FONT]
  #2 (permalink)  
Antiguo 23/05/2009, 16:22
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Paginar resultados no funciona

Donde estas obteniendo $pagina? eso no lo veo y tal vez confias en tener activo register_globals (muy peligroso por cierto) y debieras obtenerla antes de calcular $inicio asi:

$pagina = (isset($_GET['pagina'])) ? intval($_GET['pagina']) : 1;
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 24/05/2009, 20:51
 
Fecha de Ingreso: enero-2006
Ubicación: Cancún
Mensajes: 39
Antigüedad: 18 años, 3 meses
Puntos: 0
Respuesta: Paginar resultados no funciona

No Triby, aun con esa linea de codigo me sale error de que faltan argumentos heeeelp por favorrrrrrrrrrrrrrrrrr =S
__________________
[FONT="Book Antiqua"]Me llaman Castillo, Lex Castillo...[/FONT]
  #4 (permalink)  
Antiguo 24/05/2009, 21:13
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 14 años, 11 meses
Puntos: 1517
Respuesta: Paginar resultados no funciona

Te voy a dar mejor el codigo que yo uso para paginacion espero que te ayude

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);
    
$p->page $_REQUEST["page"];
    foreach(
$p->getPagePagination() as $k => $v){
        if(
trim($v)){
            global $
$k;
            $
$k $v;
        }
    }
}
?>
Cambia el & #37; por %. Para Usarlo debes llamar la funcion de esta forma

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;


Espero te sea útil

Última edición por abimaelrc; 31/05/2009 a las 00:45
  #5 (permalink)  
Antiguo 25/05/2009, 18:59
 
Fecha de Ingreso: enero-2006
Ubicación: Cancún
Mensajes: 39
Antigüedad: 18 años, 3 meses
Puntos: 0
Respuesta: Paginar resultados no funciona

Wow, ya vi como hacer funcionar el de Triby y tambien el de abimaelrc (a pesar que al principio me marcaba un error con respecto a la "}" ) pero ya quedaron los dos resueltos, gracias a ambos, son verdaderos masters!!!!! =D
__________________
[FONT="Book Antiqua"]Me llaman Castillo, Lex Castillo...[/FONT]
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 01:46.