PHP pagination - How to enable-disable previous-next buttons in pagination using PHP?

Viewed 71

I want to extract all the rows from the database of a table and display it 5 by 5 so that the user has two buttons next and previous and these are blocked if they can continue or not. As much as I search, I only find how to show all the data in one go and that doesn't work for me, I need them to go 5 by 5

With a loop that shows 5 by 5 and I put on the button to restart the counter, it could be done, but how do I block the button when I can no longer go forward and how do I go back and block the backward button?

I have tried the following but the button does not modify the values ​​of the variables

<?php
    define("DB_HOST","localhost" );
    define("DB_USER", "root");
    define("DB_PASS", "");
    define("DB_DATABASE", "blog" );
    $mysql = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
    if($mysql->connect_errno > 0){
        die("Imposible conectarse con la base de datos [" . $mysql->connect_error
        . "]");
    }
    

if( isset( $_POST['submit'] ) ){
        if(!empty( $_POST['minimo'] ) ){
            $minimo = array_keys($_POST['minimo']);
            $maximo = $minimo + 5;
        }else{
            $minimo = array_keys($_POST['maximo']);
            $maximo = $minimo + 5;
        }
    }
else {
    $minimo = 0;
    $maximo = 5;
}
    


    $seleccion = "SELECT COUNT(*) FROM posts";
    $resultado=$mysql->query($seleccion);
    $totalPosts = $resultado->fetch_array(MYSQLI_NUM)[0];

    $seleccion = "SELECT * FROM posts ORDER BY id LIMIT " . $minimo.", " . $maximo;
    $resultado=$mysql->query($seleccion);

    if ($mysql->errno) die("Error ". $mysql->error);
?>

<!DOCTYPE html>
<html lang="es">
    <head>
        <title>Blog PHP</title>
        <meta name="author" content="MiguelDAD"/>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <link rel="stylesheet" type="text/css" href="./main.css"/>
    </head>
    <body>
        <table>
            <tr>
                <th>Título</th>
                <th>Descripcion corta</th>
                <th>Imagen</th>
                <th></th>
            </tr>
            <?php
                while($fila = $resultado -> fetch_assoc()){
                    echo "<tr>";
                    echo "<td>". $fila['titulo']."</td>";
                    echo "<td>". $fila['desc_corta']."</td>";
                    echo "<td><img src=".$fila['imagen']."></img></td>";
                    echo "<td><img src='./imagenes/lapiz.png' class='icono'></img></br></br>
                    <img src='./imagenes/borrar.png' class='icono'></img></td>";
                    echo "</tr>";
                }
            ?>
        </table>
        <form action="index.php" method="post">
            <?php
                if ($minimo == 0) {
                    echo '<input type="submit" disabled="disabled" value="Anterior" class="botonAnterior"/>';
                } else {
                    echo '<input type="submit" name="minimo['.($minimo-5).']"  value="Anterior" class="botonAnterior"/>';
                } 

                if ($maximo >= $totalPosts) {
                    echo '<input type="submit" disabled="disabled" value="Siguiente" class="botonSiguiente"/>';
                } else {
                    echo '<input type="submit" name="maximo['.($minimo+5).']" value="Siguiente" class="botonSiguiente"/>';
                    
                } 


            ?>
        </form>    
    </body>
</html>
1 Answers

you need to know already the number of page you gonna have (number of rows / number of row that should be displayed) and then you can use this number to display or not prev or next button

pseudo code

$nbrItemPerPage = 5;
$totalPages = $nbrRows / $nbrItemPerPage;

if $current_page >= $totalPages
    block btn-next

else if $current_page <=1 //first_page
    block btn-previous 
Related