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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.