472,126 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,126 software developers and data experts.

Gallery Pagination Result Problem

ste
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>";

?>
May 5 '06 #1
11 2581
ste wrote:
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>";

?>


Howdy.

One thing I see - you use $where before you set it. What happens if you move
the code which sets $where earlier in your routine?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 5 '06 #2
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:SN******************************@comcast.com. ..
<snip>
Howdy.

One thing I see - you use $where before you set it. What happens if you
move the code which sets $where earlier in your routine?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Hi Jerry,

When I moved the code which set's $where to above the first $where, I found
that although gallery.php worked fine, gallery.php?category=landscape failed
to run and I got an error message.

Do you have any more ideas at all?

I feel like the problem is to do with the $query values and I'm just not
setting them correcly with the new piece of code, or not in the right order.

To add to this, I've just noticed that when I load the landscape gallery,
not only does it display all images now (albeit in a paginated format)
instead of just the landscape images, but if I click the paginated result
for page 2, it changes the page title back to just Gallery, instead of the
main page of Landscape gallery.

This looks like it's more involved than I thought it would be! :-(

Again, if you or anyone else spot anything else obvious, please let me know.

Thanks,

Stephen
May 6 '06 #3
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:SN******************************@comcast.com. ..
<snip>
Howdy.

One thing I see - you use $where before you set it. What happens if you
move the code which sets $where earlier in your routine?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

When I moved the code which set's $where to above the first $where, I found
that although gallery.php worked fine, gallery.php?category=landscape failed
to run and I got an error message.

Do you have any more ideas at all?

I feel like the problem is to do with the $query values and I'm just not
setting them correcly with the new piece of code, or not in the right order.

To add to this, I've just noticed that when I load the landscape gallery,
not only does it display all images now (albeit in a paginated format)
instead of just the landscape images, but if I click the paginated result
for page 2, it changes the page title back to just Gallery, instead of the
main page of Landscape gallery.

This looks like it's more involved than I thought it would be! :-(

Again, if you or anyone else spot anything else obvious, please let me know.

Thanks,

Stephen


Stephen,

Sounds like you need to study the code and understand it a little more.

I've never found it productive to change code (or even implement someone else's
code as part of mine) until I understood just what it was doing.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #4
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:mM******************************@comcast.com. ..
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:SN******************************@comcast.com. ..
<snip>
Howdy.

One thing I see - you use $where before you set it. What happens if you
move the code which sets $where earlier in your routine?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

When I moved the code which set's $where to above the first $where, I
found that although gallery.php worked fine,
gallery.php?category=landscape failed to run and I got an error message.

Do you have any more ideas at all?

I feel like the problem is to do with the $query values and I'm just not
setting them correcly with the new piece of code, or not in the right
order.

To add to this, I've just noticed that when I load the landscape gallery,
not only does it display all images now (albeit in a paginated format)
instead of just the landscape images, but if I click the paginated result
for page 2, it changes the page title back to just Gallery, instead of
the main page of Landscape gallery.

This looks like it's more involved than I thought it would be! :-(

Again, if you or anyone else spot anything else obvious, please let me
know.

Thanks,

Stephen


Stephen,

Sounds like you need to study the code and understand it a little more.

I've never found it productive to change code (or even implement someone
else's code as part of mine) until I understood just what it was doing.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Hi Jerry,

I am doing this too. I've got PHP and My SQL for Dummies, and PHP and MySQL
Web Development. Although I know HTML inside out, I'm finding PHP very
though going.

By the time I fully understand the code, it could be months and months down
the line. I seem quite close with my limited understanding and help from
the group, so was just wondering if there was anything quick and obvious
that someone could spot that would make it all work.

I'll keep plugging away at it, though I'm still open to suggestions! ;-)

Thanks,

Stephen
May 6 '06 #5
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:mM******************************@comcast.com. ..
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:SN******************************@comcast.c om...
<snip>

Howdy.

One thing I see - you use $where before you set it. What happens if you
move the code which sets $where earlier in your routine?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Hi Jerry,

When I moved the code which set's $where to above the first $where, I
found that although gallery.php worked fine,
gallery.php?category=landscape failed to run and I got an error message.

Do you have any more ideas at all?

I feel like the problem is to do with the $query values and I'm just not
setting them correcly with the new piece of code, or not in the right
order.

To add to this, I've just noticed that when I load the landscape gallery,
not only does it display all images now (albeit in a paginated format)
instead of just the landscape images, but if I click the paginated result
for page 2, it changes the page title back to just Gallery, instead of
the main page of Landscape gallery.

This looks like it's more involved than I thought it would be! :-(

Again, if you or anyone else spot anything else obvious, please let me
know.

Thanks,

Stephen


Stephen,

Sounds like you need to study the code and understand it a little more.

I've never found it productive to change code (or even implement someone
else's code as part of mine) until I understood just what it was doing.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

I am doing this too. I've got PHP and My SQL for Dummies, and PHP and MySQL
Web Development. Although I know HTML inside out, I'm finding PHP very
though going.

By the time I fully understand the code, it could be months and months down
the line. I seem quite close with my limited understanding and help from
the group, so was just wondering if there was anything quick and obvious
that someone could spot that would make it all work.

I'll keep plugging away at it, though I'm still open to suggestions! ;-)

Thanks,

Stephen


Stephen,

Not really at this point. Obvious problems are resolved, but anyone would have
to look through a lot more code to understand your latest problems.

Welcome to the world of programming! :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #6
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:QP********************@comcast.com...
<snip>

Stephen,

Not really at this point. Obvious problems are resolved, but anyone would
have to look through a lot more code to understand your latest problems.

Welcome to the world of programming! :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Hi Jerry,

Indeed! :-)

By the way, I've been writing the HTML / PHP etc using Notepad++ for the
syntax highlighting. I use Altova XML Spy Home Edition when writing simple
XML scripts, and it has the added benefit of validating the XML and pointing
out errors against the DTD.

Is there such a free program for writing PHP that has a good debugging mode
or validation?

Thanks,

Stephen
May 6 '06 #7
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:QP********************@comcast.com...
<snip>
Stephen,

Not really at this point. Obvious problems are resolved, but anyone would
have to look through a lot more code to understand your latest problems.

Welcome to the world of programming! :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Hi Jerry,

Indeed! :-)

By the way, I've been writing the HTML / PHP etc using Notepad++ for the
syntax highlighting. I use Altova XML Spy Home Edition when writing simple
XML scripts, and it has the added benefit of validating the XML and pointing
out errors against the DTD.

Is there such a free program for writing PHP that has a good debugging mode
or validation?

Thanks,

Stephen


Stephen,

Sorry, I don't know about free - I don't use them. I do know Zend Studio, while
not free, is excellent, though. Well worth the price, IMHO. And if I'm not
using a debugger, I've found strategically placed echo statements do wonders for
debugging.

Disclaimer: And no, I have no relationship with the other than having used their
software. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #8
ste

"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:TI********************@comcast.com...
<snip>
Stephen,

Sorry, I don't know about free - I don't use them. I do know Zend Studio,
while not free, is excellent, though. Well worth the price, IMHO. And if
I'm not using a debugger, I've found strategically placed echo statements
do wonders for debugging.

Disclaimer: And no, I have no relationship with the other than having used
their software. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================


Thanks Jerry, I'll check it out.

So you won't even get a commission if I mention your name? ;-)

Stephen
May 6 '06 #9
Rik
ste wrote:
<?php
include("databasepasswords");
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];
Tssk, if thought I told you to escape GET variables if used in queries :-).
$pageno = mysql_real_escape_string(_GET['pageno'],$connection);
$query = "SELECT count(*) FROM images ".$where;
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);
As said, $where whould be set BEFORE this query.
$query = "SELECT * FROM images ORDER BY dateadded,
datetaken DESC $limit ".$where ;


Echo this query and you'll see what's wrong. It should be

$query = "SELECT * FROM images ORDER BY `dateadded`, `datetaken` DESC $where
$limit";

Wether your code creates proper pages I'm not going to check.

For future reference:
Echoing variables after they are set or print_r() arrays will make a lot
more clear to you in debugging.
In this instance, echoing the query would have told you right away the query
was composed in the wrong order.

Grtz,
--
Rik Wasmus
May 6 '06 #10
ste wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:TI********************@comcast.com...
<snip>
Stephen,

Sorry, I don't know about free - I don't use them. I do know Zend Studio,
while not free, is excellent, though. Well worth the price, IMHO. And if
I'm not using a debugger, I've found strategically placed echo statements
do wonders for debugging.

Disclaimer: And no, I have no relationship with the other than having used
their software. :-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Thanks Jerry, I'll check it out.

So you won't even get a commission if I mention your name? ;-)

Stephen


No, but they might refuse to sell it to you for any price! :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 6 '06 #11
ste

"Rik" <lu************@hotmail.com> wrote in message
news:e3**********@netlx020.civ.utwente.nl...
ste wrote:
<?php
include("databasepasswords");
if (isset($_GET['pageno'])) {
$pageno = $_GET['pageno'];


Tssk, if thought I told you to escape GET variables if used in queries
:-).
$pageno = mysql_real_escape_string(_GET['pageno'],$connection);
$query = "SELECT count(*) FROM images ".$where;
$result = mysql_query($query) or trigger_error("SQL", E_USER_ERROR);


As said, $where whould be set BEFORE this query.
$query = "SELECT * FROM images ORDER BY dateadded,
datetaken DESC $limit ".$where ;


Echo this query and you'll see what's wrong. It should be

$query = "SELECT * FROM images ORDER BY `dateadded`, `datetaken` DESC
$where
$limit";

Wether your code creates proper pages I'm not going to check.

For future reference:
Echoing variables after they are set or print_r() arrays will make a lot
more clear to you in debugging.
In this instance, echoing the query would have told you right away the
query
was composed in the wrong order.

Grtz,
--
Rik Wasmus

Hi Rik,

I echo'd the actual query to see what was failing, then I tried the query in
question in MyPHPAdmin to see if it worked there, and it didn't of course.

After a bit of trial and error, I have changed the query from:

SELECT *
FROM images
ORDER BY dateadded, datetaken DESC
LIMIT 0,9
WHERE category = 'Abstract'

To:

SELECT *
FROM images
WHERE category = 'Abstract'
ORDER BY dateadded, datetaken
LIMIT 0 , 9

....and this now works! :-)

Thanks again,

Ste
May 11 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Chris H | last post: by
3 posts views Thread by assgar | last post: by
3 posts views Thread by ishkur88 | last post: by
16 posts views Thread by gnawz | last post: by
2 posts views Thread by kkshansid | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.