Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /***********************************
- * 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 = 20; //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 $eventdate;
- private $exiration;
- private $episode;
- private $setReminder;
- public function __construct($program, $channel, $eventdate, $expiration, $eventactive, $setReminder)
- {
- $this->program = $program;
- $this->channel = $channel;
- $this->eventdate = $eventdate;
- $this->expiration = $expiration;
- $this->eventactive = $eventactive;
- $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->event.'</strong></td>'."\n".
- ' <td>showing on '.$this->venue.'</td>'."\n".
- ' <td>'.date($dateFormat, $this->eventdate).'</td>'."\n".
- ' <td>'.$this->eventactive.'</td>'."\n".
- ' <td>'.$this->setReminder.'</td>'."\n".
- '</tr>'."\n";
- }
- }
- ?>
- This is the page that generates the results:
- ----------
- <?php
- /***********************************
- * 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. *
- ***********************************/
- ?>
- <!doctype html>
- <html>
- <head>
- <style type="text/css">
- body { color: #000; font: normal 13px/14px Arial, Helvetica, sans-serif; }
- small { color: #F60; font-weight: bold; }
- #nav { font: normal 13px/14px Arial, Helvetica, sans-serif; margin: 2px 0; }
- #nav a { background: #EEE; border: 1px solid #DDD; color: #000080; padding: 1px 7px; text-decoration: none; }
- #nav strong { background: #000080; border: 1px solid #DDD; color: #FFF; font-weight: normal; padding: 1px 7px; }
- #nav span { background: #FFF; border: 1px solid #DDD; color: #999; padding: 1px 7px; }
- </style>
- </head>
- <body>
- <?php
- //Require the file that contains the required classes
- include("pmcPagination.php");
- //PhpMyCoder Paginator
- $paginator = new pmcPagination(10, "page");
- //Connect to the database
- mysql_connect("localhost","root","PASSWORD");
- //Select DB
- mysql_select_db("eventguide");
- //Select only results for today and future
- $result = mysql_query("SELECT * FROM events1 WHERE expiration >=NOW() ORDER BY expiration ASC");
- //You can also add reuslts to paginate here
- while($row = mysql_fetch_array($result))
- {
- $paginator->add(new paginationData($row['event'],
- $row['venue'],
- $row['eventdate'],
- $row['expiration'],
- $row['eventactive'],
- $row['setreminder']));
- }
- ?>
- <table>
- <?php
- //Show the paginated results
- $paginator->paginate();
- ?>
- </table>
- <?php
- //Show the navigation
- $paginator->navigation();
- ?>
- <small>© 2010 PhpMyCoder</small>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement