PDA

View Full Version : Pagination without a database


n8w
7-13-04, 01:47 AM
Hello .. I am trying to figure out how I do pagination from an multidimensional array? I do not have a this info in a database and all the tutorials I am reading.. I get kind of lost because they are showing how to do it with a database
example: http://www.phpnoise.com/tutorials/9/5

Xinil showed me how to create an html table from the multidimensional array
http://forum.powweb.com/showthread.php?t=39017

Now I just need to figure out how to limit 16 images per page
here is how it currently looks
http://www.n8w.com/newweb/thumbnails.php?arr=color&text=Color

any help .. is greatly appreciated
thanks
n8w

n8w
7-13-04, 01:50 AM
PS .. here is what the code looks like for the current thumbnails.php page

<?php include("inc-top.php"); ?>
<?php
// assign the temp with the array from the query string
require_once("inc-image-array.php");
$arr = $_GET['arr'];
if( $arr == "color" ) {
$arr_temp=$arr_color;
}elseif($arr == "bw"){
$arr_temp=$arr_bw;
}
// used to write number of thumbnails
$length=count($arr_temp)-1;

?>
<table width="760" border="0" align="center" cellpadding="5" cellspacing="2">
<?
$num = 1;
$columns = 4;
$a = 1;
while ($num <= $length) {
if ($num <= 4) {
$new = TRUE;
} else {
$new = FALSE;
}
if ($a == 1)
echo '<tr>';
?>
<td align="center">
<? $id=$arr_temp[$num][0];
$text=$arr_temp[$num][1];
$client=$arr_temp[$num][2];
$date=$arr_temp[$num][3];
$category=$arr_temp[$num][4];
?>
<p><a href="image.php?id=<?= $id ?>&arr=<?= $arr ?>&text=<?= $text ?>&client=<?= $client ?>&date=<?= $date ?>&category=<?= $category ?>"><img src="images/<?= $id ?>-s.gif" border="0" height="100" alt="<?= $text ?> - <?= $client ?>"></a><br>
<?
if ($new)
echo '<span class="new">NEW</span><br>'
?>
<?php echo $text; ?><br><br></p></td>
<?
if ($a == $columns) {
echo '</tr>';
$a = 0;
}
$a++;
$num++;
}
?>
</table>
<?php include("inc-bottom.php"); ?>

Pig
7-13-04, 12:22 PM
You need to do 2 things.
1. determine the portion of the array you want on this page
2. determine what links to display to navigate them


//num of images per page
$limit = 16;

//determine what page we are on
if( empty($_GET['page']) ) $page = 1;
else $page = $_GET['page'];

//determine the current set of images
$start = $limit*$page -1;
$end = $start +$limit;

That will give you the range for the current page. Then you would loop through it with something like:
for( $i = $start; $i <= $limit; $i++ )
{
//do stuff
}

To calculate the links at the bottom, you just do a little math.

if( $page > 1 ) echo("<a href='" . $_SERVER['PHP_SELF'] . "?page=" . ($page -1) . "'>previous</a>");
if( $page < (sizeof($array) -($page*$limit) ) echo("<a href='" . $_SERVER['PHP_SELF'] . "?page=" . $page++ . "'>next</a>");

n8w
7-13-04, 12:40 PM
Awesome .. thank you .. its working perfectly now :)