<?php
date_default_timezone_set('GMT/London');
/***********************************
* PhpMyCoder Paginator *
* Created By PhpMyCoder *
* 2010 PhpMyCoder *
* ------------------------------- *
* You may use this code as long *
* as this notice stays intact and *
* the proper credit is given to *
* the author. *
***********************************/
class pmcPagination {
private $pageVar = "page"; //$_GET variable to get page # from
private $items = array(); //Array of items to paginate
private $resultsPerPage = 10; //Results to be displayed per page
private $paginated = false; //Has the paginate() function been called?
public function __construct($resultsPerPage="", $pageVar=0, $items=NULL)
{
//Set pageVar if arg is valid
if($this->isValidPageVar($pageVar)) $this->pageVar = $pageVar;
//Set resultsPerPage if arg is valid
if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
//Add items passed
$this->add($items);
}
public function add($items)
{
//If the item is null, exit function
if(is_null($items)) return;
//If arg is not an array, make it one
if(!is_array($items)) $items = array($items);
//Cycle through items passed
foreach($items as $item)
{
//Add them to the $items array if they are valid
if($item instanceof paginationData) $this->items[] = $item;
}
}
public function paginate($resultsPerPage="", $pageVar=0, $items=NULL)
{
//Prevent set function form changing values
$this->paginated = true;
//Set pageVar if arg is valid
if($this->isvalidPageVar($pageVar)) $this->pageVar = $pageVar;
//Set parVar to previous value
else $pageVar = $this->pageVar;
//Set resultsPerPage if arg is valid
if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
//Set resultsPerPage to previous value
else $resultsPerPage = $this->resultsPerPage;
//Add any extra items
$this->add($items);
//Get Page #, Staring Result #, and Ending Result #
if(isset($_GET[$pageVar]) && $_GET[$pageVar] > 0) $page = $_GET[$pageVar];
else $page = 1;
$result = ($page - 1) * $resultsPerPage + 1;
$max = $result + $resultsPerPage;
if($max >= count($this->items)) $max = count($this->items);
//Are there results?
if($result<count($this->items))
{
//Cycle through results on current page
for(;$result<$max;$result++)
{
//Show the result
$this->items[$result]->display();
}
}
//Display a "no results" message
else echo "<strong>No Results found on this page.</strong>";
}
public function navigation()
{
//Get Pag #
if(isset($_GET[$this->pageVar]) && $_GET[$this->pageVar] > 0) $page = $_GET[$this->pageVar];
else $page = 1;
//Start Nav
echo '<div id="nav">'."\n";
//Show the Previous Link
if($page==1) echo "<span>« Previous</span> ";
else echo "<a href='?".$this->pageVar."=".($page-1)."'>« Previous</a> ";
//Show the Number Links
for($p=1;$p<=ceil(count($this->items)/$this->resultsPerPage);$p++)
{
if($p==$page) echo "<strong>{$p}</strong> ";
else echo "<a href='?".$this->pageVar."={$p}'>{$p}</a> ";
}
//Show the Next Link
if($page==ceil(count($this->items)/$this->resultsPerPage)) echo "<span>Next »</span><br>";
else echo "<a href='?".$this->pageVar."=".($page+1)."'>Next »</a><br>";
//End Nav
echo '</div>'."\n";
}
/* Get or set the pageVar field */
public function getPageVar() { return($this->pageVar); }
public function setPageVar($pageVar) { if(is_string($pageVar) && !$this->paginated) $this->pageVar = $pageVar; }
/* Get or set the resultsPerPage field */
public function getResultsPerPage() { return($this->resultsPerPage); }
public function setResultsPerPage($resultsPerPage)
{
//If the arg is a valid #, set the new value
if(is_int($resultsPerPage) && $resultsPerPage>0 && !$this->paginated) $this->resultsPerPage = $resultsPerPage;
}
private function isValidPageVar($pageVar)
{
//Return valid if arg is string and is not empty
return(!empty($pageVar) && is_string($pageVar));
}
private function isValidResultsPerPage($resultsPerPage)
{
//Return valid if arg is a positive integer
return(is_int($resultsPerPage) || $resultsPerPage>0);
}
}
class paginationData {
private $program;
private $channel;
private $airdate;
private $exiration;
private $episode;
private $setReminder;
public function __construct($program, $channel, $airdate, $expiration, $episode, $setReminder)
{
$this->program = $program;
$this->channel = $channel;
$this->airdate = strtotime($airdate);
$this->expiration = $expiration;
$this->episode = $episode;
$this->setReminder = $setReminder;
}
//This function shows the data
public function display()
{
if(date('Y-m-d') == date('Y-m-d', $this->airdate)) $dateFormat = 'g:ia';
elseif(date('Y') == date('Y', $this->airdate)) $dateFormat = 'F jS - g:ia';
else $dateFormat = 'F jS, Y - g:ia';
echo '<tr>'."\n".
' <td><strong>'.$this->program.'</strong></td>'."\n".
' <td>showing on '.$this->channel.'</td>'."\n".
' <td>'.date($dateFormat, $this->airdate).'</td>'."\n".
' <td> '.$this->episode.'</td>'. "\n".
' <td>'.$this->setReminder.'</td>'."\n".
'</tr>'."\n";
}
}
?>