View Single Post
Old 04-17-2009, 06:51 PM   #9 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Ok, I modified my function so that the DB query isn't within it. Now you have to do the DB query outside, then provide the results to the function. This SHOULD work for other types of data types correct?

You should be able to feed it results from other DB sources or feed it an array and it should still work....


Code:
/* QUERY TO GET TOTAL NUMBER OF ENTRIES FOR PAGINATE FUNCTION */
$query_total = "YOUR SELECT STATEMENT";
	
/* GET TOTAL NUMBER OF ENTRIES FIRST */
$results = mysql_query($query_total);
$totalrows = mysql_num_rows($results);


function pagination($get_page,$totalrows,$page_limit) {
			
/* SET SOME VARIABLES */
if(!$get_page) $p = 1;
if(!$display) $display = 5;
$page_next = ($get_page + 1);
$page_prev = ($get_page - 1);

/* CALCULATE LAST PAGE LINK */
/* THIS ALSO WILL BE USED IN THE FOR LOOP */
$page_last = ceil($totalrows/$page_limit);
	
/* USE $start VAR FOR DATABASE QUERY ex: LIMIT $start,$page_limit */
if($p == 1)	$start = 0;
else	$start = ($get_page - 1) * $page_limit;
				
/* CREATE OFFSET ex: 1 -25, 26 - 50 */
$starting_no = ($start +1);

/* CREATE ENDCOUNT ex: 1 - 25, THE 25 IS THE ENDCOUNT */
if (($totalrows - $start) < $page_limit) {
$end_count = $totalrows;
} elseif ($totalrows - $start >= $page_limit) { 
$end_count = $start + $page_limit; 
}

/* CREATE PAGES TO DISPLAY */
for ($i=1; $i<=$page_last; $i++) { 
					 
					 
if(($get_page - 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if(($get_page - 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if($get_page == $i) $display_pages[] = "$i";
if(($get_page + 1) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
if(($get_page + 2) == $i) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";

}
				
				
/* IF ON PAGE 1, UNLINK FIRST AND PREV LINKS */
if($get_page == 1) {
$first = "First";
$prev = "Prev";
}else{ 
$first = "<a href=\"$_SERVER[PHP_SELF]?p=1\">First</a>";
$prev = "<a href=\"$_SERVER[PHP_SELF]?p=$page_prev\">Prev</a>";
}
				
/* IF ON LAST PAGE, UNLINK LAST AND NEXT LINKS */
if($get_page == $page_last) {
$last = "Last";		
$next = "Next";
}else{
$last = "<a href=\"$_SERVER[PHP_SELF]?p=$page_last\">Last</a>";		
$next = "<a href=\"$_SERVER[PHP_SELF]?p=$page_next\">Next</a>";				
}
				
return (array($first,$prev,$display_pages,$next,$last,$totalrows,$starting_no,$start,$page_limit,$end_count));
			
}###END FUNCTION
			
list($first,$prev,$display_pages,$next,$last,$totalrows,$starting_no,$start,$page_limit,$end_count) = pagination($_GET[p],$totalrows,$page_limit);
allworknoplay is offline  
Reply With Quote