Jump to content

Recommended Posts

Does anyone have any nice prebuilt, re-useable functions that handle creating html numbed paged links to toggle between mysql result sets

 

e.g mysql returns 500 rows, you split that up into 50 pages, 10 rows

 

Im trying to avoid ugly code each time I need to do this, would be nice to have a reusable function that can handle returning the html paged links as well as maintain query strings

 

I've seen a few on google but they arnt too great. Also the PHP_Pear is not to hot either

 

How do you experts on here go about this :)

Link to comment
https://forums.phpfreaks.com/topic/166997-solved-pagination-html-links/
Share on other sites

http://www.tonymarston.net/php-mysql/pagination.html

 

I found this link super helpful.  Only about 20 lines of code and its easy to modify to make your own style from it.  You'll need to be more specific if you want further help though.

Ah I ended up making my own, I guess the best is one that works for you. Here it is :)

 

// ###########################################
// FUNCTION TO GENERATE DROP DOWN PAGE LINKS #
// ###########################################

function page_links($rowsPerPage, $totalRows, $currentRow, $url = false) {

// ----------------------------------
// BUILD SELECT OPTION FOR EACH PAGE 
// ----------------------------------

	if ($totalRows % $rowsPerPage == 0) {$offset = 0;} else {$offset = 1;}

	$pages = floor($totalRows / $rowsPerPage) + $offset;

	while ($i < $pages) {
		$i++;
		$page_from = ($i * $rowsPerPage) - $rowsPerPage;	
		if ($page_from == $currentRow) {$selected = " SELECTED";} else {$selected = "";}
		$page_options .= "<option value=\"$page_from\"{$selected}>Page $i</option>";
	}

// -------------------------------
// REMOVE pageFrom= FROM QUERY STRING 
// -------------------------------

	if (!$url) {$url = $_SERVER['QUERY_STRING'];}

	parse_str($url, $queryString);
	unset($queryString['rowFrom']);
	$urlQuery = rawurldecode(http_build_query($queryString));

// --------------------------------------
// PUT THE DROP DOWN TOGETHER AND RETURN 
// --------------------------------------

	$page_jump = "<SELECT style=\"width:80px\" id=\"quick_jump_menu\" OnChange=\"ajaxopen('#center_content', '$_SERVER[sCRIPT_NAME]?$urlQuery&rowFrom=' + this.value)\">$page_options</SELECT>";

	return $page_jump;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.