Tutorials Home Scripts Home

How To Create A Pagination In PHP

How To Create A Pagination In PHP

Overview


This is a script to make pagination in PHP with (Previous Next) buttons.This is what you'll get after finished this code


http://img14.imageshack.us/img14/4281/32313001.gif



Syntax:


Pages($tbl_name,$limit,$path);

Parameters

tbl_name
your mysql table e.g(jooria_users) and use normal mysql querys e.g(jooria_users where groupid='5')
limit
how many items to show per page.
path
is the name of file ex. I saved this file as pagination.php, my $path should be "pagination.php?". I saved this file as home/pagination/ my $path should be "home/pagination/" to appear "home/pagination/page="

dowwnload

you can download the code with the examples + 3 css styles:Pagination.zip [3.87 KB]

Usage:

use this function to Creates Full Pagination to creat a new Pagination use this:

Pages("table","limit to appear","the path");

Example#1:

i have a table in my mysql named 'my_users' contain some users table

echo Pages("my_users","20","path/users/");

this will Return all users in pages and pages urls will be: /path/users/page=(page number)

The function

pagination.php [2.82 KB]
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 .= "<div class='pagination'>";
if ($page > 1)
$pagination.= "<a href='".$path."page=$prev'>« previous</a>";
else
$pagination.= "<span class='disabled'>« previous</span>";   

if ($lastpage < 7 + ($adjacents * 2))
{   
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='current'>$counter</span>";
else
$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
}
elseif($lastpage > 5 + ($adjacents * 2))
{
if($page < 1 + ($adjacents * 2))       
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class='current'>$counter</span>";
else
$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
$pagination.= "...";
$pagination.= "<a href='".$path."page=$lpm1'>$lpm1</a>";
$pagination.= "<a href='".$path."page=$lastpage'>$lastpage</a>";       
}
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href='".$path."page=1'>1</a>";
$pagination.= "<a href='".$path."page=2'>2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='current'>$counter</span>";
else
$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
$pagination.= "..";
$pagination.= "<a href='".$path."page=$lpm1'>$lpm1</a>";
$pagination.= "<a href='".$path."page=$lastpage'>$lastpage</a>";       
}
else
{
$pagination.= "<a href='".$path."page=1'>1</a>";
$pagination.= "<a href='".$path."page=2'>2</a>";
$pagination.= "..";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class='current'>$counter</span>";
else
$pagination.= "<a href='".$path."page=$counter'>$counter</a>";                   
}
}
}

if ($page < $counter - 1)
$pagination.= "<a href='".$path."page=$next'>next »</a>";
else
$pagination.= "<span class='disabled'>next »</span>";
$pagination.= "</div>\n";       
}


return $pagination;
}

The Css:

style1.css | style2.css | style3.css
div.pagination {
padding: 3px;
margin: 3px;
text-align:center;
}

div.pagination a {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #AAAADD;

text-decoration: none; /* no underline */
color: #000099;
}
div.pagination a:hover, div.digg a:active {
border: 1px solid #000099;

color: #000;
}
div.pagination span.current {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #000099;

font-weight: bold;
background-color: #000099;
color: #FFF;
}
div.pagination span.disabled {
padding: 2px 5px 2px 5px;
margin: 2px;
border: 1px solid #EEE;

color: #DDD;
}

Category: Website Programming | Views: 17,693 | on: September 16, 2009 | by: jooria
“Subscribe and get the new Tutorials”

Enter your email address:

Last 15 Responses

Comment form

  1. flash About 4 months ago
    Hi, Thanks for this pagination code, it works really well and seems clean. Would you mind if I shown this code on my own blog and then made a reference/link to your site as the original source? thanks a lot.
  2. jooria About 4 months ago
    sure you can thanks a lot
  3. Molty About 4 months ago
    Hi there. Nice toturial. It helped me ALOT. This will make my site much better =D Thank you. (thumb up)
  4. wcet About 3 months ago
    great code
    thank you!
  5. Pradeesh About 3 months ago
    Nice one......! Thank You......!! Can I modify this code and use in web pages I create..?
  6. jooria About 3 months ago
    sure my dear its for public use.
  7. Tariq Mahmood Abbasi About 3 months ago
    nice code! its really great
  8. gaby About 2 months ago
    great bro.. just like a jquery pagination.. but so wierd code if else statement without {} ^^ if(){ } else { }
  9. jooria About 2 months ago
    its not important to use {} ^^ if(){ }
  10. Mehedi Hasan About 2 months ago
    Thanks a lot for sharing this very useful script.
  11. Alex About 2 months ago
    Here is a video tutorial I created about PHP pagination in my blog. http://www.webmastervideoschool.com/blog_item.php?id=15 Hope you will find it useful.
  12. martin About 4 weeks ago
    Hi i have a problem my pages start at zero and i want them to show as page one and the link to still show the zero so it would look like this 1 2 3 4 <---- the link for number one should be page=0 and link for number 2 should be page=1 ect any idea how i can do that
  13. jooria About 4 weeks ago
    @martin
    i think this line in the code work on this problem:
    if ($page == 0) $page = 1;
    and in your query: if you tring to get for example the rows from table use this method
    $page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
    $page = ($page == 0 ? 1 : $page);
    $perpage = 10;
    $startpoint = ($page * $perpage) - $perpage;
    
    $sql = mysql_query("select * from anything LIMIT $startpoint,$perpage");
    
    echo Pages("my_table wherecondetion_one='0'",$perpage,"page.php?");
    

    and maybe you delete line 16 from the code
    thanks
  14. martin About 4 weeks ago
    @jooria thanks the if ($page == 0) $page = 1; fixes showing the right page number but the problem am having is i need the actual links to show one number differnt to what the page numbers are so say on page 1 it shows page 1 but i need the link to be page=0 and for page 2 i need the link to be page=1 and so on for the other pages Thanks for your time and help
  15. martin About 4 weeks ago
    just a quick update on my problem i was having.

    I put the $counter into an array and used -1

    $arr = array($counter-1);


    and then in my link i used page=$arr[0]

    cheers and thanks for the great script

Leave a Comment

Comment form