<?
if($page)$pn=$page +1;
	  
$sqlpg = mysql_query($sql);
$nr = $num_rows;

mysql_num_rows($sqlpg); // Get total of Num rows from the database query

if($nr > 0){

	if (isset($pn)) { // Get pn from URL vars if it is present
		//$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']); // filter everything but numbers for security(new)
		$pn=$page;
//		$pn=$page +1;
		//$pn = ereg_replace("[^0-9]", "", $_GET['pn']); // filter everything but numbers for security(deprecated)
	} else { // If the pn URL variable is not present force it to be value of page number 1
		$pn = 1;
	} 
	
	$itemsPerPage = 20; 
	// Get the value of the last page in the pagination result set
	$lastPage = ceil($nr / $itemsPerPage);
	// Be sure URL variable $pn(page number) is no lower than page 1 and no higher than $lastpage
	if ($pn < 1) { // If it is less than 1
		$pn = 1; // force if to be 1
	} else if ($pn > $lastPage) { // if it is greater than $lastpage
		$pn = $lastPage; // force it to be $lastpage's value
	} 
	
	$centerPages = "";
	$sub1 = $pn - 1;
	$sub2 = $pn - 2;
	$add1 = $pn + 1;
	$add2 = $pn + 1;
	
	$lnsub1=$sub1;
	$lnsub2=$sub2-1;
	$lnadd1 = $add1 -1;
	$lnadd2 = $add2;
	
	if ($pn == 1) {
		$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
//		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page1=' . $lnadd1 . "&param=".$param.'">' . $add1 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnadd2 . "&param=".$param.'">' . $add2 . '</a> &nbsp;';	
	} else if ($pn == $lastPage) {
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnsub1 . "&param=".$param.'">' . $sub1 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
	} else if ($pn > 2 && $pn < ($lastPage - 1)) {
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnsub2 . "&param=".$param.'">' . $sub2 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnsub1 . "&param=".$param.'">' . $sub1 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnadd1 . "&param=".$param.'">' . $add1 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnadd2 . "&param=".$param.'">' . $add2 . '</a> &nbsp;';
	} else if ($pn > 1 && $pn < $lastPage) {
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnsub1 . "&param=".$param.'">' . $sub1 . '</a> &nbsp;';
		$centerPages .= '&nbsp; <span class="pagNumActive">' . $pn . '</span> &nbsp;';
		$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $lnadd1 . "&param=".$param.'">' . $add1 . '</a> &nbsp;';
	}

	$paginationDisplay = ""; // Initialize the pagination output variable
	// This code runs only if the last page variable is ot equal to 1, if it is only 1 page we require no paginated links to display
	if ($lastPage != "1"){
		// This shows the user what page they are on, and the total number of pages
		$paginationDisplay .= 'Page <strong>' . $pn . '</strong> of ' . $lastPage. '&nbsp;  &nbsp;  &nbsp; ';
		// If we are not on page 1 we can place the Back button

		if ($pn != 1) {
			$previous = $pn - 1;
			$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $previous . "&param=".$param.'"> Back</a> ';
		} 
		// Lay in the clickable numbers display here between the Back and Next links
		$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
		// If we are not on the very last page we can place the Next button
		if ($pn != $lastPage) {
		//	$nextPage = $pn;
	       $nextPage = $pn + 1;
			$paginationDisplay .=  '&nbsp;  <a href="' . $_SERVER['PHP_SELF'] . '?page=' . $nextPage . "&param=".$param.'"> Next</a> ';
		} 
	}
	
	echo $paginationDisplay." &nbsp; Total Record:  $nr";
}
?>