Jump to content

Php Pages & Numbers.


xyn

Recommended Posts

Hey,
I basically have a news system which I wanted to show 6 articles Per page
and have page numbers at the bottom like:
[ 1 | 2 | 3 | 4 | 5 | >> ]
And when you get to page 2+ it adds "<<" previous. and obviously
when it's on the last page you can't go ">>" next.

Does anyone have any ideas how i can achieve this?
Link to comment
https://forums.phpfreaks.com/topic/15379-php-pages-numbers/
Share on other sites

Here's an example that uses grouping of pages, useful if you have a lot pages:

[code]$page = !isset($_GET['page'])?1:$_GET['page'];
printNavLinks(800, 6, 10, $page);

function printNavLinks($totalItems, $maxitems, $groupsize, $currpage)
{
//Get number of pages:
$pages = ceil($totalItems / $maxitems);
//Divide the pages into groups:
$groupnr = 0;
for($page=1; $page<$pages; $page++) {
//If page is dividable by the groupsize, new group.
if($page % $groupsize == 0){
$groupnr++;
}
$groupArr[$page] = $groupnr;
}
//Display 'previous page link':
if($currpage>1){
echo '<a href=test.php?page='.($currpage-1).'>< Previous </a>';
}
$groupToDisplay = $groupArr[$currpage];
//Display pages in this group:
foreach($groupArr as $page=>$groupnr)
{
if($groupnr == $groupToDisplay) {
if($page == $currpage){
echo '<strong><a href=test.php?page='.$page.'> ['.$page.'] </a></strong>';
}
else {
echo '<a href=test.php?page='.$page.'> ['.$page.'] </a>';
}
}
}
//Display 'next page link':
if(isset($groupArr[$currpage+1])){
echo '<a href=test.php?page='.($currpage+1).'> Next ></a>';
}
}
?>[/code]

To fetch from mysql use:
[code]'SELECT * FROM table LIMIT '.($currpage-1)*$maxitems.', '.$maxitems[/code]
Link to comment
https://forums.phpfreaks.com/topic/15379-php-pages-numbers/#findComment-62354
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.