monkeytooth Posted May 4, 2010 Share Posted May 4, 2010 I have this pagination concept i built up a couple years ago.. and for all intensive purposes it works as intended.. However I have a project I am working on currently where i want to switch things up a bit. My Current technique is simple enough.. and only returns next page and previous page.. determines if your are on the last page or on the first, but thats the extent of it. What I am trying to do is take it from what it is and make it so I can dynamicly place a small list of pages before and after instead of [previous] [next] i want to do [previous] 1, 2, 3, 4, 5.. 15, 20, 25 30... 100 [next] I just cant for the life of me figure out how to do the math dynamically to produce the results I want. $rowsPerPage = 10; $pageNum = 1; if(isset($_GET['pg'])) { $pageNum = $_GET['pg']; } elseif($_GET['pg'] == "") { $pageNum = 1; } elseif($_GET['pg'] < "1"){ $pageNum = 1; }else { $pageNum = 1; } $offset = ($pageNum - 1) * $rowsPerPage; $maxPage = ceil($dispCount/$rowsPerPage); $link2use = $_SERVER['PHP_SELF']."?rl=shop&stype=".$sType."&kw=".$sTerm; if ($pageNum > 1){$page = $pageNum - 1;$prev = '<a href="' . $link2use . '&pg=' . $page . '"><img src="img/prev.png" width="9" height="15" border="0" /></a>';} else {$prev = '<img src="img/prevd.png" width="9" height="15" border="0" />'; }// we're on page one, don't print previous link if ($pageNum < $maxPage){$page = $pageNum + 1;$next = '<a href="' . $link2use . '&pg=' . $page . '"><img src="img/next.png" width="9" height="15" border="0" /></a>';} else { $next = '<img src="img/nextd.png" width="9" height="15" border="0" />'; }// we're on the last page, don't print next link Now im pretty sure I really dont even need any of the to make the function itself to generate the links i want to generate between the previous and next but as I said for some reason I can't figure out in my head how to start the math for it to build it dynamically.. and apply it towards results that can be anything from only 1 or 2 pages worth to some with a couple hundred results.. can anyone help me get pointed in the right direction? thanks in advance. Link to comment https://forums.phpfreaks.com/topic/200618-pagination/ Share on other sites More sharing options...
Presto-X Posted May 4, 2010 Share Posted May 4, 2010 Hey this is some code I used on a Joomla project a few weeks back, so you will want to change the code to fit your needs but it works really well. $tbl_name = $app->getCfg('dbprefix')."portfoliosystem"; //your table name // How many adjacent pages should be shown on each side? $adjacents = 3; // First get total number of rows in data table. // If you have a WHERE clause in your query, make sure you mirror it here. $query = "SELECT COUNT(*) as num FROM $tbl_name"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[num]; // Setup vars for query. $targetpage = "index.php?option=".$_GET['option']."&view=".$_GET['view']."&Itemid=".$_GET['Itemid']; //your file name (the name of this file) $limit = $params->get('pagination'); //how many items to show per page $page = $_GET['page']; if($page) $start = ($page - 1) * $limit; //first item to display on this page else $start = 0; //if no page var is given, set start to 0 // Setup page vars for display. if ($page == 0) $page = 1; //if no page var is given, default to 1. $prev = $page - 1; //previous page is page - 1 $next = $page + 1; //next page is page + 1 $lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up. $lpm1 = $lastpage - 1; //last page minus 1 // Select bios from the database $query_portfoliosystem = "SELECT * FROM ".$app->getCfg('dbprefix')."portfoliosystem WHERE itemid = ".$_GET['Itemid']." ORDER BY ".$params->get('dis_sortorder')." ".$params->get('dis_sortorder_sort')." LIMIT ".$start.", ".$limit.""; $portfoliosystem = mysql_query($query_portfoliosystem) or die(mysql_error()); $row_portfoliosystem = mysql_fetch_assoc($portfoliosystem); $totalRows_portfoliosystem = mysql_num_rows($portfoliosystem); // Now we apply our rules and draw the pagination object. // We're actually saving the code to a variable in case we want to draw it more than once. $pagination = ""; if($lastpage > 1) { $pagination .= "<div class=\"pagination\">"; //previous button if ($page > 1): $pagination.= "<a href=\"$targetpage&page=1\" class=\"prev\" title=\"".JText::_('BUTTON FIRST PAGE')."\">««</a>"; $pagination.= "<a href=\"$targetpage&page=$prev\" class=\"prev\" title=\"".JText::_('BUTTON PREVIOUS PAGE')."\">«</a>"; else: endif; //pages if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up { for ($counter = 1; $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage&page=$counter\" class=\"link\">$counter</a>"; } } elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some { //close to beginning; only hide later pages if($page < 1 + ($adjacents * 2)) { for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage&page=$counter\" class=\"link\">$counter</a>"; } $pagination.= " ... "; $pagination.= "<a href=\"$targetpage&page=$lpm1\" class=\"link\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage&page=$lastpage\" class=\"link\">$lastpage</a>"; } //in middle; hide some front and some back elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2)) { $pagination.= "<a href=\"$targetpage&page=1\" class=\"link\">1</a>"; $pagination.= "<a href=\"$targetpage&page=2\" class=\"link\">2</a>"; $pagination.= " ... "; for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage&page=$counter\" class=\"link\">$counter</a>"; } $pagination.= " ... "; $pagination.= "<a href=\"$targetpage&page=$lpm1\" class=\"link\">$lpm1</a>"; $pagination.= "<a href=\"$targetpage&page=$lastpage\" class=\"link\">$lastpage</a>"; } //close to end; only hide early pages else { $pagination.= "<a href=\"$targetpage&page=1\" class=\"link\">1</a>"; $pagination.= "<a href=\"$targetpage&page=2\" class=\"link\">2</a>"; $pagination.= " ... "; for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) { if ($counter == $page) $pagination.= "<span class=\"current\">$counter</span>"; else $pagination.= "<a href=\"$targetpage&page=$counter\" class=\"link\">$counter</a>"; } } } //next button if ($page < $counter - 1): $pagination.= "<a href=\"$targetpage&page=$next\" class=\"next\" title=\"".JText::_('BUTTON NEXT PAGE')."\">»</a>"; $pagination.= "<a href=\"$targetpage&page=$lastpage\" class=\"next\" title=\"".JText::_('BUTTON LAST PAGE')."\">»»</a>"; else: endif; $pagination.= "<div class=\"pageindex\">".JText::_('LABEL PAGE')." $page ".JText::_('LABEL OF')." $lastpage</div>"; $pagination.= "</div>\n"; } echo $pagination; Link to comment https://forums.phpfreaks.com/topic/200618-pagination/#findComment-1052783 Share on other sites More sharing options...
monkeytooth Posted May 4, 2010 Author Share Posted May 4, 2010 Thank you, I will have to take a better look at that soon enough an break it down for myself. Link to comment https://forums.phpfreaks.com/topic/200618-pagination/#findComment-1052970 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.