I've been writing a pagination class for my projects and i'm trying to put this one variable inside the class so it doesn't have to be out in the main files... cause its not very neat.
to include the class in your project you do this
Code: Select all
//Include the files:
require $root.'lib/inc/class.pagination.php';
$p = new pagination();
//Variables
$p->page_table = database_table;
$p->per_page = string;
if(isset($_GET['page'])):
$p->res = $p->per_page * $_GET['page'] - $p->per_page;
else:
$p->res = $p->per_page * 1 - $p->per_page;
endif;
//Initiate an instance:
$p->output();
Code: Select all
mysql_query("SELECT * FROM module_projects ORDER BY id DESC LIMIT $p->res, $p->per_page ");Code: Select all
<?php
/*
Pagination
Written and Maintained by
Shan Robertson (http://invisibled.com)
Include the files:
require $root.'lib/inc/class.pagination.php';
$p = new pagination();
Variables
$p->page_table = database_table;
$p->per_page = string;
if(isset($_GET['page'])):
$p->res = $p->per_page * $_GET['page'] - $p->per_page;
else:
$p->res = $p->per_page * 1 - $p->per_page;
endif;
Initiate an instance:
$p->output();
TO DO
-fix prev and next links when page var is not set
-put the class init at the bottom of this file
-put the res variable into the class
*///
class pagination{
function output(){
//Brings the vars into the function
$page_table = $this->page_table;
$per_page = $this->per_page;
//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($_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>';
}
}
?>