hee,i had gone to many website ralated to PHP,and i found that too many peoples asking about how to pagination in PHP,here i will guide you through it with the easiest way
1.now we begin with this:
PHP Code:
if(!$page)
{
$page=1;
}
see those code above?it works like:if your page is not set yet,it will set your page to 1,mean page 1.
2.a little matimathicall here:
PHP Code:
$max=5;
$num=$page * $max - $max;
$totalpage = ceil($lines/$max)+1;
I will explain it:
+the variable $max is your max result in
1 page
+in the variable $Num,we have to think,example,your page is 2,then the result of $num is 5(so if the page is 3,it will pass 5 results again to go to the 10 result),so in the loop to display data,we will use this
+In variable $totalpage,
--$lines is your line(or row)in your data(hope u know how to speccify this)
--ceil mean Returns the next highest integer value by rounding up value if necessary. The return value of ceil() is still of type float as the value range of float is usually bigger than that of int. ,so it will give $totale page the interger value.
3.the pagination:
PHP Code:
if ($page > 1) {
echo "<a href=\"?page=".($page-1)."\">[Prev]</a> \n";
}
for($i=1; $i<$totalpage; $i++) {
if ($i == $page) {
echo "<b>[$i] </b>\n";
} else {
echo "<a href=\"?page=".$i."\">[$i]</a> \n";
}
}
if ($page < $totalpage-1) {
echo "<a href=\"?page=".($page+1)."\">[Next]</a>\n";
}
It simple to understand,so i wont explain this,(cuz i have to go to school now lolz :P)
4.now u done it,the whole code is here:
PHP Code:
(data display can goes here if u wnat the pagination under the result)
/*==========================Pagination============================*/
if(!$page)
{
$page=1;
}
$max=5;
$num=$page * $max - $max;
$totalpage = ceil($lines/$max)+1;
if ($page > 1) {
echo "<a href=\"?page=".($page-1)."\">[Prev]</a> \n";
}
for($i=1; $i<$totalpage; $i++) {
if ($i == $page) {
echo "<b>[$i] </b>\n";
} else {
echo "<a href=\"?page=".$i."\">[$i]</a> \n";
}
}
if ($page < $totalpage-1) {
echo "<a href=\"?page=".($page+1)."\">[Next]</a>\n";
}
echo "<BR>";echo "<a HREF='form.html'>sign!<BR></a>" ;
/*========================end of pagination========================*/
(or data display can goes here)
====================
My E is really terrible,so if u dont understand some point,just ask me,i`m willing tp answer you