Posted by Thurein on 11th November 2009

PHP Pagination

I have look for php pagination source. I got one very nice source code from Stranger Studio. That very nice code if your have a lot of records. You can see at sample preview.

PHP Pagination

However, this is a lot of lines in the file. If you want to put into a lot of pages, all of code you need to put into those pages. We have to take care about this. That’s while I have changed to class and functions. That’s very easy to use. I will share it back.

Firstly I have created class and function in the paganition.php file.

For usage :


require_once('pagination.php');
 $sql = 'SELECT * FROM contact';
 $page_name = "page.php";

 //  pagination($query, $adjacents, $row_per_page, $page_name)
 $pager = new pagination($sql, 3, 20, $page_name );
 $rs = $pager->paginate();

 while($each = mysql_fetch_assoc($rs))
 {
 // Your Loop Here
 echo $each[name];
 }

 // Pages Navagation
 echo $pager->pages();

So, Page links will be come like “pagename.php?page=” . If you would like to use with .htaccess some think like http://www.yourdomain.com/content/page/page_number , you can easily to change at pagination.php . Find, “?page=” and replace with “/” . Then, you can use at page name like


$page_name = "http://www.yourdomain.com/content/page";

In CSS.


div.pagination {
 padding: 3px;
 margin: 3px;
}

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.pagination 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;
}

Download Source ( size 2.6 kB)

  • About Thurein Soe
    Visit Thurein's website.

    Freelance Web Developer. Founder of The Tech Space. Follow me on Twitter.

  • No comments yet!

    Post your comments