Blog > detail

Just my tiny mind

Simple PHP Paging

26-2-2012 Admin General 2 Comments

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

if($start_link1){

echo 'First .....';

}

for($i=$start_link;$i<$page;$i++){

echo ' '.$i.'';

}


echo '   '.$page.'  ';


$end_link = $page + $width_links;

if($end_link>$pages)$end_link=$pages;//set end link into max page if more than max page

for($i=$page+1;$i<=$end_link;$i++){

echo ' '.$i.'';

}

if(($end_link)<$pages){

echo ' ..... Last';

}

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:


.link{

text-decoration:none;

}


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 '

';

while($r = mysql_fetch_array($e)){ echo '

';}

echo '

'.$r['match_id'].''.$r['desc'].'
';

TARRRA... we just make a Simple PHP Paging.


Good Luck.

Simple PHP Paging Simple PHP Paging Simple PHP Paging

Simple <a href=PHP Paging" width="1" height="1" />

word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word

Simple PHP Paging

Simple PHP Paging

Simple PHP Paging