Dsiembab 5 Junior Poster

I wrote this script and I was wondering if their was an easier way. It's basically for dynamic links

<?php 
$currents = $_GET['page'];
function paginator($current , $link, $getpre, $firstimg , $lastimg, $endimg, $startimg,  $totalitems, $maxperpage){
// $current is the current page should be dynamic
// $link is the url , 
// $getpre page prefix i.e "?page=" or whatever you want it to be
// $firstimg is the img for back browsing 
// $lastimg is for the forward if for forward browsing
// $endimg is for the last link to the pages in the series
// $startimg is for the first link to the pages in the series
// $maxpages is the maximum amount of items per page
// $totalitems is the total amount of items or articles
// if no imagesare set you will get the default you could even put in your own words

	$urlsegment   = "<a href='".$link.$getpre;
	$urlsegment1  = "'>";
	$endsegment   = "</a>";
$now = $current;
	if($startimg == ''){
		$startimg     = "First ... ";
	}
	if($firstimg == ''){
		$firstimg     = " Back ... ";
	}
	if($lastimg == ''){
		$lastimg    = " ... Next";
	}
	if($endimg == ''){
		$endimg     = " ... Last";
	}
	$first ='1';
if($totalitems == '0'){
$pages = '0';
}
elseif($totalitems < $maxperpage){
$pages = '1';
}else{
	$pages = ceil(($totalitems/$maxperpage));
}
echo "<div class ='paginator' style='text-align:center;width:100%;'>$current of $pages</div> <div class ='paginater' style='text-align:center;width:100%;'>";
	if ($pages   == '0'){
		echo "Please try your search again";
	exit();}
	elseif ($pages == '1' ){
		echo $urlsegment.$pages.$urlsegment1.$pages.$endsegment;
	exit();}
	elseif($pages >= '2' ){

		if($now != 1){
			if($pages >=10 && $now >=10){
			echo $urlsegment.$first.$urlsegment1.$startimg.$endsegment;
			}
			$back = ($now - '1');
			echo $urlsegment.$back.$urlsegment1.$firstimg.$endsegment;
			}
		}
  	if( $now <= 9){
			for($i=1; $i<=10; $i++){
				echo $urlsegment.$i.$urlsegment1.$i.$endsegment.' '; 
			}
		}
		elseif($now >=10 && $now <= ($pages -5)){
			for($i=1; $i<=10; $i++){
				echo $urlsegment.($now-5).$urlsegment1.($now-5).$endsegment.' '; 
				$now++;
			}
		}
		elseif( $now = $pages ){
			for($i=1; $i<=10; $i++){
				echo $urlsegment.($now-10).$urlsegment1.($now-10).$endsegment.' '; 
				$now++;
			}
		}
	
	if($current != $pages){
      $next = ($current + '1');
			echo $urlsegment.$next.$urlsegment1.$lastimg.$endsegment;
				if($pages >=10 && $current != $pages){
					echo $urlsegment.$pages.$urlsegment1.$endimg.$endsegment;
				}
	}
echo "</div>";
}

echo paginator($currents, "page.php" ,"?page=", '', '','','', '100000','30');

?>

this is just for if you have a million articles or pages. the demo is if you have page.php and your $_GET variable. I am sure there is an easier way, maybe a for statement or something. Any help would be appreciated. first function I ever wrote and it actually does what I want it to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.