nade93 Posted May 19, 2010 Share Posted May 19, 2010 Hi I am trying to work a simple arrow (left right) navigation and the arrows only appear if there is a slide to navigate to For example. THe page is set up to detect content based on the slides number. This is taken from the database. I am using the following code at present <a href="mod_one.php?slideno=<? echo $row['current_slide'] -1 ?>"><img src="../img/layout/previous-arrow.png" alt="previous slide" border="0" /></a> <a href="mod_one.php?slideno=<? echo $row['current_slide'] +1 ?>"><img src="../img/layout/next-arrow.png" alt="next slide" border="0" /></a>< I need something that only shows the arrow if there is a next slide in the database or a previous one. Please can anyone help? Link to comment https://forums.phpfreaks.com/topic/202242-arrow-navigation/ Share on other sites More sharing options...
ignace Posted May 19, 2010 Share Posted May 19, 2010 You need a Paginator like: class Paginator implements Iterator { private $key = 0; private $sizeOf = 0; private $itemsPerPage; private $data = array(); private $activePage = 1; public function __construct($array, $itemsPerPage) { $this->data = $array; $this->sizeOf = sizeof($array); $this->itemsPerPage = abs((int)$itemsPerPage); } public function getItemsForPage($pageNumber) { $pageNumber = $pageNumber > 0 ? $pageNumber : 1; $pageNumber = $pageNumber < $this->getNumberOfPages() ? $pageNumber : $this->getNumberOfPages(); $this->activePage = $pageNumber; $this->key = $this->calculateOffset($pageNumber); return $this; } public function getActivePage() { return $this->activePage; } public function getNumberOfPages() { return ceil($this->sizeOf / $this->itemsPerPage); } public function valid() { return $this->key < $this->calculateOffset($this->getActivePage() + 1) && $this->key < $this->sizeOf; } public function key() { return $this->key; } public function next() { $this->key++; } public function rewind() { //$this->key = 0; } public function current() { return new ArrayObject($this->data[$this->key], ArrayObject::ARRAY_AS_PROPS); } private function calculateOffset($pageNumber) { return ($pageNumber - 1) * $this->itemsPerPage; } public function hasNextPage() { return $this->getActivePage() < $this->getNumberOfPages(); } public function hasPreviousPage() { return $this->getActivePage() > 1; } } Use it like: $rows = $db->execute('SELECT * FROM table')->fetchAll(); $paginator = new Paginator($rows, 20);//20=items per page $offset = $_GET['page_number']; $items = $paginator->getItemsForPage($offset); if ($paginator->hasNextPage()) { echo "has next"; } else { echo "doesn't have next"; } if ($paginator->hasPreviousPage()) { echo "has previous"; } else { echo "doesn't have previous"; } foreach ($items as $item) { print_r($item); } Link to comment https://forums.phpfreaks.com/topic/202242-arrow-navigation/#findComment-1060482 Share on other sites More sharing options...
nade93 Posted May 20, 2010 Author Share Posted May 20, 2010 thank for the suggestion, however I cannot get this to work! Link to comment https://forums.phpfreaks.com/topic/202242-arrow-navigation/#findComment-1061307 Share on other sites More sharing options...
nade93 Posted May 20, 2010 Author Share Posted May 20, 2010 managed to sort a solution!!! here it is incase anyone else needs it <?php // Connects to your Database include('../../lib/conn.php'); //This checks to see if there is a page number. If not, it will set it to page 1 if (!(isset($slideno))) { $slideno = 1; } //Here we count the number of results //Edit $data to be your query $data = mysql_query("SELECT * FROM module_ws_slides where mod_no='5'") or die(mysql_error()); $rows = mysql_num_rows($data); //This is the number of results displayed per page $page_rows = 1; //This tells us the page number of our last page $last = ceil($rows/$page_rows); //this makes sure the page number isn't below one, or more than our maximum pages if ($slideno < 1) { $slideno = 1; } elseif ($slideno > $last) { $slideno = $last; } //This sets the range to display in our query $max = 'limit ' .($slideno - 1) * $page_rows .',' .$page_rows; //This is your query again, the same one... the only difference is we add $max into it $data_p = mysql_query("SELECT * FROM module_ws_slides $max") or die(mysql_error()); //This is where you display your query results while($info = mysql_fetch_array( $data_p )) { Print $info['Name']; // First we check if we are on page one. If we are then we don't need a link to the previous page or the first page so we do nothing. If we aren't then we generate links to the first page, and to the previous page. if ($slideno == 1) { } else { $previous = $slideno-1; echo " <a href='{$_SERVER['PHP_SELF']}?slideno=$previous'> <img src=\"../img/layout/previous-arrow.png\" alt=\"previous slide\" border=\"0\" /></a> "; } //This does the same as above, only checking if we are on the last page, and then generating the Next and Last links if ($slideno == $last) { } else { $next = $slideno+1; echo " <a href='{$_SERVER['PHP_SELF']}?slideno=$next'><img src=\"../img/layout/next-arrow.png\" alt=\"next slide\" border=\"0\" /></a> "; } } ?> Link to comment https://forums.phpfreaks.com/topic/202242-arrow-navigation/#findComment-1061375 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.