4
votes

I'm trying to learn PHP pagination. So far I can paginate through numbers, but previous/next buttons are appearing on the left and right of each page number, like this.

Here is the code I am using to generate pagination:

    <!DOCTYPE html>
<html lang="en">
<head>
  <title>PHP</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>List Of Customers</h2>

<div class="row">   

    <a href="add.php" class="btn btn-success">Create</a>

  <table class="table table-bordered">
    <thead>
      <tr>
        <th>Name</th>
        <th>Last Name</th>
        <th>Address</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Action</th>  
      </tr>
    </thead>
    <tbody>


<?php

include 'db.php';


$link = db_connect();



$limit = 4; 


    if (isset($_GET["page"] )) 
        {
        $page  = $_GET["page"]; 
        } 
    else 
       {
        $page=1; 
       };  

$record_index= ($page-1) * $limit;      






 $sql = "SELECT * FROM student_form LIMIT $record_index, $limit";

   $retval = mysqli_query($link, $sql);
//  print_r($retval);


   if (mysqli_num_rows($retval) > 0) {

         while($row = mysqli_fetch_assoc($retval)) {

        echo '<tr>';

                            echo '<td>'. $row['first_name'] . '</td>';
                            echo '<td>'. $row['last_name'] . '</td>';
                            echo '<td>'. $row['address'] . '</td>';
                            echo '<td>'. $row['phone'] . '</td>';
                            echo '<td>'. $row['email'] . '</td>';
                            echo '<td>'. "<a class='btn btn-primary' href=edit.php?id=".$row["id"].">&nbsp;&nbsp;Edit&nbsp;&nbsp;&nbsp;&nbsp;</a><br>"."<a class='btn btn-danger' href=delete.php?id=".$row["id"].">&nbsp;&nbsp;Delete</a><br>";
     }


} 
    else {
    echo "0 results";
}



echo "</tbody>";  
 echo "</table>";


$sql = "SELECT COUNT(*) FROM student_form"; 
$retval1 = mysqli_query($link, $sql);  
$row = mysqli_fetch_row($retval1);  
$total_records = $row[0];
    //  echo $total_records;
$total_pages = ceil($total_records / $limit);  
//$pagLink = "<div class='pagination'>";  
for ($i=1; $i<=$total_pages; $i++) {  

    echo "<ul class='pagination'>";

    echo "<li><a href='index.php?page=".($page-1)."' class='button'>Previous</a></li>"; 
    echo "<li><a href='index.php?page=".$i."'>".$i."</a></li>";
    echo "<li><a href='index.php?page=".($page+1)."' class='button'>NEXT</a></li>";


    echo"</ul>";               
};  
//echo $pagLink . "</div>";     
    mysqli_close($link);
?>


    </div>
</div>

</body>
</html>
1

1 Answers

6
votes

Move these urls out of for loop:

echo "<ul class='pagination'>";
echo "<li><a href='index.php?page=".($page-1)."' class='button'>Previous</a></li>"; 

for ($i=1; $i<=$total_pages; $i++) {  
    echo "<li><a href='index.php?page=".$i."'>".$i."</a></li>";
};  

echo "<li><a href='index.php?page=".($page+1)."' class='button'>NEXT</a></li>";
echo "</ul>";