04-18-2009, 05:01 PM
|
#1 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
My first pagination class
Thanks to the "usual suspects" around here, I have been able to complete my pagination class!! I hope to get a lot of comments for "improvements"...
Below is the HTML code, then the class code, and then a link to where you guys can play with it!!
HTML CODE
Code:
<?
include_once("pagination.php");
/* LETS SET SOME VARIABLES TO SIMULATE USER EXPERIENCE */
if(!$_GET[record_limit]) $record_limit = 5;
else $record_limit = $_GET[record_limit];
if(!$_GET[p]) $current_page = 1;
else $current_page = $_GET[p];
/* ARRAY WILL SIMULATE DATABASE ENTRIES */
$array = array(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,33,34,35,36,37,38,39,40);
$totalrows = count($array);
/* CREATE OBJECT */
$pagination = new pagination($current_page,$record_limit,$totalrows);
$getViewPage = $pagination->viewPage();
$calc_page = $pagination->calculateLastPage();
?>
<table width="949" border="0" cellspacing="3" cellpadding="2">
<tr>
<td> </td>
<td> </td>
<td align="center" colspan="4"><? echo "You are viewing page: " . $getViewPage[0] . " - " . $getViewPage[1] . " of " . $totalrows . " entries! "; ?></td>
<td colspan="2"> </td>
</tr>
<tr>
<td> </td>
<td>
<form action="" method="GET">
<select name="record_limit">
<option value="5">5 records</option>
<option value="10">10 records</option>
<option value="20">20 records</option>
</select>
<input type="submit" name="Submit" value="Submit">
</form></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td colspan="2"><? echo $pagination->getPageFirst() . $pagination->getPagePrev() . implode(' ',$pagination->getPageLinks()) . $pagination->getPageNext() . $pagination->getPageLast(); ?></td>
</tr>
<tr bgcolor="#eeeeee">
<td width="30"><div align="center"><strong>#</strong></div></td>
<td width="165"><strong>Username</strong></td>
<td width="100"><strong>First Name</strong></td>
<td width="106"><strong>Last Name</strong></td>
<td width="101"><strong>Location</strong></td>
<td width="107"><strong>Phone</strong></td>
<td width="111"><strong>Email</strong></td>
<td width="170"><strong>Notes</strong></td>
</tr>
<? for($i=$getViewPage[0];$i<=$getViewPage[1];$i++) { ?>
<tr>
<td><? echo $i; ?></td>
<td>fake_username</td>
<td>fake_first</td>
<td>fake_last</td>
<td>fake_location</td>
<td>fake_phone</td>
<td>fake_email</td>
<td>fake_notes</td>
</tr>
<? } ?>
</table>
CLASS CODE
Code:
<?
class pagination {
private $currentPage;
private $recordLimit;
private $start;
private $totalrows;
private $pageLast;
private $starting_no;
private $end_count;
/* INITIALIZE THIS CLASS WITH CURRENT PAGE AND PAGE LIMIT VARS */
/* TO BE USED THROUGHOUT REST OF CLASS */
public function __construct($current_page,$record_limit,$totalrows) {
$this->currentPage = $current_page;
$this->recordLimit = $record_limit;
$this->totalRows = $totalrows;
}
/* CREATE STATS FOR VIEWPAGE ex: 1 - 25 of 1,000 entries */
public function viewPage() {
/* USE $start VAR FOR DATABASE QUERY ex: LIMIT $start,$page_limit */
if($this->currentPage == 1) $this->start = 0;
else $this->start = ($this->currentPage - 1) * $this->recordLimit;
/* CREATE OFFSET ex: 1 -25, 26 - 50 */
$this->starting_no = ($this->start + 1);
/* CREATE ENDCOUNT ex: 1 - 25, THE 25 IS THE ENDCOUNT */
if (($this->totalRows - $this->start) < $this->recordLimit) $this->end_count = $this->totalRows;
elseif (($this->totalRows - $this->start) >= $this->recordLimit) $this->end_count = ($this->start + $this->recordLimit);
$viewPageEntry[] = $this->starting_no;
$viewPageEntry[] = $this->end_count;
return $viewPageEntry;
}
public function calculateLastPage() {
/* CALCULATE LAST PAGE LINK */
/* THIS ALSO WILL BE USED IN THE FOR LOOP */
return $this->pageLast = ceil($this->totalRows/$this->recordLimit);
}
/* GENERATE LINKS ex: 1 2 3 4 5 6 7 8 */
public function getPageLinks() {
for ($i=1; $i<=$this->pageLast; $i++) {
if(($this->currentPage - 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i&record_limit=$this->recordLimit\">$i</a>";
if(($this->currentPage - 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i&record_limit=$this->recordLimit\">$i</a>";
if($this->currentPage == $i) $display_pages[] = "$i";
if(($this->currentPage + 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i&record_limit=$this->recordLimit\">$i</a>";
if(($this->currentPage + 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i&record_limit=$this->recordLimit\">$i</a>";
}
return $this->displayPages = $display_pages;
}
/* GENERATE FIRST LINK */
public function getPageFirst() {
if($this->currentPage == 1) return $this->first = "First ";
else
return $this->first = "<a href=\"$_SERVER[PHP_SELF]?p=1&record_limit=$this->recordLimit\">First</a> ";
}
/* GENERATE PREV LINK */
public function getPagePrev() {
if($this->currentPage == 1) return $this->previous = "Prev ";
else $this->p = ($this->currentPage - 1);
return $this->previous = " <a href=\"$_SERVER[PHP_SELF]?p=$this->p&record_limit=$this->recordLimit\">Prev</a> ";
}
/* GENERATE NEXT LINK */
public function getPageNext() {
if($this->currentPage == $this->pageLast) return $this->pageNext = " Next";
else $this->p = ($this->currentPage + 1);
return $this->pageNext = " <a href=\"$_SERVER[PHP_SELF]?p=$this->p&record_limit=$this->recordLimit\">Next</a>";
}
/* GENERATE LAST LINK */
public function getPageLast() {
if($this->currentPage == $this->pageLast) return $this->last = " Last";
else return $this->last = " <a href=\"$_SERVER[PHP_SELF]?p=$this->pageLast&record_limit=$this->recordLimit\">Last</a>";
}
}###END CLASS
?>
And here's a link where I uploaded it all...
http://www.gatebattle.com/paginate.html
Please, let me know what you guys think!
Thanks!
|
|
|
|