TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-18-2009, 05:01 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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>&nbsp;</td>
    <td>&nbsp;</td>
    <td align="center" colspan="4"><? echo "You are viewing page: " . $getViewPage[0] . " - " . $getViewPage[1] . " of " . $totalrows . " entries! "; ?></td>
    <td colspan="2">&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</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>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</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!
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please review my pagination class Tanax Advanced PHP Programming 12 03-23-2009 05:51 PM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
My Pagination Class Andrew Show Off 4 12-17-2007 10:10 PM
Advance Pagination Class Rendair Advanced PHP Programming 9 12-13-2007 08:28 AM


All times are GMT. The time now is 09:03 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design