kevin
kevin

Reputation: 234

how to create pagination with next and previous button?

code:

<table>
    <tr style="background: white url(bg.gif) repeat-x left top;color: #fff;">
      <th>College Id</th>
      <th>College Name</th>
      <th>Website</th>
      <th>Field</th>
      <th>City</th>
      <th>Action</th>
    </tr>

    <?php
      $per_page=10;
      if (isset($_GET["page"])) 
      {
      $page = $_GET["page"];
      }
      else {
      $page=1;
      }
      $start_from = ($page-1) * $per_page;

      $sql="select * from all_colleges LIMIT $start_from, $per_page";
      $result = mysqli_query($link,$sql);
      while ($row = mysqli_fetch_array($result)) 
      {
    ?>
    <tr>
      <td><?php echo $row['college_id']; ?></td>
      <td><?php echo $row['college_name']; ?></td>
      <td><?php echo $row['website']; ?></td>
      <td><?php echo $row['field']; ?></td>
      <td><?php echo $row['city']; ?></td>
      <td>
        <a class='view' title='view' href='view.php?id=<?php echo $row['college_id']; ?>'>
          <img src='gridview/view.png' alt='view' />
        </a>

        <a class='update' title='Update' href='update.php?id=<?php echo $row['college_id']; ?>'>
          <img src='gridview/update.png' alt='Update' />
        </a>

        <a class='delete' title='delete' href='delete.php?ad_id=<?php echo $row['college_id']; ?>'>
          <img src='gridview/delete.png' alt='delete' />
        </a>
      </td>
    </tr>
    <?php 
      }
    ?>
</table>

<div style="padding: 10px;">
  <?php
  $query = "select * from all_colleges";
  $result = mysqli_query($link, $query);
  $total_records = mysqli_num_rows($result);
  $total_pages = ceil($total_records / $per_page);
  echo "<center><a href='admin.php?page=1' style='padding:5px;'>".'previous'."</a>";
  for ($i=1; $i<=$total_pages; $i++) {
  echo "<a href='admin.php?page=".$i."'>".$i."</a>";
  }
  echo "<a href='admin.php?page=$total_pages'>".'next'."</a></center>";
  ?>
</div>

In this code I want to create a pagination with next and previous button here this code is working properly but it show diffrent pagination i.e. if the number of results is 200 then it will display like

previous 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 next

but I want like

previous 1 2 3 4 5 ....8 9 next

How can I do this please help ?

Thank You

Upvotes: 0

Views: 2290

Answers (1)

Imanuel
Imanuel

Reputation: 3667

You can check each iteration of your loop if you want to display the number.
In the following code, only:

  • the first two pages
  • the last two pages
  • the current page
  • each two pages to the left and to the right of the current page

are displayed.

$skipped = false;
for ($i = 1; $i <= $total_pages; $i++)  {
    if ($i < 3 || $total_pages- $i < 3 || abs($page - $i) < 3) {
        if ($skipped)
            echo '<span> ... </span>';
        $skipped = false;
        echo "<a href='admin.php?page=" . $i . "'>" . $i . "</a>";
    } else {
        $skipped = true;
    }
}

If you have a lot of pages, the big loop is unnecessary.
Instead, you could use three different loops:

$done = [];

for ($i = 1; $i < 3; $i++) {
    $done[$i] = true;
    //echo 
}

if ($page > 3)
   echo '<span> ... </span>';

for ($i = $page - 2; $i < $page + 3; $i++) {
    if (isset($done[$i])
        continue;

    $done[$i] = true;
    //echo
}

if ($page < $total_pages - 3)
   echo '<span> ... </span>';

for ($i = $total_pages- 2; $i < $total_pages; $i++) {
    if (isset($done[$i])
        continue;

    $done[$i] = true;
    //echo
}

To fix your two other buttons (previous and next), link to $page - 1 and$page + 1 instead of 1 and $total_pages.

Upvotes: 3

Related Questions