Pagenation with php example
by Adrian on Dec.19, 2008, under Programming
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">«</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">»</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.
General discussion RSS Feed
December 30th, 2008 on 10:42 pm
Pagination
January 5th, 2009 on 10:28 am
lol, ok