Skip to content
Advertisement

pagination show an extra page

I am currently doing a paging system and I have an error that the “NEXT” button continues to work despite having reached the last page, when clicking this it takes me to an extra page without content.

The “next” button should disappear on page 2 but it is continuous and sends me to page 3 where there is no content.

CODE

$display_pagina = 5;

if(isset($_GET['pagina'])){
    $pagina = $_GET['pagina']; 
} else{
    $pagina = 1;
}


$inicio = ($pagina-1)*$display_pagina;

$query = "SELECT p.*, t.name, e.status FROM post AS p LEFT JOIN tipo as t ON (t.id = p.tipo_id) LEFT JOIN estado as e ON (e.id = p.estado_id) ORDER BY id DESC LIMIT $inicio, $display_pagina";

$result = mysqli_query($link, $query);

                <div class="info-list" id="info-list-id">
                    
                    <?php 
                    while($post = mysqli_fetch_assoc($result)){
                    ?>
                    
                    <a class="boton" href="info/<?php echo $post['url_slug']?>">
                    <div class="info">
                        <div class="info-content">
                            
                            <img src="<?php echo '/images/info/' . $post['image']; ?>" class="info-image"/>
                    <p class="info-title"><?php echo $post['title'];?></p>
                            <p class="info-type"><?php echo $post['name']?></p>
                        </div>
                        </div>
                        </a>
                    <?php } ?>
                        
                    </div>
    
    
            <div class="catalogo-pagination">
                <?php 
                
                $query = "SELECT p.*, t.name, e.status FROM post AS p LEFT JOIN tipo as t ON (t.id = p.tipo_id) LEFT JOIN estado as e ON (e.id = p.estado_id)";
                
                
                $result = mysqli_query($link, $query);
                
                $total_registros = mysqli_num_rows($result);
                
                $total_paginas = ceil($total_registros/$display_pagina);
                
                
                
                
                if($pagina>1){
                echo "<li><a href='info.php?pagina=".($pagina-1)."'>Previous</a></li>"; 
                }

                for ($i=1; $i<=$total_paginas; $i++) {  
                echo "<li><a href='info.php?pagina=".$i."'>".$i."</a></li>";
                };  

                if($i>$pagina){
                echo "<li><a href='info.php?pagina=".($pagina+1)."' class='button'>NEXT</a></li>";
                }
                
                echo "</ul>";          
                
                ?>

Advertisement

Answer

Since you only want to show the next link if the total pages are higher than the current page number, you can change the condition to:

if ($total_paginas > $pagina) {
    echo "<li><a href='info.php?pagina=".($pagina+1)."' class='button'>NEXT</a></li>";
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement