Advertisement
Guest User

Untitled

a guest
May 22nd, 2010
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.43 KB | None | 0 0
  1. <?php
  2. date_default_timezone_set('GMT/London');
  3. /***********************************
  4.  * PhpMyCoder Paginator            *
  5.  * Created By PhpMyCoder           *
  6.  * 2010 PhpMyCoder                 *
  7.  * ------------------------------- *
  8.  * You may use this code as long   *
  9.  * as this notice stays intact and *
  10.  * the proper credit is given to   *
  11.  * the author.                     *
  12.  ***********************************/
  13.  
  14. class pmcPagination {
  15.     private $pageVar = "page";     //$_GET variable to get page # from
  16.     private $items = array();      //Array of items to paginate
  17.     private $resultsPerPage = 10;  //Results to be displayed per page
  18.     private $paginated = false;    //Has the paginate() function been called?
  19.    
  20.     public function __construct($resultsPerPage="", $pageVar=0, $items=NULL)
  21.     {
  22.         //Set pageVar if arg is valid
  23.         if($this->isValidPageVar($pageVar)) $this->pageVar = $pageVar;
  24.        
  25.         //Set resultsPerPage if arg is valid
  26.         if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
  27.        
  28.         //Add items passed
  29.         $this->add($items);
  30.     }
  31.    
  32.     public function add($items)
  33.     {
  34.         //If the item is null, exit function
  35.         if(is_null($items)) return;
  36.                    
  37.         //If arg is not an array, make it one
  38.         if(!is_array($items)) $items = array($items);
  39.        
  40.         //Cycle through items passed
  41.         foreach($items as $item)
  42.         {
  43.             //Add them to the $items array if they are valid
  44.             if($item instanceof paginationData) $this->items[] = $item;
  45.         }
  46.     }
  47.    
  48.     public function paginate($resultsPerPage="", $pageVar=0, $items=NULL)
  49.     {
  50.         //Prevent set function form changing values
  51.         $this->paginated = true;
  52.        
  53.         //Set pageVar if arg is valid
  54.         if($this->isvalidPageVar($pageVar)) $this->pageVar = $pageVar;
  55.         //Set parVar to previous value
  56.         else $pageVar = $this->pageVar;
  57.        
  58.         //Set resultsPerPage if arg is valid
  59.         if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
  60.         //Set resultsPerPage to previous value
  61.         else $resultsPerPage = $this->resultsPerPage;
  62.        
  63.         //Add any extra items
  64.         $this->add($items);
  65.        
  66.         //Get Page #, Staring Result #, and Ending Result #
  67.         if(isset($_GET[$pageVar]) && $_GET[$pageVar] > 0) $page = $_GET[$pageVar];
  68.         else $page = 1;
  69.         $result = ($page - 1) * $resultsPerPage + 1;
  70.         $max = $result + $resultsPerPage;
  71.         if($max >= count($this->items)) $max = count($this->items);
  72.        
  73.         //Are there results?
  74.         if($result<count($this->items))
  75.         {
  76.             //Cycle through results on current page
  77.             for(;$result<$max;$result++)
  78.             {
  79.                 //Show the result
  80.                 $this->items[$result]->display();
  81.             }
  82.         }
  83.         //Display a "no results" message
  84.         else echo "<strong>No Results found on this page.</strong>";
  85.     }
  86.    
  87.     public function navigation()
  88.     {
  89.         //Get Pag #
  90.         if(isset($_GET[$this->pageVar]) && $_GET[$this->pageVar] > 0) $page = $_GET[$this->pageVar];
  91.         else $page = 1;
  92.        
  93.         //Start Nav
  94.         echo '<div id="nav">'."\n";
  95.        
  96.         //Show the Previous Link
  97.         if($page==1) echo "<span>&laquo; Previous</span> ";
  98.         else echo "<a href='?".$this->pageVar."=".($page-1)."'>&laquo; Previous</a> ";
  99.        
  100.         //Show the Number Links
  101.         for($p=1;$p<=ceil(count($this->items)/$this->resultsPerPage);$p++)
  102.         {
  103.             if($p==$page) echo "<strong>{$p}</strong> ";
  104.             else echo "<a href='?".$this->pageVar."={$p}'>{$p}</a> ";
  105.         }
  106.        
  107.         //Show the Next Link
  108.         if($page==ceil(count($this->items)/$this->resultsPerPage)) echo "<span>Next &raquo;</span><br>";
  109.         else echo "<a href='?".$this->pageVar."=".($page+1)."'>Next &raquo;</a><br>";
  110.        
  111.         //End Nav
  112.         echo '</div>'."\n";
  113.     }
  114.    
  115.     /* Get or set the pageVar field */
  116.     public function getPageVar() { return($this->pageVar); }
  117.     public function setPageVar($pageVar) { if(is_string($pageVar) && !$this->paginated) $this->pageVar = $pageVar; }
  118.    
  119.     /* Get or set the resultsPerPage field */
  120.     public function getResultsPerPage() { return($this->resultsPerPage); }
  121.     public function setResultsPerPage($resultsPerPage)
  122.     {
  123.         //If the arg is a valid #, set the new value
  124.         if(is_int($resultsPerPage) && $resultsPerPage>0 && !$this->paginated) $this->resultsPerPage = $resultsPerPage;
  125.     }
  126.    
  127.     private function isValidPageVar($pageVar)
  128.     {
  129.         //Return valid if arg is string and is not empty
  130.         return(!empty($pageVar) && is_string($pageVar));
  131.     }
  132.    
  133.     private function isValidResultsPerPage($resultsPerPage)
  134.     {
  135.         //Return valid if arg is a positive integer
  136.         return(is_int($resultsPerPage) || $resultsPerPage>0);
  137.     }
  138. }
  139.  
  140. class paginationData {
  141.     private $program;
  142.     private $channel;
  143.     private $airdate;
  144.     private $exiration;
  145.     private $episode;
  146.     private $setReminder;
  147.            
  148.     public function __construct($program, $channel, $airdate, $expiration, $episode, $setReminder)
  149.     {
  150.         $this->program = $program;
  151.         $this->channel = $channel;
  152.       $this->airdate = strtotime($airdate);
  153.         $this->expiration = $expiration;
  154.         $this->episode = $episode;
  155.         $this->setReminder = $setReminder;
  156.     }
  157.    
  158.     //This function shows the data
  159.     public function display()
  160.     {
  161.         if(date('Y-m-d') == date('Y-m-d', $this->airdate)) $dateFormat = 'g:ia';
  162.         elseif(date('Y') == date('Y', $this->airdate)) $dateFormat = 'F jS - g:ia';
  163.         else $dateFormat = 'F jS, Y - g:ia';
  164.    
  165.         echo '<tr>'."\n".
  166.              '  <td><strong>'.$this->program.'</strong></td>'."\n".
  167.              '  <td>showing on '.$this->channel.'</td>'."\n".
  168.              '  <td>'.date($dateFormat, $this->airdate).'</td>'."\n".
  169.              '  <td> '.$this->episode.'</td>'. "\n".
  170.              '  <td>'.$this->setReminder.'</td>'."\n".
  171.              '</tr>'."\n";
  172.     }
  173. }
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement