Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/02/2014, 15:50
Avatar de Faks
Faks
 
Fecha de Ingreso: junio-2012
Mensajes: 26
Antigüedad: 11 años, 10 meses
Puntos: 1
Respuesta: Problema con consulta y paginado

Código PHP:
    <?php  if(isset($_GET['c'])){}else{$_GET['c']='FALSE';};
            switch (
$_GET['c']){
                    case (
'FALSE') :
                                    
$cat 'FALSE';
                                    
$query "SELECT * FROM posts WHERE Categoria!='- Borrador' ORDER BY id DESC";
                                    break;
                    case (
'paintball') :
                                    
$cat 'Paintball';
                                    
$query "SELECT * FROM posts WHERE Categoria='Paintball' ORDER BY id DESC";
                                    break;
                    case (
'airsoft') :
                                    
$cat 'Airsoft';
                                    
$query "SELECT * FROM posts WHERE Categoria='Airsoft' ORDER BY id DESC";
                                    break;
                    case (
'hardware') :
                                    
$cat 'Hardware';
                                    
$query "SELECT * FROM posts WHERE Categoria='Hardware' ORDER BY id DESC";
                                    break;
                    case (
'software') :
                                    
$cat 'Software';
                                    
$query "SELECT * FROM posts WHERE Categoria='Software' ORDER BY id DESC";
                                    break;
                    case (
'juegos') :
                                    
$cat 'Juegos';
                                    
$query "SELECT * FROM posts WHERE Categoria='Juegos' ORDER BY id DESC";
                                    break;
                    case (
'cahos') :
                                    
$cat 'CAHOS Gaming';
                                    
$query "SELECT * FROM posts WHERE Categoria='CAHOSGaming' ORDER BY id DESC";
                                    break;
    };
?>
        <!--                                    
                                                INICIO DE CONTENIDO                          -->
        <div class="section-header">
            <h3>Noticias <?php if($cat!='FALSE'){echo '> '.$cat;}else{};?></h3><a href="javascript:history.back()" class="ver-buttom fr">atras</a>
        </div><!-- fin section-header -->
        <div id="content-overflow-container" class="cf"> <!-- OVERFLOW CONTAINER -->
            <div id="content-container" class="content-overflow fl">
                <section id="news">
                    <?php
                        
include 'includes/conexiondb.php';
                        
//AL PRINCIPIO COMPRUEBO SI HICIERON CLICK EN ALGUNA PÁGINA 
                        
if(isset($_GET['page']))  {
                            
$page$_GET['page'];
                        } else {
                            
//SI NO DIGO Q ES LA PRIMERA PÁGINA
                            
$page=1;
                        }
                        
//ACA SE SELECCIONAN TODOS LOS DATOS DE LA TABLA
                        
$consulta=$query;
                        
$datos=mysql_query($consulta,$conexion);
                        
//MIRO CUANTOS DATOS FUERON DEVUELTOS
                        
$num_rows=mysql_num_rows($datos);
                        
//ACA SE DECIDE CUANTOS RESULTADOS MOSTRAR POR PÁGINA , EN EL EJEMPLO PONGO 15
                        
$rows_per_page8;
                        
//CALCULO LA ULTIMA PÁGINA
                        
$lastpageceil($num_rows $rows_per_page);
                        
//COMPRUEBO QUE EL VALOR DE LA PÁGINA SEA CORRECTO Y SI ES LA ULTIMA PÁGINA
                        
$page=(int)$page;
                        if(
$page $lastpage) {
                            
$page$lastpage; }
                        if(
$page 1) {
                            
$page=1; }
                        
//CREO LA SENTENCIA LIMIT PARA AÑADIR A LA CONSULTA QUE DEFINITIVA
                        
$limit'LIMIT '. ($page -1) * $rows_per_page ',' .$rows_per_page;
                        
//REALIZO LA CONSULTA QUE VA A MOSTRAR LOS DATOS (ES LA ANTERIO + EL $limit)
                        
$consulta .=" $limit";
                        
$resultado=mysql_query($consulta,$conexion);
                        if(!
$resultado) {
                                
//SI FALLA LA CONSULTA MUESTRO ERROR
                                
die('Invalid query: ' mysql_error());
                        } else {
                                
//SI ES CORRECTA MUESTRO LOS DATOS 
                                
while($fila mysql_fetch_assoc($resultado)) {   
                                        
$contenido truncate($fila["Contenido"], 310"..."falsetrue);
                                        echo 
'
                                            <div class="article-container cf">
                                                <div class="news_img_destacada fl">
                                                    <img src="'
.$fila["ImgDestacada"].'" alt="'.$fila["Titulo"].'" height="150" width="150">
                                                </div>
                                                <article class="news_text fr">
                                                    <h2><a href="leer_post.php?id='
.$fila['ID'].'">'.$fila["Titulo"].'</a></h2>
                                                    <h4>'
.$fila["Fecha"].' // escrito por <a href="#">'.$fila["Autor"].'</a> en <a href="#">'.$fila["Categoria"].'</a></h4>
                                                    <p class="news_text_p">'
.$contenido.'</p>
                                                    <div class="ar">
                                                        <p><a class="leer-mas fr" href="leer_post.php?id='
.$fila['ID'].'">Leer Mas</a></p>
                                                    <div>
                                                </article>
                                            </div><!-- END OF ARTICLE-CONTAINER -->
                        '
; };
                            
//UNA VEZ Q MUESTRO LOS DATOS TENGO Q MOSTRAR EL BLOQUE DE PAGINACIÓN SIEMPRE Y CUANDO HAYA MÁS DE UNA PÁGINA
                            
if($num_rows != 0) {
                                
$nextpage$page +1;
                                
$prevpage$page -1?>
                               <ul id="pagination-clean"><?php
                                    
//SI ES LA PRIMERA PÁGINA DESHABILITO EL BOTON DE PREVIOUS, MUESTRO EL 1 COMO ACTIVO Y MUESTRO EL RESTO DE PÁGINAS
                                    
if ($page == 1
                                    {
                                    
?>
                                        <li class="previous-off">&laquo; Anterior</li>
                                        <li class="active">1</li> 
                                    <?php
                                        
for($i$page+1$i<= $lastpage $i++)
                                        {
?>
                                        <li><a href="noticias.php?cat=<?php echo $cat;?>&page=<?php echo $i;?>"><?php echo $i;?></a></li>
                                <?php }
                                    
//Y SI LA ULTIMA PÁGINA ES MAYOR QUE LA ACTUAL MUESTRO EL BOTON NEXT O LO DESHABILITO
                                    
if($lastpage >$page )
                                    {
?>      
                                        <li class="next"><a href="noticias.php?cat=<?php echo $cat;?>&page=<?php echo $nextpage;?>" >Siguiente &raquo;</a></li><?php
                                    
}
                                    else
                                    {
?>
                                        <li class="next-off">Siguiente &raquo;</li>
                                <?php
                                    
}
                                } else {
                                    
//EN CAMBIO SI NO ESTAMOS EN LA PÁGINA UNO HABILITO EL BOTON DE PREVIUS Y MUESTRO LAS DEMÁS
                                
?>
                                    <li class="previous"><a href="noticias.php?cat=<?php echo $cat;?>&page=<?php echo $prevpage;?>">&laquo; Anterior</a></li><?php
                                        
for($i1$i<= $lastpage $i++) {
                                                    
//COMPRUEBO SI ES LA PÁGINA ACTIVA O NO
                                        
if($page == $i) {
                                    
?>       <li class="active"><?php echo $i;?></li><?php
                                        
} else {
                                    
?>       <li><a href="noticias.php?cat=<?php echo $cat;?>&page=<?php echo $i;?>" ><?php echo $i;?></a></li><?php
                                        
}
                                    }
                                        
//Y SI NO ES LA ÚLTIMA PÁGINA ACTIVO EL BOTON NEXT     
                                    
if($lastpage >$page )
                                    {   
?>   
                                        <li class="next"><a href="noticias.php?cat=<?php echo $cat;?>&page=<?php echo $nextpage;?>">Siguente &raquo;</a></li><?php
                                    
} else {
                                
?>       <li class="next-off">Siguiente &raquo;</li><?php
                                    
}
                                }     
                            
?></ul><?php
                            

                        }
 
                        
?>                    
                </section><!-- fin section news -->
            </div><!-- fin de content-container -->