Simple PHP pagination function

The McCodes v2 game engine is quite minimal in a sense that it doesn’t with a lot of functionality to improve the interface of the game. It is up to the community to develop features and modifications to offer such necessary functions. Below is my PHP pagination function that I created. It is quite simple, but it does the job well.

Features

  • can be used anywhere in your game
  • ability to customize how many pages are displayed in the spread
  • CSS styled pagination elements
  • First Page and Last Page buttons

How to Use

The pagination function calls for 5 arguments; $total, $action, $page, $perpage, $spread.

  • $total: the total number of entries to use in your pagination. Ex. If this function was used for your events, you would need to send in the total number of events for the given user.
  • $action: the function requires you to use a $_GET action parameter to send the user back to the correct page. If you are using a different parameter, or not using a parameter at all, the function can be easily modified to accomodate
  • $page: what page the user is currently on
  • $perpage: how many instances you want to display (default 25)
  • $spread: the maximum number of pages you want to show between the first and last button (default 5)

The function will print out the page list and will return the value of the current page that you are on, so that you can use it in your query. The returned value will be whatever page the user is currently on multiplied by the total number of instances per page. This will be used as the starting value for your loop (in my case a MySQL query).

CODE

/* ------- pagination function -------
$total: number of instances to consider
$page: what page the user is on
$perpage: sorting how many instances per page is shown
$action: the action for the vent (ex. ?action=index)
$spread: how many page option links sit between the first and last links
*/

function pagination($total, $action, $page = 1, $perpage = 25, $spread = 5)
{
	global $db;

	//return nothing as the total number of instances is less than the per page sort
	if($total < $perpage) {
		return 0;
	}
	$page=abs(intval($page));

	//Calculate the last page
	$lastpage=floor($total/$perpage);

	//Class for page links
	$span_pre="<span class='pagelist'>";
	$span_suf="</span>";

	//Color styling for page link that the user is on
	$font_pre="<font color='#00FF33' weight='bold'>";
	$font_suf="</font>";

	//checking what the distance is away from the median of the spread (rounded to highest int)
	$spread_dist=intval($spread/2);

	//check if the total number of instances is great enough that first and last links are needed.
	$totalspread=$spread+2;
	if(($total/$perpage) >= $totalspread)
	{
		//Only show first link when player isn't viewing the first page
		if($page > ($spread_dist+1)) {
			//Show First link
			print "<a href='?action=$action&page=1'>".$span_pre."<< First".$span_suf."</a>..";
			$start_point = $page-$spread_dist;
			$end_point = $spread;
		}
		else {
			//Player is on first page already
			$start_point=1;
		}

		//Only show last link when player isn't viewing the last page
		if($page < ($lastpage-($spread_dist))) {
			//Show last link
			$lastlink="..<a href='?action=$action&page=$lastpage'>".$span_pre."Last >>".$span_suf."</a>";
			$end_point = $page+$spread_dist+1;
			if($start_point == 1) {
				$end_point = $start_point + $spread;
			}
		}
		else {
			//Player is on last page already
			$end_point=$lastpage+1;
			if($end_point > ($lastpage - $spread_dist)) {
				$start_point=$lastpage-$spread+1;
			}
		}

		//Inbetween pages
		for ($x = $start_point; $x < $end_point; $x++) {
			if($x==$page) {
				print"<a href='?action=$action&page=$x'>".$span_pre.$font_pre.$x.$font_suf.$span_suf."</a>";
			}
			else {
				print"<a href='?action=$action&page=$x'>".$span_pre.$x.$span_suf."</a>";
			}
		}

		print $lastlink;

		return ($page-1)*$perpage;
	}
	else {
		for ($x = 1; $x <= $lastpage+1; $x++) {
			if($x==$page) {
				print"<a href='?action=$action&page=$x'>".$span_pre.$font_pre.$x.$font_suf.$span_suf."</a>";
			}
			else {
				print"<a href='?action=$action&page=$x'>".$span_pre.$x.$span_suf."</a>";
			}
		}

		return ($page-1)*$perpage;
	}
}

Code also displayed at: http://www.makewebgames.com/showthread.php/39356-Pagination-Function

About the author
Michael Kretz is a Web Designer, Front-End Developer and Game Developer. He is the owner of Blade of Eternity and is currently interning in Geneva, Switzerland.