A Friend in Need is a Friend in Deed.
Become a Member at ITTreats.com !! Share Your invaluable Experience to Fellow Programmers. Signup, Start Blogging and support fellow Techies.

PHP pagination tutorial

Use Pagination to display results of your MySQL query over multiple pages in PHP. This php pagination tutorial will help you out in sorting out problems with pagination

As your database grows, showing all the results of a query on a single page is no longer practical. This is where pagination comes in handy. You can display your results over a number of pages, each linked to the next, to allow your users to browse your content in bite sized pieces.This php pagination tutorial will help you out.

PHP Pagination Tutorial

1. get the total no of rows for the search query

1
2
3
4
5
6
7
$query2=” SELECT count(*)  FROM pagingstu  “;
$result2=mysql_query($query2);
echo mysql_error();
 
$rowww=mysql_fetch_array($result2,MYSQL_BOTH);
 
$nume=$rowww[0];

2. Now,  set the  page limit implicitly  or explicitly

1
$limit=20;

3. Now,  make  a  querystring variable  pno and  get the  variable value

1
2
3
$start=0;
if (isset($_GET[’start’]))
$start=$_GET[’start’]

4. dynamically change the  start index  to  the recordno  with   $start=$limit*($start-1) and  try the search query  with limit attribute  as follows

1
2
3
4
5
$start1=$start;
$start=$limit*($start-1);
$query=” SELECT * FROM pagingstu  limit $start, $limit;
//echo $query;
$result=mysql_query($query);

5. process the display part of the query  result

6. after that , for paging links code the following lines

// Pagination Starts Here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
echo "<table align = 'center' width='50%'><tr><td  align='left' width='20%'>";
 
if ($start1&gt;1)
 
{
 
$j=$start1-1;
 
echo " <a href='$page_name?start=$j' title='Prev Page'><font face='Verdana' size='2'>« Prev</font></a> ";
 
}
 
for($i=$start1-5;$i &lt; $start1+5 ;$i++){
 
if($i>0){
 
if ($i==$start1)
 
echo " <font face='Verdana' size='2'><b>".$i."</b></font> ";
 
else
 
echo " <a href='$page_name?start=$i' title='".$i." Page'><font face='Verdana' size='2'>".$i."</font></a> ";
 
}
 
}
 
if ($start1<($nume/$limit))
 
{
 
$j=$start1+1;
 
echo "<a href='$page_name?start=$j' title='Next Page'><font face='Verdana' size='2'>» Next</font></a> ";
 
}

So simple is’t it ??

Article Written by : adrevol
( Articles Submitted : 47 Articles Commented : )




Leave a Reply