Jump to content


Photo

Php Pages & Numbers.


  • Please log in to reply
2 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 23 July 2006 - 08:19 AM

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?

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 July 2006 - 09:07 AM

Check the pagination tutorial here -> http://www.phpfreaks...orials/43/0.php
Legend has it that reading the manual never killed anyone.
My site

#3 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 23 July 2006 - 11:29 AM

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

$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>';
	}
}
?>

To fetch from mysql use:
'SELECT * FROM table LIMIT '.($currpage-1)*$maxitems.', '.$maxitems





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users