Hi,
here is the example of Pagination
[php]
<?php
function Pages($tbl_name,$limit,$path)
{
$query = "SELECT COUNT(*) as num FROM $tbl_name";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];
$adjacents = "2";
$page = $_GET['page'];
if($page)
$start = ($page - 1) * $limit;
else
$start = 0;
$sql = "SELECT id FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
if ($page == 0) $page = 1;
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$lpm1 = $lastpage - 1;
$pagination = "";
if($lastpage > 1)
{
$pagination .= "
";
if ($page > 1)
$pagination.= "
« previous";
else
$pagination.= "
« previous";
if ($lastpage < 7 + ($adjacents * 2))
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "
$counter";
else
$pagination.= "
$counter";
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "
$counter";
else
$pagination.= "
$counter";
}
$pagination.= "...";
$pagination.= "
$lpm1";
$pagination.= "
$lastpage";
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "
1";
$pagination.= "
2";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "
$counter";
else
$pagination.= "
$counter";
}
$pagination.= "..";
$pagination.= "
$lpm1";
$pagination.= "
$lastpage";
}
else
{
$pagination.= "
1";
$pagination.= "
2";
$pagination.= "..";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "
$counter";
else
$pagination.= "
$counter";
}
}
}
if ($page < $counter - 1)
$pagination.= "
next »";
else
$pagination.= "
next »";
$pagination.= "
\n";
}
return $pagination;
}
//connect to the mysql
$db = @mysql_connect('localhost', 'root', '') or die("Could not connect database");
@mysql_select_db('pagination', $db) or die("Could not select database");
?>
Pagination In PHP
a {
color:#333;
text-transform: capitalize;
}
a:hover{
color: #999;
text-decoration:underline
}
<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$page = ($page == 0 ? 1 : $page);
$perpage = 9;//limit in each page
$startpoint = ($page * $perpage) - $perpage;
$sql = @mysql_query("select * FROM `tutorials` order by id desc LIMIT $startpoint,$perpage");
while($Row = mysql_fetch_array($sql)) {
echo ‘
’.$Row[‘title’].’
’;
}
//show pages
echo Pages(“tutorials”,$perpage,“index.php?”);
?>
Your limit per page is = '<?php echo $perpage;?>'
[/php]