|
Posted by ste on 05/05/06 05:51
Hi there,
Further to my recent posts where I've received excellent help from Rik and
Jerry, I've ended up with an image gallery on my website that displays
images in a table, 3 images per row. This works great and opens all images
in the database when I open the url mywebsite/gallery.php, or I can choose
certain images (by category) by going to url's like
mywebsite/gallery.php?category=landscape
Although the above worked perfectly with 15 images in the database, I've
realised that as I add more images to the database (I will probably have a
few hundred images in total before long), I'm going to need to sort out the
gallery pages so that instead of displaying, say, 100 images in a big long
page, it instead displays 12 images per page, and then has links to Next,
Previous, First, Last, etc.
After spending an hour going through Google searches, I've found some code
which enables pagination from this website tutorial:
http://www.tonymarston.co.uk/php-mysql/pagination.html
I've inserted this code into my existing code, yet have came across a small
problem. The pagination now works when I go to mywebsite/gallery.php and
it's great. However, when I go to mygallery/gallery.php?category=landscape,
instead of showing the paginated Landscape gallery, it instead shows all
images from the database in paginated format, which is basically the same as
the mywebsite/gallery.php page
In adding the above pagination code, I've somehow messed something up when
wanting to open galleries based on categories. I've copied the code below -
can anyone see anything obvious I've done wrong? Through trial and error,
I've tried to adjust the queries in the code, but to no avail.
Thanks for any help,
Stephen
<?php
include("databasepasswords");
$connection = mysql_connect($host,$user,$password) or die ("couldn't
connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select
database");
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
} else {
$pageno = 1;
} // if
$query = "SELECT count(*) FROM images ".$where;
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];
$rows_per_page = 12;
$lastpage = ceil($numrows/$rows_per_page);
$pageno = (int)$pageno;
if ($pageno < 1) {
$pageno = 1;
} elseif ($pageno > $lastpage) {
$pageno = $lastpage;
} // if
$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;
$query = "SELECT * FROM images ORDER BY dateadded, datetaken DESC $limit
".$where ;
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
if(get_magic_quotes_gpc()){
$_GET['category'] = stripslashes($_GET['category']);
}
$category = mysql_real_escape_string( $_GET['category'], $connection);
$category = ucfirst($category);
$where = (isset($_GET['category'])&&$_GET['category']!='') ? "WHERE category
= '".$category."'":'';
$result = mysql_query($query) or die ("Couldn't execute query.");
echo "\n<div id=\"content\">";
echo "\n<h2>$category Gallery</h2>";
if ($pageno == 1) {
echo " FIRST PREV ";
} else {
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
$prevpage = $pageno-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if
echo " ( Page $pageno of $lastpage ) ";
if ($pageno == $lastpage) {
echo " NEXT LAST ";
} else {
$nextpage = $pageno+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if
echo "\n<table id=\"thumbgallery\" cellSpacing=\"0\" cellPadding=\"10\"
width=\"400\" border=\"0\">";
echo "\n<tbody>";
$i = 3;
while ($row = mysql_fetch_assoc($result))
{
if($i==3) echo "\n\t<tr>";
echo "\n\t\t<td valign=\"top\" width=\"113\" id=\"thumbs\"
align=\"center\"><a
href=\"/image_viewer.php?imageid=".$row['imageid']."\"><img
src=\"/images/gallery/t".$row['imageid'].".jpg\" border=\"0\"
alt=\"".$row['caption']."\" /></a><p class=\"captionref\">Ref:
".$row['imageid']."</p><p class=\"caption\">".$row['caption']."</p></td>";
$i--;
if($i==0) {
echo "\n\t<tr>";
$i = 3;
}
}
if($i!=3) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</tbody>";
echo "\n</table>";
?>
Navigation:
[Reply to this message]
|