iLexN
2/17/2015 - 8:44 AM

PHP NAV function

PHP NAV function

<? unset($_GET['page']);
	$get_var = http_build_query($_GET); ?>

<table cellpadding="0" cellspacing="0" border="0" width="100%" class="nav" align='left' >
	<tr>
		<td align='left' width="110">Total Record : <?=$nav[total]?></td>
		<td align='left'>Reconds From  <?=$nav[display_from]?> To  <?=$nav[display_to]?></td>
		<td align="right">

		<?if( $nav[have_previous_page] == 1 ) {?>
			<a href="<?=$_SERVER['PHP_SELF']?>?page=<?=$nav[previous_page]?>&<?=$get_var?>">Previous</a> &nbsp;
		<? } ?>
						
		<? for ( $i = $nav[section_page_start] ; $i <= $nav[section_page_end] ; $i++ ) { ?>
			<? if ( $i == $nav[page] ) { ?>
				<span style="color:green;font-weight:bold;">[<img src="t.gif" width="3" height="1"><?=$i?><img src="t.gif" width="3" height="1">]</span> &nbsp;
			<? } else { ?>
				<a href="<?=$_SERVER['PHP_SELF']?>?page=<?=$i?>&<?=$get_var?>"><?=$i?></a> &nbsp;
			<? } ?>
		<? } ?>
						
		<?if( $nav[have_next_page] == 1 ) {?>
			<a href="<?=$_SERVER['PHP_SELF']?>?page=<?=$nav[next_page]?>&<?=$get_var?>">Next</a> &nbsp;
		<? } ?>
		</td>
	</tr>
</table>
<?php

function getNav($total_rs,$select_page){
		$nav = array();
		
		$nav[total] = $total_rs;
		$nav[page] = $select_page; // now in which page
		$nav[numPage] = 5; // page section
		$nav[numCol] = 1; 
		$nav[numRow] = 20;
		
		$nav[page_show] = $nav[numRow] * $nav[numCol];
		
		$nav[no_of_pages] = ceil( $nav[total] / $nav[page_show] );
		if ( $nav[no_of_pages] == 0 ) { 
			$nav[no_of_pages] = 1; 
		} 
				
		if ( $nav[page] <= $nav[no_of_pages] ) { 
			$nav[showed] = ($nav[page] - 1) * $nav[page_show];
		} else { 
			$nav[page] = 1;
			$nav[showed] = 0;
		}
		
		if ( $nav[total] == 0 ) { 
			$nav[display_from] = 0;  
		} else { 
			$nav[display_from] = $nav[showed] + 1;
		}
		
		$nav[display_to] = $nav[showed] + $nav[page_show];
		if ( $nav[display_to] > $nav[total] ) { 
			$nav[display_to] = $nav[total]; 
		} 
		
		$nav[previous_page] = $nav[page] - 1;
		if ( $nav[previous_page] > 0 ) {
			$nav[have_previous_page] = 1;
		} else { 
			$nav[have_previous_page] = 0;
		}
		
		$nav[next_page] = $nav[page] + 1;
		
		if ( $nav[page] < $nav[no_of_pages] ) {
			$nav[have_next_page] = 1;
		} else {
			$nav[have_next_page] = 0;
		}
		
		$no_of_sections = ceil( $nav[no_of_pages] / $nav[numPage] );
		$page_section = ceil( $nav[page] / $nav[numPage] );
		
		$nav[section_page_start] = ( ($page_section - 1) * $nav[numPage] ) + 1;
		$nav[section_page_end] = $page_section * $nav[numPage];
		if ( $nav[section_page_end] > $nav[no_of_pages] ) { 
			$nav[section_page_end] = $nav[no_of_pages]; 
		}
		$previous_section = $page_section - 1;
		if ( $previous_section > 0 ) {
			$nav[have_previous_section] = 1;
			$nav[previous_section_page] = ( ($previous_section - 1) * $nav[numPage] ) + 1;
		} else {
			$nav[have_previous_section] = 0;
		}
		
		$next_section = $page_section + 1;
		if ( $next_section <= $no_of_sections ) {
			$nav[have_next_section] = 1;
			$nav[next_section_page] = ( $page_section * $nav[numPage] ) + 1;
		} else {
			$nav[have_next_section] = 0;
		}
		
		return $nav;
	}