Hi, Guest ~ Login or Register

Simple Pagination

Simple Pagination

Listed In Uncategorized » — Viewing Full Tutorial
Well the idea of this pagination is from this site shoutbox, very simple but effective ^^ so here its my code :)


<?php
/*
SIMPLE PHP PAGINATION BY FEDEKILLER
*/
include("config.php");//Includes the configuration file
$table = 'tutorials_content';//Table name here
$limit = 42;//Limit of results here
$page = $_GET['page'];//Gets the page
$totalrows = mysql_num_rows(mysql_query("SELECT id FROM $table"));//Get the total rows of the table
if(empty($page))//If the page is empty
{
  $page = '1';//sets the page to 1
};
$start = ($page-1)*$limit;//set the start page
$start = round($start,0);//rounds it
$result = mysql_query("SELECT * FROM $table LIMIT $start, $limit");//makes the query, here you can add for example
//WHERE something='somethingelse'
while ($r = mysql_fetch_array($result))
{
  echo "$r[title]<br>";
};
$totalpages = $totalrows / $limit;//Gets the totalpages
$totalpages = ceil($totalpages);//rounds them to the bigger number, so if the limit is 10 and there are 11 results it will show 2 paegs instead of 1 :)
if($page == 1)//if the page is 1
{
  $actualpage = '[1]';//actial page 1
}
else
{
  $actualpage = "[$page]";//else actualpage is the one we get using the $_GET
}
if($page < $totalpages)//if the page is smaller than totalpages
{
  $nv = $page+1;//next page
  $pv = $page-1;//prev page
  $nextpage = "<a href=?page=$nv>></a>";//next page link
  $prevpage = "<a href=?page=$pv><</a>";//preg page link
  $firstpage = "<a href="?page=1">&laquo;</a>";//first page
  $finalpage = "<a href="?page=$totalpages">&raquo;</a>";//last page
}
if($page == '1')//if the page is 1
{
  $nv = $page+1;
  $nextpage = "<a href=?page=$nv>> </a>";
  $prevpage = "<";
  $firstpage = "&laquo;";
  $finalpage = "<a href="?page=$totalpages">&raquo;</a>";
}elseif($page == $totalpages){//is the page is equal than the totalpages
  $pv = $page-1;
  $nextpage = ">";
  $prevpage = "<a href=?page=$pv><</a>";
  $firstpage = "<a href="?page=1">&laquo;</a>";
  $finalpage = "&raquo;";
}
if($totalpages == '1' || $totalpages == '0'){//if totalpages is 1 or 0
    $nextpage = ">";
    $prevpage = "<";
    $firstpage = "&laquo;";
    $finalpage = "&raquo;";
}
echo "$firstpage $prevpage $actualpage $nextpage $finalpage<br>Pages: $totalpages";//echoes the pages at the botton of the file
?>


Hope you find this useful, any problem, pm me

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to rayjo! rayjo joined on Wednesday, 3rd December.

Sponsor

Shoutbox