0

How to implement a pagination in this code:

<?php
    include "connect.php";
    $query  = mysql_query("SELECT username, date FROM users ORDER BY id ASC");
    while($row  = mysql_fetch_array($query)):
?>
        <div><h5><?php echo $row['username']; ?> (<?php echo $row['date']; ?>)</h5></div>
<?php endwhile; ?>
q7eb2a
  • 11
  • 1
  • 1

6 Answers6

3

The idea is to play with mysql LIMIT keyword. It lets you limit the number of results you will get from mysql.

SELECT data FROM table LIMIT 5 

is equivalent to

SELECT data FROM table LIMIT 0, 5

Where 0 is the offset and 5 the number of row returned by MySQL.

So you have to fix a number of item to display, let's say $numRes = 10 and a page number:

if (isset($_GET['page']) && is_numeric($_GET['page']))
     $page = $_GET['page'];
else
     $page = 0;

So you request is something like:

sprintf($request, "SELECT data FROM pages LIMIT %d, %d", $page, $numRes);
Aif
  • 11,015
  • 1
  • 30
  • 44
1
class Pagination
{
    public $cur_page;
    public $total;
    public $per_page;

    function __construct($cur_page, $total, $per_page)
    {
        $this->cur_page = $cur_page;
        $this->total = $total;
        $this->per_page = $per_page;
    }

    function getTotalPage(){
        return ceil($this->total / $this->per_page);
    }

    function hasPrevPage(){
        if($this->cur_page > 1){
            return true;
        }
        else{
            return false;
        }
    }

    function hasNextPage(){
        if($this->cur_page < $this->getTotalPage()){
            return true;
        }
        else{
            return false;
        }
    }

    function offSet(){
        return ($this->cur_page - 1) * $this->per_page;
    }
}

$total = 12;
$per_page = 5;
$cur_page = 1;

$pagination = new Pagination($cur_page, $total, $per_page);
$offSet = $pagination->offSet();
$query = "SELECT username, date FROM users ORDER BY id ASC LIMIT {$offset}, {$per_page}";
netto
  • 135
  • 1
  • 3
  • 11
0

Pagination in PHP/MySQL is simply attained by using the LIMIT clause.

Initially you have to count the total records and divide it by the records per page. Pass the page number along with REQUEST.

Read the page number from the REQUEST array and find out the starting record of the page.

You can further read and see the complete implementation on the following page http://www.csnotes32.com/2014/11/page-wise-or-paginated-listing-of-data.html

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
ANAND
  • 11
  • 2
0

This is code for pagination. Use this with your database and you will get Google style pagination:

 <?php

  $conn=mysqli_connect("localhost","root","","ui");


  $start=0;
   $limit=5;

      $t=mysqli_query($conn,"select * from form_table");
      $total=mysqli_num_rows($t);



       if(isset($_GET['id']))
       {
            $id=$_GET['id'] ; 
                            $start=($id-1)*$limit;

                              }
                else
                {
            $id=1;
   }
     $page=ceil($total/$limit);

       $query=mysqli_query($conn,"select * from form_table limit                                        $start,$limit");
     ?>
  <!DOCTYPE html>
<html>
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet"       href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script s        src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">     </script>
     <script                    src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">          </script>
      </head>         
     <body>

    <div class="container">
     <h2>Table</h2>
        <table class="table table-bordered">
        <thead>
          <tr>
           <th>Id</th>
             <th>Name</th>
           <th>Gender</th>


    <th>Hobbies</th>
    <th>Course</th>
  </tr>
  </thead>
    <tbody>

<?php
while($ft=mysqli_fetch_array($query))
{?>
 <tr>
    <td><?= $ft['0']?></td>
    <td><?= $ft['1']?></td>
    <td><?= $ft['2']?></td>
    <td><?= $ft['3']?></td>
    <td><?= $ft['4']?></td>
  </tr>
<?php
}

?>


</tbody>
  </table>
     <ul class="pagination">
 <?php if($id > 1) {?> <li><a href="?id=<?php echo ($id-1) ?       >">Previous</a></li><?php }?>
 <?php
 for($i=1;$i <= $page;$i++){
 ?>
    <li><a href="?id=<?php echo $i ?>"><?php echo $i;?></a></li>
  <?php
 }
  ?>
<?php if($id!=$page)
//3!=4
{?> 
Bugs
  • 4,491
  • 9
  • 32
  • 41
Mathew Coder
  • 29
  • 1
  • 1
  • 5
0
<?php
    require_once "connect.php";
    define('PER_PAGE',5);
    if (isset($_GET['page']) && is_numeric($_GET['page'])):  // in your href use "domain.com/index.php?page=1"
        $start = $_GET['page_name'] * PER_PAGE;
        $query  = mysql_query("SELECT username, date FROM users ORDER BY id ASC LIMIT {$start},{$per_page}"); // where to start and for how many 1-5
        while ($row = mysql_fetch_array($query)): ?>
            <div><h5><?php echo $row['username']; ?> (<?php echo $row['date']; ?>)</h5></div>
            <?php
        endwhile;
    endif;
?>
Chris McClellan
  • 1,123
  • 7
  • 14
0

this is code for pagination in php##

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" id="font-awesome-style-css" href="https://www.phpflow.com/code/css/bootstrap3.min.css" type="text/css" media="all">
<!-- jQuery -->
<script type="text/javascript" charset="utf8" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js"></script>
<title>phpflow.com : Source code of simaple ajax pagination</title>
</head>
<body>
<div><h3>Source code : PHP simaple ajax pagination</h1></div>
<div>
<div id="target-content" >loading...</div>

<?php
include('db.php'); 
$limit = 4;
$sql = "SELECT COUNT(id) FROM coffee";  
$rs_result = mysqli_query($conn, $sql);  
$row = mysqli_fetch_row($rs_result);  
$total_records = $row[0];  
$total_pages = ceil($total_records / $limit); 
?>
<div align="center">
<ul id="pagination">
<?php if(!empty($total_pages)):for($i=1; $i<=$total_pages; $i++):  
      if($i == 1):?>
            <h3 class='active'  id="<?php echo $i;?>" style="float: left;"><a href='pagination.php?page=<?php echo $i;?>' ><?php echo $i;?></a></h3> 
      <?php else:?>
      <h3 id="<?php echo $i;?>" style="float: left;"><a href='pagination.php?page=<?php echo $i;?>'><?php echo $i;?></a></h3>
    <?php endif;?>      
<?php endfor;endif;?>  
</div>
</div>
</body>
<script>
jQuery(document).ready(function() {
jQuery("#target-content").load("pagination.php?page=1");
    jQuery("#pagination h3").live('click',function(e){
  e.preventDefault();
    jQuery("#target-content").html('loading...');
    jQuery("#pagination h3").removeClass('active');
    jQuery(this).addClass('active');
        var pageNum = this.id;
        jQuery("#target-content").load("pagination.php?page=" + pageNum);
    });
    });
</script>

##