Title: pagination script how to create page numbers links function php
Description: pagination script how to create page numbers links function php PHP
Tags: pagination, script, how, to, create, page, numbers, links, function, php
Info: This Post Has Been Viewed 8214 Times SinceMon Feb 18, 2008 11:00 pm Author hostman With 16 Replies #5775

pagination script how to create page numbers links function php

Welcome to Wallpaperama Forums, This script was provided by our friend at Webune.com - Webune specialize in Web Hosting for Individual just like you. To enhance our service we provide helpful tutorials to help you make your scripting easier. this short tutorial is to show you how you can make a simple pagination script with a function, this function will create a pagination menu, similar to the one found here at wallpaperama.


each number page will have a link using the GET method in url


lets say for example i have a forums, and for each topic i want to show only 20 replies per page. so lets say i have a forums topic with 200 replies, so if you do that math, i should end up with 10 pages.


now all you have to do to see this script in action, is copy and paste to your text editor like notepad. then save it as webune-pagination.php and upload to your website, then open it in your browser and you will see how it works.


Thanks to the folks at www.webune.com for their support on this.


webune-pagination.php this function will create a pagination menu, similar to the one found here at wallpaperama.

each number page will have a link using the GET method in url

lets say for example i have a forums, and for each topic i want to show only 20 replies per page. so lets say i have a forums topic with 200 replies, so if you do that math, i should end up with 10 pages.

now all you have to do to see this script in action, is copy and paste to your text editor. then save it as webune-pagination.php and upload to your website, then open it in your browser and you will see how it works.

Thanks to the folks at www.webune.com for their support on this.

webune-pagination.php
<?php
# THIS FUNCTION CREATED THE URL
function pagination_link($id, $page_num){
	return $_SERVER['PHP_SELF'].'?page_num='.$page_num;
}
###### PAGINATION FUNCTION ###### 
function pagination($num_of_items, $items_per_page, $id, $page_num, $max_links){
	$total_pages = ceil($num_of_items/$items_per_page);
	if($page_num) {
		if($page_num >1){ 
			$prev = ' &nbsp; <a href="'.pagination_link($id, ($page_num -1 )).'">&lt; PREV</a> &nbsp; '; 
			$first = '<a href="'.$_SERVER['PHP_SELF'].'">First Page &lt;&lt;</a>'; 
		}
	}
	if($page_num <$total_pages){ 
		$next = ' &nbsp; <a href="'.pagination_link($id, ($page_num+1)).'">NEXT &gt;</a> &nbsp; '; 
		$last = ' &nbsp; <a href="'.pagination_link($id, $total_pages).'"> LAST PAGE &gt;&gt;</a> &nbsp; ';
	}
	echo $first;
	echo $prev;
	$loop = 0;
	if($page_num >= $max_links) {
		$page_counter = ceil($page_num - ($max_links-1));
	} else {
		$page_counter = 1;
	}
	if($total_pages < $max_links){
		$max_links = $total_pages;
	}
	do{ 
		if($page_counter == $page_num) {
			echo ' &nbsp; <strong>'.$page_counter.'</strong> &nbsp; '; 
		} else {
			echo '<a href="'.pagination_link($id, ($page_counter)).'">'.$page_counter.'</a> &nbsp; ';
		} 
		$page_counter++; $current_page=($page_counter+1);
		$loop++;
	} while ($max_links > $loop);
echo $next;
echo $last;
}
?>
<h1>PAGINATION SCRIPT BY <a href="http://www.webune.com">WWW.WEBUNE.COM</a></h1>
<?php
#################################
# Please Do Not Remove
# SCRIPT CREATED BY WEBUNE.COM
#################################
# CONFIGURATION EXAMPLE
$num_of_items = 200;
$items_per_page = 9;
$max_links = 10;

if($_GET['page_num']){
	$page_num = $_GET['page_num'];
} else {
	$page_num = 1;
}
?>
<h1>PAGE: <?php echo $page_num; ?></h1>
<strong>example:</strong> <br>
<strong>number of items</strong> = <?php echo $num_of_items; ?><br>
<strong>number of items per page</strong> = <?php echo $items_per_page; ?><br>
<strong>number of links per page</strong> = <?php echo $max_links; ?><br>
<div align="center">
	<?php echo pagination($num_of_items, $items_per_page, $id, $page_num, $max_links); ?> 
</div><br><br>
<div align="center"><a href="http://www.webune.com">PHP/MYSQL WEB HOSTING<br>
<img src="http://www.webune.com/images/headers/default_logo.jpg" border="0"></a></div>





hope this helps, please provide any comments, whether be good or bad

Comments (16)
View Top Comments
Leave Your Comments...
#1
Joe:
3 years ago
#58879
Joe Sun Jun 22, 2008 5:50 pm
hi,

i'm trying to use your webune-pagination.php script for my website. i added it to the bottom of one of my pages and it seems to work as far as showing up and everything. i don't really know much about php.

how dose it work? i've only got one page in the directory but i want to add allot more. how should i name the pages? i'm i suppose to include a page number or something or does it work by using the same page only changing the products? or dose it just cycle through the pages in that directory?

thank you

joe




#2
Sean:
3 years ago
#67166
Sean Mon Aug 18, 2008 8:38 pm
thanks for the script thats exactly what i was looking for. found your site via google.
thanks,
sean gallagher
(atlwebsite )
#3
amit:
3 years ago
#69406
amit Thu Sep 04, 2008 8:58 pm
hei is so noce,.....
#4
damien:
3 years ago
#97313
damien Mon Mar 16, 2009 11:43 am
i am belgi this is very zero script
#5
damien:
3 years ago
#97315
damien Mon Mar 16, 2009 11:44 am
i am belgi this is very zero script
#6
Jitendra:
2 years ago
#113725
Jitendra Tue Aug 25, 2009 2:46 am
this script is very good .\r\nthis is very helffull and user friendly.\r\n\r\nthanks alote
#7
Jitendra:
2 years ago
#113727
Jitendra Tue Aug 25, 2009 2:48 am
this script is very good .\r\nthis is very helffull and user friendly.\r\n\r\nthanks alote
#8
Jitendra:
2 years ago
#113728
Jitendra Tue Aug 25, 2009 2:48 am
this script is very good .\r\nthis is very helffull and user friendly.\r\n\r\nthanks alote
#9
1
Jitendra:
2 years ago
#113729
Jitendra Tue Aug 25, 2009 2:48 am
this script is very good .\r\nthis is very helffull and user friendly.\r\n\r\nthanks alote
#10
hemanth:
2 years ago
#119531
hemanth Wed Nov 04, 2009 1:00 am
hi ,

the above code is working fine with all our requirements.

thanks
hema
#11
Jehu:
1 year ago
#148359
Jehu Fri Nov 26, 2010 12:34 pm
I used your code on my site, the page i used it on i usually embed videos but what i would like to know is... How do i get it to show only 6 videos on a page and if i input more videos the lower ones shift to the next page???
#12
chandsys:
7 months ago
#165219
chandsys Mon Aug 22, 2011 5:03 pm
its veary useful learn...
#13
Greg:
6 months ago
#167611
Greg Sun Oct 02, 2011 1:51 pm
I have similiar questions to the person below. I am trying to use this to display a certain amount of article posts on a page. How does this work?
#14
Asad:
3 months ago
#173873
Asad Fri Jan 13, 2012 11:33 pm
Amazing website
#15
Bla:
5 seconds ago
#180983
Bla Mon Apr 16, 2012 1:43 am
It's a shame no one answers to the question.
I am having the same problem as previously stated.
Leave Your Comments...
Share
| More