Monday, March 3, 2014

PHP Pagination

Paging button designed to be included into web pages.
First, create a file "index.php", copy the code below.



First query to get total rows of data
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);

Set the number of rows to be displayed in every pages
$max = 10;

$totalpage = ceil($rows/$max); 
$currentPage = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = "LIMIT ".($currentPage-1)*$max.",".$max;

Second query to display data
$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
     echo $result['column'];
}

So, in the index.php,
index.php
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
$query=mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($query);

$max = 10;

$totalpage = ceil($rows/$max); 
$currentPage = (isset($_GET['page'])) ? $_GET['page'] : 1;
$limit = "LIMIT ".($currentPage-1)*$max.",".$max;

$query2=mysql_query("SELECT * FROM table ".$limit."");
while($result = mysql_fetch_assoc($query2)){
     echo $result['column'];
}

include 'pagenum.php';
$page = "index.php";
echo getPageNum($totalpage,$currentPage,$page);
?>
</body>
</html>

Click links below to continue.
pagenum.php

No comments:

Post a Comment