Pagination Class Critique
Posted: Fri Jan 16, 2009 2:08 am
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hey Everybody,
So I've been working on writing classes to speed up development in my projects, and my first one is a pagination class. I would like to hear what you guys think of it, as I'm new to writing classes and maybe you think I could be more efficient or use better practices. Code below
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Hey Everybody,
So I've been working on writing classes to speed up development in my projects, and my first one is a pagination class. I would like to hear what you guys think of it, as I'm new to writing classes and maybe you think I could be more efficient or use better practices. Code below
Code: Select all
<?php
/*
Pagination
Written and Maintained by
Shan Robertson (http://invisibled.com)
Include the file:
require $root.'lib/inc/class.pagination.php';
Variables:
$p->page_table = database_table;
$p->per_page = string;
Initiate an instance:
$p->output();
Add this to the end of your Mysql query:
LIMIT $p->res, $p->per_page
*///
class pagination{
function output(){
//Access vars outside of class
$page_table = $this->page_table;
$per_page = $this->per_page;
//Global Vars
$get_page = $_GET['page'];
//If the page param isnt set, set to page 1
if(isset($get_page)):
$this->res = $per_page * $get_page - $per_page;
else:
$this->res = $per_page * 1 - $per_page;
endif;
//The Query
$page_query = mysql_query("SELECT id FROM $page_table");
//Stores total number of results
$num = mysql_num_rows($page_query);
//Divides the number of results by the number per page
$pages = ceil($num / $per_page);
//Sets the increment value at 1
$i = "1";
//Next and Prev Links
$next = $get_page + 1;
$prev = $get_page - 1;
//The Output
print '<div class="pagination">';
if(isset($get_page) && $get_page != "1"):
print '<a href="'.$php_self.'?page='.$prev.'"><</a>';
endif;
while($i <= $pages):
print '<a href="'.$php_self.'?page='.$i.'" ';
if($get_page == $i):
print 'class="current"';
endif;
print '>'.$i.'</a>';
$i++;
endwhile;
if($get_page != $pages):
print '<a href="'.$php_self.'?page='.$next.'">></a>';
endif;
print '</div>';
}
}
// Create a new class object
$p = new pagination();
?>
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: