ray-solomon
asked on
Need help with pagination
I am creating a simple gallery script with pagination. This will find all images in a directory and show the navigation, image and some image information. This works great, but there is only one problem. The pagination starts at 0 and I would like it to start a 1 instead. I think there needs to be a loop somewhere to add +1 to the current pagination numbers. But I am not sure where to put this.
Here is the directory structure:
Start at document root: /
/gallery
/gallery/index.php
/gallery/.htaccess
/gallery/images/ <- several images in here
On a side note: I use mod_rewrite to make it look like static html pages like this: http://www.mysite.com/gallery/1.html
#start htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteBase /gallery/
RewriteRule ^([^/]+)\.html$ index.php?page=$1 [L]
#end htaccess file
<?php
// Get the requested page
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
// Set the image directories
$image_dir = "./images/";
// Reading the thumbnail directory an putting the filenames into an array
$readdir = opendir("$image_dir");
while (false !== ($file = readdir($readdir)))
{
if ($file != "." && $file != "..")
{
$gallery[] = $file;
}
}
closedir($readdir);
// Calculating the number of images in the array
$total_images = count($gallery);
// Set current, prev and next page
$prev = ($page - 1);
$next = ($page + 1);
// Max results per page
$max_results = 1;
// Calculate total pages
$total_pages = ceil($total_images / $max_results);
// Predefine this var
$pagination = '';
// Create a PREV link if there is one
if ($page >= 1)
{
$pagination .= '<a href="'.$prev.'.html"> Previous </a> ';
}
// Loop through the total pages
for ($i = 0; $i <= $total_pages; $i++)
{
if (($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="'.$i.'.html"> '.$i.' </a> ';
}
}
// Print NEXT link if there is one
if ($page <= $total_pages)
{
$pagination .= '<a href="'.$next.'.html"> Next </a> ';
}
// Show the navigation
echo $pagination.'<br><br>You are viewing result '.$page.' of '.$total_images.'<br>';
// Show the gallery
if ($gallery[$page])
{
echo '<a href="'.$image_dir.$galler y[$page].' "
title="Show image" target="_blank"><img src="'.$image_dir.$gallery [$page].'"
style="margin: 5px;" border="0"></a><br />';
// Removing the extension from the filename and other misc functions
$name = substr_replace($gallery[$p age],'',-4 );
$name = ucwords($name);
$dimensions = getimagesize($image_dir.$g allery[$pa ge]);
$size = filesize($image_dir.$galle ry[$page]) /1024;
if ($size <= 1024)
{
$filesize = number_format($size,1,'.', '.').' Kilobytes';
}
else
{
$filesize = number_format($size,1,'.', ',').' Megabytes';
}
echo 'Name: '.$name.'<br />';
echo 'Dimensions: ' .$dimensions[0].' x '.$dimensions[1].' Pixels<br />';
echo 'Filesize: '.$filesize.' <br />';
}
echo '<br /><br />';
?>
Here is the directory structure:
Start at document root: /
/gallery
/gallery/index.php
/gallery/.htaccess
/gallery/images/ <- several images in here
On a side note: I use mod_rewrite to make it look like static html pages like this: http://www.mysite.com/gallery/1.html
#start htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteBase /gallery/
RewriteRule ^([^/]+)\.html$ index.php?page=$1 [L]
#end htaccess file
<?php
// Get the requested page
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
// Set the image directories
$image_dir = "./images/";
// Reading the thumbnail directory an putting the filenames into an array
$readdir = opendir("$image_dir");
while (false !== ($file = readdir($readdir)))
{
if ($file != "." && $file != "..")
{
$gallery[] = $file;
}
}
closedir($readdir);
// Calculating the number of images in the array
$total_images = count($gallery);
// Set current, prev and next page
$prev = ($page - 1);
$next = ($page + 1);
// Max results per page
$max_results = 1;
// Calculate total pages
$total_pages = ceil($total_images / $max_results);
// Predefine this var
$pagination = '';
// Create a PREV link if there is one
if ($page >= 1)
{
$pagination .= '<a href="'.$prev.'.html"> Previous </a> ';
}
// Loop through the total pages
for ($i = 0; $i <= $total_pages; $i++)
{
if (($page) == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="'.$i.'.html"> '.$i.' </a> ';
}
}
// Print NEXT link if there is one
if ($page <= $total_pages)
{
$pagination .= '<a href="'.$next.'.html"> Next </a> ';
}
// Show the navigation
echo $pagination.'<br><br>You are viewing result '.$page.' of '.$total_images.'<br>';
// Show the gallery
if ($gallery[$page])
{
echo '<a href="'.$image_dir.$galler
title="Show image" target="_blank"><img src="'.$image_dir.$gallery
style="margin: 5px;" border="0"></a><br />';
// Removing the extension from the filename and other misc functions
$name = substr_replace($gallery[$p
$name = ucwords($name);
$dimensions = getimagesize($image_dir.$g
$size = filesize($image_dir.$galle
if ($size <= 1024)
{
$filesize = number_format($size,1,'.',
}
else
{
$filesize = number_format($size,1,'.',
}
echo 'Name: '.$name.'<br />';
echo 'Dimensions: ' .$dimensions[0].' x '.$dimensions[1].' Pixels<br />';
echo 'Filesize: '.$filesize.' <br />';
}
echo '<br /><br />';
?>
If everything is working right as you said, it could be tricky, because you may want to only display $page + 1, but internally your pagination system in zero based. So you could just change what you are getting from your URL
It could be as simple as changing the line of code to this:
// Get the requested page
$page = (!isset($_GET['page']))? 0 : $_GET['page'] - 1;
Or there may be more to it. It's hard to say without the whole picture.
It could be as simple as changing the line of code to this:
// Get the requested page
$page = (!isset($_GET['page']))? 0 : $_GET['page'] - 1;
Or there may be more to it. It's hard to say without the whole picture.
ASKER
I tried both of your suggestions, but it still will not work. I modified the code so it can be used without mod_rewrite. If anyone would like to test this, just put this script into a new directory, then create a subdirectory called "images", then put some images in that.
// rename to index.php
<?php
// Get the requested page
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
// Set the image directories
$image_dir = "./images/";
// Reading the thumbnail directory an putting the filenames into an array
$readdir = opendir($image_dir);
while (false !== ($file = readdir($readdir)))
{
if ($file != "." && $file != "..")
{
$gallery[] = $file;
}
}
closedir($readdir);
// Calculating the number of images in the array
$total_images = count($gallery);
// Set current, prev and next page
$prev = ($page - 1);
$next = ($page + 1);
// Max results per page
$max_results = 1;
// Calculate total pages
$total_pages = ceil($total_images / $max_results);
// Predefine this var
$pagination = '';
// Create a PREV link if there is one
if ($page >= 2)
{
$pagination .= '<a href="index.php?page='.$pr ev.'"> Previous </a> ';
}
// Loop through the total pages
for ($i = 0; $i <= $total_pages; $i++)
{
if ($page == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="index.php?page='.$i. '"> '.$i.' </a> ';
}
}
// Print NEXT link if there is one
if ($page <= $total_pages)
{
$pagination .= '<a href="index.php?page='.$ne xt.'"> Next </a> ';
}
// Show the navigation
echo $pagination.'<br><br>You are viewing result '.$page.' of '.$total_images.'<br>';
// Show the gallery
if ($gallery[$page])
{
echo '<a href="'.$image_dir.$galler y[$page].' "
title="Show image" target="_blank"><img src="'.$image_dir.$gallery [$page].'"
style="margin: 5px;" border="0"></a><br />';
// Removing the extension from the filename and other misc functions
$name = substr_replace($gallery[$p age],'',-4 );
$name = ucwords($name);
$dimensions = getimagesize($image_dir.$g allery[$pa ge]);
$size = filesize($image_dir.$galle ry[$page]) /1024;
if ($size <= 1024)
{
$filesize = number_format($size,1,'.', '.').' Kilobytes';
}
else
{
$filesize = number_format($size,1,'.', ',').' Megabytes';
}
echo 'Name: '.$name.'<br />';
echo 'Dimensions: ' .$dimensions[0].' x '.$dimensions[1].' Pixels<br />';
echo 'Filesize: '.$filesize.' <br />';
}
echo '<br /><br />';
?>
// rename to index.php
<?php
// Get the requested page
$page = (!isset($_GET['page']))? 1 : $_GET['page'];
// Set the image directories
$image_dir = "./images/";
// Reading the thumbnail directory an putting the filenames into an array
$readdir = opendir($image_dir);
while (false !== ($file = readdir($readdir)))
{
if ($file != "." && $file != "..")
{
$gallery[] = $file;
}
}
closedir($readdir);
// Calculating the number of images in the array
$total_images = count($gallery);
// Set current, prev and next page
$prev = ($page - 1);
$next = ($page + 1);
// Max results per page
$max_results = 1;
// Calculate total pages
$total_pages = ceil($total_images / $max_results);
// Predefine this var
$pagination = '';
// Create a PREV link if there is one
if ($page >= 2)
{
$pagination .= '<a href="index.php?page='.$pr
}
// Loop through the total pages
for ($i = 0; $i <= $total_pages; $i++)
{
if ($page == $i)
{
$pagination .= $i;
}
else
{
$pagination .= '<a href="index.php?page='.$i.
}
}
// Print NEXT link if there is one
if ($page <= $total_pages)
{
$pagination .= '<a href="index.php?page='.$ne
}
// Show the navigation
echo $pagination.'<br><br>You are viewing result '.$page.' of '.$total_images.'<br>';
// Show the gallery
if ($gallery[$page])
{
echo '<a href="'.$image_dir.$galler
title="Show image" target="_blank"><img src="'.$image_dir.$gallery
style="margin: 5px;" border="0"></a><br />';
// Removing the extension from the filename and other misc functions
$name = substr_replace($gallery[$p
$name = ucwords($name);
$dimensions = getimagesize($image_dir.$g
$size = filesize($image_dir.$galle
if ($size <= 1024)
{
$filesize = number_format($size,1,'.',
}
else
{
$filesize = number_format($size,1,'.',
}
echo 'Name: '.$name.'<br />';
echo 'Dimensions: ' .$dimensions[0].' x '.$dimensions[1].' Pixels<br />';
echo 'Filesize: '.$filesize.' <br />';
}
echo '<br /><br />';
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
One more thing bud,
I just want to also mention I *just* noticed your code gives you the option on the last page to go one page more. Just thought I'd let you know that you should include a check to not allow next to go past the total pages.
I just want to also mention I *just* noticed your code gives you the option on the last page to go one page more. Just thought I'd let you know that you should include a check to not allow next to go past the total pages.
ASKER
Thank you for your help . It is all working now.
I fixed the problem that you described above.
// Print NEXT link if there is one (Took out the equal sign)
if ($page < $total_pages)
My next modification is to limit the amount of page numbers seen between the NEXT & PREV links.
=]
I fixed the problem that you described above.
// Print NEXT link if there is one (Took out the equal sign)
if ($page < $total_pages)
My next modification is to limit the amount of page numbers seen between the NEXT & PREV links.
=]
Take a look at this block from your code:
// Loop through the total pages
for ($i = 0; $i <= $total_pages; $i++)
{
if (($page) == $i)
{
$pagination .= $i;
Now you are setting the variable $i to 0. I believe that is where your problem is. If not, then double check the fact that you are going to less than/equal to the $total_pages.
But I'm fairly certain that you meant for that 0 to be a 1.