TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 04-16-2009, 02:00 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default Pagination Function

Hey guys, here's the pagination function that I've been working on. It is based off of Websavvy's pagination program so I don't want to take full credit for it. Everything works as I want to except one feature and I will explain that later. But here's the code.


Code:
/* GET CURRENT PAGE */
$get_page = $_GET['p'];
/* SET PAGE LIMIT TO 25 FOR CURRENT TESTING */
$page_limit = "25";

/* QUERY TO GET TOTAL NUMBER OF ENTRIES FOR PAGINATE FUNCTION */
$query_total = "SELECT * FROM schedules";


function pagination($get_page,$display,$query_total,$page_limit) {
			
/* SET SOME VARIABLES */
if(!$get_page) $p = 1;
if(!$display) $display = 5;
$page_next = ($get_page + 1);
$page_prev = ($get_page - 1);
				
				
/* GET TOTAL NUMBER OF ENTRIES FIRST */
$results = mysql_query($query_total);
$totalrows = mysql_num_rows($results);

/* CALCULATE LAST PAGE LINK */
$page_last = ceil($totalrows/$page_limit);
	
/* USE $start VAR FOR DATABASE QUERY ex: LIMIT $start,$page_limit */
$start = ($get_page - 1) * $page_limit;
				
$starting_no = $start + 1;

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 ($i != $get_page) $display_pages[] = " <a href=\"$_SERVER[PHP_SELF]?p=$i\">$i</a>";
else $display_pages[] = " $i"; 
					
}
				
				
/* 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],5,$query_total,$page_limit);

echo "<span class=\"style13\">" . "$first | $prev" . implode(' ',$display_pages) . " $next | $last" . "</span>";
Code:
OUTPUT YOUR RECORDS: SECOND QUERY USING LIMIT

$query2 = "SELECT * FROM schedules LIMIT $start,$page_limit";
$result2 = mysql_query($query2);

etc etc etc......


Ok, so here is what the output is:

First | Prev 1 2 3 4 Next | Last

and

Viewing Items (1 - 25 of 76 Entries)



Everything works pretty well. When you are on the first page,
the "First" and "Prev" gets unlinked since there's no need to click on them. If you are on the last page, the same thing happens for the "Next" and "Last" links..

There is only one issue that I have, and that is the "1 2 3 4" part. That is based on how many entries are in the table divided by how many items to list per page.

Anyways, if you have a lot of entries, say 1,000. You will get too many links like this: "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ....etc".

What I would like to do is keep it "clean". Displaying only 5 at a time with the "current" page in the middle:

So say you are on page 3. It would look like this.

First | Prev 1 2 3 4 5 Next | Last


If you're on page 7. It would look like this.

First | Prev 5 6 7 8 9 Next | Last


If anyone has "hints" please let me know, it's one of those things that you can either figure out in 5 minutes or it takes a day...I don't think it's hard, just a matter of "figuring" it out...but I know there are a lot of people here with "pagination" experience.

Also, PLEASE comment on my code, if you have suggestions to make it better, let me know, I won't be offended...

Just don't tell me to do it in OO because I'm still new to that and that will be my next project...
allworknoplay is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Advance Pagination Class Rendair Advanced PHP Programming 13 02-01-2013 11:08 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM


All times are GMT. The time now is 04:48 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design