Advertisement
Guest User

John Smith

a guest
Feb 15th, 2010
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.67 KB | None | 0 0
  1. <?php
  2. /***********************************
  3. * PhpMyCoder Paginator *
  4. * Created By PhpMyCoder *
  5. * 2010 PhpMyCoder *
  6. * ------------------------------- *
  7. * You may use this code as long *
  8. * as this notice stays intact and *
  9. * the proper credit is given to *
  10. * the author. *
  11. ***********************************/
  12.  
  13. class pmcPagination {
  14. private $pageVar = "page"; //$_GET variable to get page # from
  15. private $items = array(); //Array of items to paginate
  16. private $resultsPerPage = 20; //Results to be displayed per page
  17. private $paginated = false; //Has the paginate() function been called?
  18.  
  19. public function __construct($resultsPerPage="", $pageVar=0, $items=NULL)
  20. {
  21. //Set pageVar if arg is valid
  22. if($this->isValidPageVar($pageVar)) $this->pageVar = $pageVar;
  23.  
  24. //Set resultsPerPage if arg is valid
  25. if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
  26.  
  27. //Add items passed
  28. $this->add($items);
  29. }
  30.  
  31. public function add($items)
  32. {
  33. //If the item is null, exit function
  34. if(is_null($items)) return;
  35.  
  36. //If arg is not an array, make it one
  37. if(!is_array($items)) $items = array($items);
  38.  
  39. //Cycle through items passed
  40. foreach($items as $item)
  41. {
  42. //Add them to the $items array if they are valid
  43. if($item instanceof paginationData) $this->items[] = $item;
  44. }
  45. }
  46.  
  47. public function paginate($resultsPerPage="", $pageVar=0, $items=NULL)
  48. {
  49. //Prevent set function form changing values
  50. $this->paginated = true;
  51.  
  52. //Set pageVar if arg is valid
  53. if($this->isvalidPageVar($pageVar)) $this->pageVar = $pageVar;
  54. //Set parVar to previous value
  55. else $pageVar = $this->pageVar;
  56.  
  57. //Set resultsPerPage if arg is valid
  58. if($this->isValidResultsPerPage($resultsPerPage)) $this->resultsPerPage = $resultsPerPage;
  59. //Set resultsPerPage to previous value
  60. else $resultsPerPage = $this->resultsPerPage;
  61.  
  62. //Add any extra items
  63. $this->add($items);
  64.  
  65. //Get Page #, Staring Result #, and Ending Result #
  66. if(isset($_GET[$pageVar]) && $_GET[$pageVar] > 0) $page = $_GET[$pageVar];
  67. else $page = 1;
  68. $result = ($page - 1) * $resultsPerPage + 1;
  69. $max = $result + $resultsPerPage;
  70. if($max >= count($this->items)) $max = count($this->items);
  71.  
  72. //Are there results?
  73. if($result<count($this->items))
  74. {
  75. //Cycle through results on current page
  76. for(;$result<$max;$result++)
  77. {
  78. //Show the result
  79. $this->items[$result]->display();
  80. }
  81. }
  82. //Display a "no results" message
  83. else echo "<strong>No Results found on this page.</strong>";
  84. }
  85.  
  86. public function navigation()
  87. {
  88. //Get Pag #
  89. if(isset($_GET[$this->pageVar]) && $_GET[$this->pageVar] > 0) $page = $_GET[$this->pageVar];
  90. else $page = 1;
  91.  
  92. //Start Nav
  93. echo '<div id="nav">'."\n";
  94.  
  95. //Show the Previous Link
  96. if($page==1) echo "<span>&laquo; Previous</span> ";
  97. else echo "<a href='?".$this->pageVar."=".($page-1)."'>&laquo; Previous</a> ";
  98.  
  99. //Show the Number Links
  100. for($p=1;$p<=ceil(count($this->items)/$this->resultsPerPage);$p++)
  101. {
  102. if($p==$page) echo "<strong>{$p}</strong> ";
  103. else echo "<a href='?".$this->pageVar."={$p}'>{$p}</a> ";
  104. }
  105.  
  106. //Show the Next Link
  107. if($page==ceil(count($this->items)/$this->resultsPerPage)) echo "<span>Next &raquo;</span><br>";
  108. else echo "<a href='?".$this->pageVar."=".($page+1)."'>Next &raquo;</a><br>";
  109.  
  110. //End Nav
  111. echo '</div>'."\n";
  112. }
  113.  
  114. /* Get or set the pageVar field */
  115. public function getPageVar() { return($this->pageVar); }
  116. public function setPageVar($pageVar) { if(is_string($pageVar) && !$this->paginated) $this->pageVar = $pageVar; }
  117.  
  118. /* Get or set the resultsPerPage field */
  119. public function getResultsPerPage() { return($this->resultsPerPage); }
  120. public function setResultsPerPage($resultsPerPage)
  121. {
  122. //If the arg is a valid #, set the new value
  123. if(is_int($resultsPerPage) && $resultsPerPage>0 && !$this->paginated) $this->resultsPerPage = $resultsPerPage;
  124. }
  125.  
  126. private function isValidPageVar($pageVar)
  127. {
  128. //Return valid if arg is string and is not empty
  129. return(!empty($pageVar) && is_string($pageVar));
  130. }
  131.  
  132. private function isValidResultsPerPage($resultsPerPage)
  133. {
  134. //Return valid if arg is a positive integer
  135. return(is_int($resultsPerPage) || $resultsPerPage>0);
  136. }
  137. }
  138.  
  139. class paginationData {
  140. private $program;
  141. private $channel;
  142. private $eventdate;
  143. private $exiration;
  144. private $episode;
  145. private $setReminder;
  146.  
  147. public function __construct($program, $channel, $eventdate, $expiration, $eventactive, $setReminder)
  148. {
  149. $this->program = $program;
  150. $this->channel = $channel;
  151. $this->eventdate = $eventdate;
  152. $this->expiration = $expiration;
  153. $this->eventactive = $eventactive;
  154. $this->setReminder = $setReminder;
  155. }
  156.  
  157. //This function shows the data
  158. public function display()
  159. {
  160. if(date('Y-m-d') == date('Y-m-d', $this->airdate)) $dateFormat = 'g:ia';
  161. elseif(date('Y') == date('Y', $this->airdate)) $dateFormat = 'F jS - g:ia';
  162. else $dateFormat = 'F jS, Y - g:ia';
  163.  
  164. echo '<tr>'."\n".
  165. ' <td><strong>'.$this->event.'</strong></td>'."\n".
  166. ' <td>showing on '.$this->venue.'</td>'."\n".
  167. ' <td>'.date($dateFormat, $this->eventdate).'</td>'."\n".
  168. ' <td>'.$this->eventactive.'</td>'."\n".
  169. ' <td>'.$this->setReminder.'</td>'."\n".
  170. '</tr>'."\n";
  171. }
  172. }
  173. ?>
  174.  
  175. This is the page that generates the results:
  176.  
  177.  
  178. ----------
  179.  
  180. <?php
  181. /***********************************
  182. * PhpMyCoder Paginator *
  183. * Created By PhpMyCoder *
  184. * 2010 PhpMyCoder *
  185. * ------------------------------- *
  186. * You may use this code as long *
  187. * as this notice stays intact and *
  188. * the proper credit is given to *
  189. * the author. *
  190. ***********************************/
  191. ?>
  192. <!doctype html>
  193. <html>
  194. <head>
  195. <style type="text/css">
  196. body { color: #000; font: normal 13px/14px Arial, Helvetica, sans-serif; }
  197. small { color: #F60; font-weight: bold; }
  198. #nav { font: normal 13px/14px Arial, Helvetica, sans-serif; margin: 2px 0; }
  199. #nav a { background: #EEE; border: 1px solid #DDD; color: #000080; padding: 1px 7px; text-decoration: none; }
  200. #nav strong { background: #000080; border: 1px solid #DDD; color: #FFF; font-weight: normal; padding: 1px 7px; }
  201. #nav span { background: #FFF; border: 1px solid #DDD; color: #999; padding: 1px 7px; }
  202. </style>
  203. </head>
  204. <body>
  205. <?php
  206. //Require the file that contains the required classes
  207. include("pmcPagination.php");
  208.  
  209. //PhpMyCoder Paginator
  210. $paginator = new pmcPagination(10, "page");
  211.  
  212. //Connect to the database
  213. mysql_connect("localhost","root","PASSWORD");
  214. //Select DB
  215. mysql_select_db("eventguide");
  216.  
  217. //Select only results for today and future
  218. $result = mysql_query("SELECT * FROM events1 WHERE expiration >=NOW() ORDER BY expiration ASC");
  219.  
  220. //You can also add reuslts to paginate here
  221. while($row = mysql_fetch_array($result))
  222. {
  223. $paginator->add(new paginationData($row['event'],
  224. $row['venue'],
  225. $row['eventdate'],
  226. $row['expiration'],
  227. $row['eventactive'],
  228. $row['setreminder']));
  229. }
  230. ?>
  231. <table>
  232. <?php
  233. //Show the paginated results
  234. $paginator->paginate();
  235. ?>
  236. </table>
  237. <?php
  238. //Show the navigation
  239. $paginator->navigation();
  240. ?>
  241. <small>&copy; 2010 PhpMyCoder</small>
  242. </body>
  243. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement