Simple PHP Pagination

After great fuss I came to this below code and this not only helps to paginate, also helps to break
the large amount data fetch through query, which exceed the page load time.

//Get the page number
$p = $_GET['page']; 

// Counting total rows
$pages_query = mysql_query("SELECT COUNT(id) FROM demo_data"); 

//checking if p is set and greater than 0
if($p=="" || $p=="0" || $p>$pages_query) { 
      //if not set than setting it to 1
      $p = 1; 

//Total data to display per page
$per_page = 3; 

//dividing total rows with total data to display for example 20/10=2 so 2 pages
$pages = ceil(mysql_result($pages_query, 0) / $per_page); 

// subtracting $p value with 1 and multiplying it with $per_page for                 
// example 2-1=1*10 = 10
$start = ($p - 1) * $per_page;

//Running our query
$query = mysql_query("SELECT * FROM demo_data ORDER BY id ASC LIMIT $start, $per_page");      

//fetching data using array method
while($fetch_data = mysql_fetch_array($query)) { 

    //printing the data
    echo "---------".$fetch_data["1"]." ". $fetch_data["2"].""; 


//using for to display number
for($a=1;$a<=$pages;$a++) { 

    //printing numbers also using link tags
    echo "$a "; 



Be the first to comment

Leave a Reply