Nazmul
Nazmul

Reputation: 115

php pagination code is not working

Can anyone point me out why my pagination is not working, What I am doing wrong here ? Working on it for a long time. my url generates like : http://localhost/medapp/admin/medorder.php?page=%209.

 <?php
   //pagination
    $perpage = 3;
    if (isset($_GET["page"])) {
        $page = $_GET["page"];
        }
        else {
          $page=1;
        }
    $start_from = ($page-1)*$perpage;
  //pagination

     $medorder = "SELECT * FROM `medorder` WHERE status='1'  order by ID desc";
     $result = $db->select($medorder);
      if($result){
         $i=0;
        while($row = $result->fetch_assoc()) {
        echo "<tr>";
          echo "<td>".$i++."</td>";
          echo "<td>".$row["uid"]."</td>";
          echo "<td>".$row["fullname"]."</td>"; 
          echo "</tr>";
       }
// pagination
    $query  = "select * from medorder";
    $result = $db->select($query);
    $total_rows = mysqli_num_rows($result);
    $total_pages = ceil($total_rows/$perpage); 
    echo "<span class='pagination'><a href='medorder.php?page=1'>".'First Page'."</a>";
    for ($i=1; $i <= $total_pages; $i++) { 
    echo "<a href='medorder.php?page=".$i."'>".$i."</a>";  }
    echo "<a href='medorder.php?page=$total_pages'>".'Last Page'."</a></span>";
                         //pagination
    }

  ?>

Upvotes: 0

Views: 58

Answers (2)

Arun Krish
Arun Krish

Reputation: 2153

Change your query like this by using LIMIT and OFFSET

$medorder = "SELECT * FROM `medorder` WHERE status='1'  order by ID desc LIMIT $start_from,$perpage"; //

Upvotes: 2

Alex
Alex

Reputation: 433

in this following line, you have a space before the $i variable:

echo "<a href='medorder.php?page= ".$i."'>".$i."</a>";

should be

echo "<a href='medorder.php?page=".$i."'>".$i."</a>";

Upvotes: 1

Related Questions