Simple PHP Paging

Simple PHP Paging

In this post, I’ll try to share how to make a Simple PHP Paging. The first step we have to do is to prepare data that will be shown. In this case, I use mysql to store the data.

Let say, we have a table in mysql database with the name ‘match’ and have 999 rows. And, We want to show 20 members each page. I asume we already connect the php file with database. See (Connecting PHP to Mysql).

The first code is to define the links of page:

if(!isset($_GET['page'])){$page = 1;}else{$page = $_GET['page'];}//get the page
$row_per_page = 20;//defined rows per page
$rows = mysql_num_rows(mysql_query("SELECT * FROM  `match`"));// find out how many rows available
$pages = ceil($rows/$row_per_page);//find out how many pages
$width_links = 3;//defined width links

$start_link = $page - $width_links;//start link
if($start_link<1)$start_link=1;//set start link into 1 if less than 1
	echo '<a href="?page=1">First</a>&nbsp;.....';
	echo '&nbsp;<a href="?page='.$i.'">'.$i.'</a>';

echo '&nbsp;&nbsp;&nbsp;<strong>'.$page.'</strong>&nbsp;&nbsp;';

$end_link = $page + $width_links;
if($end_link>$pages)$end_link=$pages;//set end link into max page if more than max page
	echo '&nbsp;<a href="?page='.$i.'">'.$i.'</a>';
	echo '&nbsp;.....&nbsp;<a href="?page='.$pages.'">Last</a>';

In our code here, we will see not all of the available links. Because if the available page is … let say, 50, then it will be problem if the links for page 1 till page 50 is shown. Therefore we make a cut for the links. Then it will show links like:

Optional Code (CSS) to make no underline for links:


The second code is to show the data:

$limit = ($page - 1)*$row_per_page;//set the limit $row_per_page means that we will use defined rows in $row_per_page per page
$e=mysql_query("SELECT * FROM `match` LIMIT ".$limit.",".$row_per_page);//the query will show 20 rows each page
echo '<table><tbody>';
while($r = mysql_fetch_array($e)){	echo '<tr><td>'.$r['match_id'].'</td><td>'.$r['desc'].'</td></tr>';}
echo '</tbody></table>';

TARRRA… we just make a Simple PHP Paging.

Good Luck.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: