kreoton web development

Archive for June, 2008

Easy pagination for your PHP applications

Friday, June 20th, 2008

Today i decided to share with you my pagination class. Some time ago I had posted article where was similar pagination function but it was a little bit hard to implement. Now this class has language support, easy tags change, easy style class change and some more useful features.

Before we start you should download class following this link.

Some examples how you should use my class for your applications:

First of all you should include file to your application and create new pagination object.

1
2
3
4
5
6
7
<?php
//include class in your application
require 'paginate.php';
 
//create new object
$pg = new paginate();
?>

Now you should extract witch page you are viewing right now. Extraction is different for SEO friendly url and for simple urls where current page is in $_GET variable so here is examples for $_GET and SEO friendly urls.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
$sql_limit = 20;
//for links that uses $_GET
if (isset($_GET['page']) && (int)$_GET['page'] > 0)
{
    $page = (int)$_GET['page'];
    //for sql LIMIT start
    $sql_start = ($page-1)*$sql_limit;
}
else
{
    $page = 1;
    $sql_start = 0;
}
 
//for links like page-2.html i use do like this
$page = explode('-', $_SERVER['REQUEST_URI']);
$page = array_reverse($page);
$page = substr($page[0], 0, -5);
 
if ((int)$page > 0)
{
    $page = (int)$page;
    //for sql LIMIT start
    $sql_start = ($page-1)*$sql_limit;
}
else
{
    $page = 1;
    $sql_start = 0;
}
?>

After these steps we have pagination object, current page and sql LIMIT start. Now we can write sql for page data.

1
2
3
4
5
6
7
<?php
$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM data_table LIMIT ".$sql_start.",".$sql_limit);
//select how many rows is in Database
$total = mysql_query("SELECT FOUND_ROWS() as total");
$total = mysql_fetch_assoc($total);
$total = $total['total'];
?>

Next you should configure and print pagination:

1
2
3
4
5
6
7
8
9
10
11
<?php
$pg->assignBaseTemplate('<ul class="paginate">', '</ul>'); //can be skipped default values are <ul>,</ul>
$pg->assignLinkTemplate('<li>', '</li>'); //can be skipped default values are <li>,</li>
$pg->assignAnchorTemplate('selected', ''); //this is link style classes default are 'link_selected', ''
$pg->assignSerperator('...'); //to separate links intervals like 1 2 3 ... 12 13
//for pagination urls for $_GET use '?page=%d' default are '/%d/'
$pg->assignLinkFormat('/page-%d.html');
$pg->assignLanguage('Next', 'Previous'); //can be skipped default 'Next', 'Previous'
//now print our pagination
echo $pg->delegate($page, $total, 'http://www.example.com/news', $sql_limit);
?>

Here is style sheet for pagination that i use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ul.paginate {
	text-align: right;
    list-style: none;
    padding: 10px 0 10px 0;
}
ul.paginate li {    
    display: inline;
    padding: 0 2px 0 2px;
}
ul.paginate li a {
    color:#CC0000;
    text-decoration: none;
    padding: 2px 3px 2px 3px;
    border: 1px solid #EFEDE9;
}
ul.paginate li a.page_active {
    color:#CC0000;
    background: #DBD7CE;
}

And you are done. Big thanks for everyone who would use this class and place link back to my page.