Pagenation with php example

Pagenate an array into a table using PHP.

This is a basic example and has been depreciated, a far more advanced and upto date example can be found here.

(Thanks to Andrew (Morf) for his help on this)

To see it in action click here

There are user configurable values at the top, such as query name, and number of results per page etc, the important thing to remember though, is the page link must be correct

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<style>
.nav_arrow {color:#222222; text-decoration:none;}
.nav_number{color:#777777; text-decoration:none;}
.nav_number_current{color:#222222; text-decoration:underline;}
</style>
 
<div style="width:80%; margin:0 auto;">
 
	<?php  
	// do your database query here and store it within array	
 
	// test content (just fills array with test content)
	$contentLimit=50;
	for ($x=0; $x<$contentLimit; $x++){
		$array[$x]['Thing_1'] = $x.'a';
		$array[$x]['Thing_2'] = $x.'b';
		$array[$x]['Thing_3'] = $x.'c';
		$array[$x]['Thing_3'] = $x.'d';
		$array[$x]['Thing_3'] = $x.'e';
		}
	?>
 
 
	<?php // output
 
	// pagination loop begins
	$result = $array;
	$limit = intval(20);
	$pagelink = 'pagenation.php';
	$queryString = 'stat';
 
	// pagenation begins, KUDOS to andrew for his considerable help here
	$listing = intval($_GET[$queryString]);
	$start = $listing * $limit;
 
	$all = count($result);
	$all_pages = ceil($all / $limit);
 
	// this forwards any criteria associated with the search by adding it onto the end of the hyperlink
	$args=""; foreach ($_GET as $key=>$val){
		$args.= $key!=$queryString ? "&".$key."=".$val : "" ;
		}
 
	if($all>0){
		if(($listing+1) > $all_pages){$listing=0;}
		$of = 0;
 
		////////////////////////////////////////// navigation starts
 
		if(($listing+1) > 1 && ($listing+1) <= $all_pages){
			// up
			echo '<a href="'.$pagelink.'?'.$queryString.'='.($listing-1).$args.'" class="nav_arrow" title="previous" alt="previous">&#171;</a>';
			}
 
		if ($all_pages>1){
			// goto page
			for($i=0;$i<$all_pages;$i++){
 
				// echo`s the number also checks to see if this is the current page
				echo $listing==$i ? '<a href="'.$pagelink.'?'.$queryString.'='.$i.$args.'" class="nav_number_current" title="current" alt="current">'.($i+1).'</a>' : '<a href="'.$pagelink.'?'.$queryString.'='.$i.$args.'" class="nav_number" title="page '.($i+1).'" alt="page '.($i+1).'">'.($i+1).'</a>';
				if($i < ($all_pages-1)) echo ' | ';
				}
			}
 
		if(($listing+1) < $all_pages){
			// down
			echo ' <a href="'.$pagelink.'?'.$queryString.'='.($listing+1).$args.'" class="nav_arrow" title="next" alt="next">&#187;</a>';
			}
 
		///////////////////////////////////////// navigation ends
	?>
	<br />
	<table border="1" cellpadding="2" cellspacing="1"  width="100%">
		<tr>
			<th class='tableHeader'>Thing 1</th>
			<th class='tableHeader'>Thing 2</th>
			<th class='tableHeader'>Thing 3</th>
		</tr>
		<?php
		// pagenation resumes
 
		for($i=$start;$i<($start+$limit);$i++){	
	                          if (empty($result[$i])) break; // break if complete
			// format results
			?>
			<tr>
				<td class='tableContents'><?php echo $result[$i]['Thing_1']; ?></td>
				<td class='tableContents'><?php echo $result[$i]['Thing_2']; ?></td>
				<td class='tableContents'><?php echo $result[$i]['Thing_3']; ?></td>
			</tr>
		<?php }	?>
	</table>
	<?php
	}
	else{
		// zero results found
		echo '<h1>No results found</h1>';
		}
	?>	
</div>

 

You can download the complete zipped file here

I have now developed a fully automated function here which is far superior.

VN:F [1.9.9_1125]
Rating: 6.3/10 (3 votes cast)
VN:F [1.9.9_1125]
Rating: 0 (from 0 votes)
Pagenation with php example, 6.3 out of 10 based on 3 ratings

17,910 views

This entry was posted on Friday, December 19th, 2008 at 3:04 pm and is filed under Programming. You can follow any responses to this entry through the RSS 2.0 feed.

2 Responses to “Pagenation with php example”

  1. Lint Filter says:

    Pagination

    VA:F [1.9.9_1125]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.9_1125]
    Rating: 0 (from 0 votes)
  2. Adrian says:

    lol, ok

    VN:F [1.9.9_1125]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.9_1125]
    Rating: 0 (from 0 votes)

Leave a Reply