For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > December 2005 > More photo gallery help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author More photo gallery help
Jon

2005-12-12, 7:00 pm

Ok, I'm so close to being done with this application, but have run into
something I simply cannot seem to fix. Here's the deal:

I have a MySQL driven gallery page, that displays 15 images per page
(thumbnails, in rows of 5). If the page goes over 1, there's a next/previous
button with numbers of each page at the top.

So far so good. I also have a page that displays the full version of the
images - with a next/previous button to rotate through the images. As you
may have seen in my previous posts, I'm tracking where I'm at in the
recordset returned from the DB via an array storing each file name, so the
next button and previous button are all built using a variable counter of $i
so I know exactly where I'm at in the recordset from the DB. Each time you
click next, the query is run again, the array of file names is rebuilt, and
$i is incremented to the next value in the data returned.


Alright, here's where it gets ugly - I WAS passing $i from the actual
gallery display (the thumbnails) so when the user clicked on a thumb, it
would query the full list of photos and we'd always be at $i on the other
side, therefore we'd always know were we were at in the array of images.

The problem is now trying to pass $i from the multiple pages - I've tried
incrementing $i by 15 (the total number of photos per page), and though the
next button and previous buttons work, the numbered links do not work - in
particular when you go BACK a number - so if I'm at 4, and I click on page
3, $i is incremented wrong, and everything blows up.

Mainly, I'm wondering if anyone has built an application like this and has a
better algorithm for passing this data - If anyone has a clear idea on what
I'm needing, or thinks the code will help, I'll post any code you need (it's
quite a bit, so I figured I'd start with just a description of the problem).
Does anyone have some advice for me here on a better way to do this, or a
way to fix the algorithm I'm currently using? I'm desparate :(


Connector5

2005-12-12, 9:58 pm

Your image gallery search code should be reproducable, right?

I mean, if you know that you want page 3 you can instantly pull up page 3,
right? If not, you may want to rethink your schema.

When I am doing pagination, I use the sql offset commands to specify a
rowstart and a rowcount to return. That way, I only need to know that I was
on page 3 and I can set a returnto=search&page=3 schema.

MySQL: http://dev.mysql.com/doc/refman/5.0/en/select.html
PostgreSQL: http://www.postgresql.org/docs/7.4/...sql-select.html


PHP:


$offset = (($pictures_per_page * $page) - $page);

// Very good habit to get into with queries
$offset = (int) $offset; // For strings: $offset = (string)
mysql_escape_string($offset);
$page = (int) $page; // For strings: $page = (string)
mysql_escape_string($page);

/* MySQL */ $result = mysql_query("select * from `image_table` order by
`submit_date` desc limit '$offset','$pictures_per_page'");
/* PgSQL */ $result = pgsql_query("select * from \"image_table\" order by
\"submit_date\" desc limit '$pictures_per_page' offset '$offset'");

// Extra credit
/* MySQL */ if (mysql_num_rows($result) == 0) { /* Show Error Page or maybe
page 1? */ }
/* PgSQL */ if (pgsql_num_rows($result) == 0) { /* Show Error Page or maybe
page 1? */ }



NOTE: The offset formula sometimes will raise a derelict page. Like 2
pages when you only have 15 results, and the second page will be blank.
This is not consistently reproduceable, so you may want to query the last
page automatically and see if it has results, and if so, then leave out the
page number. Maybe someone else can offer a better solution to that.


"Jon" <jonra@netins.com> wrote in message
news:dnkp1g$um2$1@news.netins.net...
> Ok, I'm so close to being done with this application, but have run into
> something I simply cannot seem to fix. Here's the deal:
>
> I have a MySQL driven gallery page, that displays 15 images per page
> (thumbnails, in rows of 5). If the page goes over 1, there's a

next/previous
> button with numbers of each page at the top.
>
> So far so good. I also have a page that displays the full version of the
> images - with a next/previous button to rotate through the images. As you
> may have seen in my previous posts, I'm tracking where I'm at in the
> recordset returned from the DB via an array storing each file name, so the
> next button and previous button are all built using a variable counter of

$i
> so I know exactly where I'm at in the recordset from the DB. Each time you
> click next, the query is run again, the array of file names is rebuilt,

and
> $i is incremented to the next value in the data returned.
>
>
> Alright, here's where it gets ugly - I WAS passing $i from the actual
> gallery display (the thumbnails) so when the user clicked on a thumb, it
> would query the full list of photos and we'd always be at $i on the other
> side, therefore we'd always know were we were at in the array of images.
>
> The problem is now trying to pass $i from the multiple pages - I've tried
> incrementing $i by 15 (the total number of photos per page), and though

the
> next button and previous buttons work, the numbered links do not work - in
> particular when you go BACK a number - so if I'm at 4, and I click on page
> 3, $i is incremented wrong, and everything blows up.
>
> Mainly, I'm wondering if anyone has built an application like this and has

a
> better algorithm for passing this data - If anyone has a clear idea on

what
> I'm needing, or thinks the code will help, I'll post any code you need

(it's
> quite a bit, so I figured I'd start with just a description of the

problem).
> Does anyone have some advice for me here on a better way to do this, or a
> way to fix the algorithm I'm currently using? I'm desparate :(
>
>



Jon

2005-12-13, 6:57 pm

I'm using something close, but I'm still not sure how to get from the
numbered pages to the display page. Right now, I can click on page 3, and it
will send in a variable as a query string on that page link that's being
incremented by 15 (images per page), it really only breaks when I go BACK a
page - not as in clicking the back button, but if I go from page 4 to 3 for
example, everything blows up... Here's what my code looks like:

Gallery page code:

$eu = ($start - 0);
$limit = 15; // No of records to be shown per page.
$this = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;

$sqlGetPhotos = "SELECT * FROM tblPhotoGallery WHERE catID = $category limit
$eu, $limit";

Then for the links (pages and next/prev):

if($back >=0){
print "<td><a
href='$page_name?start=$back&catID=$catID&i=".($eu-15)."'>Back</a></td>";
}
if($n != $eu){
echo "<td><a
href='$page_name?start=$n&catID=$category&i=$z'>$l</a></td>";
}
if($this < $totalPhotos){
print "<td><a
href='$page_name?start=$next&catID=$category&i=".($eu+15)."'>Next</a></td>";
}

So, do I run an identical query then on the actual display page to know
which image they've clicked on? I want the images themselves to be links so
when they click one, it displays the full size image in a separate gallery
with next/previous links.

Code on display page:

//get $i from the gallery display so we know where we're at in the image
list
$i = $_GET[i];
if(!isset ($i)){
$i = 0;
}

$sql = "SELECT photoID, fileName, photoDesc FROM tblPhotoGallery WHERE catID
= $category AND active = 'Y'";

//This is loading the images into a separate array so I can track where
we're at in the list, and increment to each photoID (as we dont know what
the IDs will be)
while($photoList = mysql_fetch_array($dataFile)){
$nextPhoto[] = $photoList['photoID'];
$photo[] = $photoList['fileName'];
$caption[] = $photoList['photoDesc'];
}

Then the link:

<td width="137"><div align="center"><a href = "photoDisplay.php?i=<?php
echo($i); ?>&catID=<?= $category ?>&fileName=<?php
echo($photo[$i]);?>&start=<?= $start?>">Next</a></div></td>

So I always know where we're at in the recordset, as I'm running by $i -
photo[] stores the file names in order... Any idea how I can implement your
algorithm to still pass the correct photo from one page to another?

"Connector5" <junkmilenko@charter.net> wrote in message
news:Uhqnf.10807$Eu3.1686@fe07.lga...
> Your image gallery search code should be reproducable, right?
>
> I mean, if you know that you want page 3 you can instantly pull up page 3,
> right? If not, you may want to rethink your schema.
>
> When I am doing pagination, I use the sql offset commands to specify a
> rowstart and a rowcount to return. That way, I only need to know that I
> was
> on page 3 and I can set a returnto=search&page=3 schema.
>
> MySQL: http://dev.mysql.com/doc/refman/5.0/en/select.html
> PostgreSQL: http://www.postgresql.org/docs/7.4/...sql-select.html
>
>
> PHP:
>
>
> $offset = (($pictures_per_page * $page) - $page);
>
> // Very good habit to get into with queries
> $offset = (int) $offset; // For strings: $offset = (string)
> mysql_escape_string($offset);
> $page = (int) $page; // For strings: $page = (string)
> mysql_escape_string($page);
>
> /* MySQL */ $result = mysql_query("select * from `image_table` order by
> `submit_date` desc limit '$offset','$pictures_per_page'");
> /* PgSQL */ $result = pgsql_query("select * from \"image_table\" order
> by
> \"submit_date\" desc limit '$pictures_per_page' offset '$offset'");
>
> // Extra credit
> /* MySQL */ if (mysql_num_rows($result) == 0) { /* Show Error Page or
> maybe
> page 1? */ }
> /* PgSQL */ if (pgsql_num_rows($result) == 0) { /* Show Error Page or
> maybe
> page 1? */ }
>
>
>
> NOTE: The offset formula sometimes will raise a derelict page. Like 2
> pages when you only have 15 results, and the second page will be blank.
> This is not consistently reproduceable, so you may want to query the last
> page automatically and see if it has results, and if so, then leave out
> the
> page number. Maybe someone else can offer a better solution to that.
>
>
> "Jon" <jonra@netins.com> wrote in message
> news:dnkp1g$um2$1@news.netins.net...
> next/previous
> $i
> and
> the
> a
> what
> (it's
> problem).
>
>



Connector5

2005-12-13, 6:58 pm

Here is some modified code:
(Note that this is PHP5 code, and may need some slight adjustments for PHP4)

<?php

// Change these!
define('PAGE_LINK', 'view_images.php');
define('VIEWER_LINK', 'image.php');

// Change these if you want
define('LINK_GAP', '  ');
define('IMAGES_PER_PAGE', 15);



if (isset($_GET['category']))
{
$category = (string) $_GET['category'];
$category = "'" . mysql_escape_string($category) . "'";
}




// Get the value of $max_page:
$result = @mysql_fetch_assoc(mysql_query("select count(*) as max_page from
tblPhotoGallery WHERE catID = '$category'"));
$max_page = $result['max_page'];


if (isset($_GET['page']))
{
$page = (int) $_GET['page'];
$page = floor(max($page, 1));
$page = min($page, $max_page);
}
else
{
// Drop the default page down.
$page = (int) 1;
}



// Get the database limit and offset (we set $limit here for ease of coding
later)
$limit = IMAGES_PER_PAGE;
$offset = ($page * $limit) - $limit;





function make_links(int $current_page, int $max_page, bool $enum_pages =
true, int $enum_limit = 6)
{
if ($current_page == 1)
{
echo '<u style="color:grey;">&lt;&lt;</u>' . LINK_GAP;
echo '<u style="color:grey;">&lt; Back</u>' . LINK_GAP;
}
else
{
echo '<a href="'. PAGE_LINK .'?page='. (1) .'">&lt;&lt;</a>' . LINK_GAP;
echo '<a href="'. PAGE_LINK .'?page='. ($current_page - 1)
..'">&lt; Back</a>' . LINK_GAP;

}


if ($enum_pages)
{
$enum_limit = max(1, floor($enum_limit / 2));

for ($i = ($current_page - $enum_limit); $i <= ($current_page +
$enum_limit); $i += 1)
{
if (($i >= 1) && ($i <= $max_page))
{
if ($i == $current_page)
{
echo "<b><u>" . $i . "</u></b>" . LINK_GAP;
}
else
{
echo '<a href="'. PAGE_LINK .'?page='. ($i) .'">' . $i . '</a>' .
LINK_GAP;
}
}
}
}


if ($current_page == $max_page)
{
echo '<u style="color:grey;">Next &gt;</u>' . LINK_GAP;
echo '<u style="color:grey;">&gt;&gt;</u>';
}
else
{
echo '<a href="'. PAGE_LINK .'?page='. ($current_page + 1)
..'">Next &gt;</a>' . LINK_GAP;
echo '<a href="'. PAGE_LINK .'?page='. ($max_page) .'">&gt;&gt;</a>';
}
}

// The function above is a beautiful pagination link generator. You may
wish to extend it with a tooltip mechanism so you can label the last and
first links.







// Get the images
$photo_array_result = @mysql_query("SELECT * FROM tblPhotoGallery WHERE
catID = '$category' limit $offset,$limit");

// Premake the links.
while ($photo_array[] = @mysql_fetch_assoc($photo_array_result))

{
$link = VIEWER_LINK . "?returnto=view_images&page=" . $page;

$photo_array[count($photo_array) - 1]['generated_link'] = $link;
}



// Later on in your code

make_links($page, $max_page);

foreach ($photo_array as $num => $photo)
{
// Display the image here
echo '<pre>' . print_r($photo, true) . '</pre>';
}

?>




In your image viewer page (when you click on an image) don't worry about
changing any pointers. Just show the image and let the user click the back
button. HOWEVER, you should add a "Return to browse images" or something.
Place this code in your single-image viewer page:



if (isset($_GET['returnto']))
{
switch($_GET['returnto'])
{
case 'view_images':
define('RETURNTO_LINK', 'view_images.php');
break;

default:
define('RETURNTO_LINK', '');
break;
}
}
else
{
define('RETURNTO_LINK', '');
}


if (isset($_GET['page']))
{
$page = (int) $page;
$page = floor(max(1, $page));
}
else
{
$page = 1;
}





// Later on:
if (RETURNTO_LINK != '')
{
echo 'Return to the <a href="'. RETURNTO_LINK .'?page='. $page .'">Previous
Page</a>';
}








A robust pagination script needs only one input, and that is the page
number. With this information, you should be able to reproduce that page
verbatim. One thing to note is that your select query MUST HAVE an order_by
clause. It can be anything as long as it can only order the images one way.
Remember this!



Good luck :-)













"Jon" <jonra@netins.com> wrote in message
news:dnmns5$un1$1@news.netins.net...
> I'm using something close, but I'm still not sure how to get from the
> numbered pages to the display page. Right now, I can click on page 3, and

it
> will send in a variable as a query string on that page link that's being
> incremented by 15 (images per page), it really only breaks when I go BACK

a
> page - not as in clicking the back button, but if I go from page 4 to 3

for
> example, everything blows up... Here's what my code looks like:
>
> Gallery page code:
>
> $eu = ($start - 0);
> $limit = 15; // No of records to be shown per page.
> $this = $eu + $limit;
> $back = $eu - $limit;
> $next = $eu + $limit;
>
> $sqlGetPhotos = "SELECT * FROM tblPhotoGallery WHERE catID = $category

limit
> $eu, $limit";
>
> Then for the links (pages and next/prev):
>
> if($back >=0){
> print "<td><a
> href='$page_name?start=$back&catID=$catID&i=".($eu-15)."'>Back</a></td>";
> }
> if($n != $eu){
> echo "<td><a
> href='$page_name?start=$n&catID=$category&i=$z'>$l</a></td>";
> }
> if($this < $totalPhotos){
> print "<td><a
>

href='$page_name?start=$next&catID=$category&i=".($eu+15)."'>Next</a></td>";
> }
>
> So, do I run an identical query then on the actual display page to know
> which image they've clicked on? I want the images themselves to be links

so
> when they click one, it displays the full size image in a separate gallery
> with next/previous links.
>
> Code on display page:
>
> //get $i from the gallery display so we know where we're at in the image
> list
> $i = $_GET[i];
> if(!isset ($i)){
> $i = 0;
> }
>
> $sql = "SELECT photoID, fileName, photoDesc FROM tblPhotoGallery WHERE

catID
> = $category AND active = 'Y'";
>
> //This is loading the images into a separate array so I can track where
> we're at in the list, and increment to each photoID (as we dont know what
> the IDs will be)
> while($photoList = mysql_fetch_array($dataFile)){
> $nextPhoto[] = $photoList['photoID'];
> $photo[] = $photoList['fileName'];
> $caption[] = $photoList['photoDesc'];
> }
>
> Then the link:
>
> <td width="137"><div align="center"><a href = "photoDisplay.php?i=<?php
> echo($i); ?>&catID=<?= $category ?>&fileName=<?php
> echo($photo[$i]);?>&start=<?= $start?>">Next</a></div></td>
>
> So I always know where we're at in the recordset, as I'm running by $i -
> photo[] stores the file names in order... Any idea how I can implement

your
> algorithm to still pass the correct photo from one page to another?
>
> "Connector5" <junkmilenko@charter.net> wrote in message
> news:Uhqnf.10807$Eu3.1686@fe07.lga...
3,[color=darkred]
last[color=darkred]
the[color=darkred]
you[color=darkred]
of[color=darkred]
it[color=darkred]
other[color=darkred]
images.[color=darkred]
tried[color=darkred]
a[color=darkred]
>
>



Jon

2005-12-14, 6:58 pm

Thanks a ton for the help :)

"Connector5" <junkmilenko@charter.net> wrote in message
news:FlCnf.7840$ES.1292@fe05.lga...
> Here is some modified code:
> (Note that this is PHP5 code, and may need some slight adjustments for
> PHP4)
>
> <?php
>
> // Change these!
> define('PAGE_LINK', 'view_images.php');
> define('VIEWER_LINK', 'image.php');
>
> // Change these if you want
> define('LINK_GAP', '  ');
> define('IMAGES_PER_PAGE', 15);
>
>
>
> if (isset($_GET['category']))
> {
> $category = (string) $_GET['category'];
> $category = "'" . mysql_escape_string($category) . "'";
> }
>
>
>
>
> // Get the value of $max_page:
> $result = @mysql_fetch_assoc(mysql_query("select count(*) as max_page from
> tblPhotoGallery WHERE catID = '$category'"));
> $max_page = $result['max_page'];
>
>
> if (isset($_GET['page']))
> {
> $page = (int) $_GET['page'];
> $page = floor(max($page, 1));
> $page = min($page, $max_page);
> }
> else
> {
> // Drop the default page down.
> $page = (int) 1;
> }
>
>
>
> // Get the database limit and offset (we set $limit here for ease of
> coding
> later)
> $limit = IMAGES_PER_PAGE;
> $offset = ($page * $limit) - $limit;
>
>
>
>
>
> function make_links(int $current_page, int $max_page, bool $enum_pages =
> true, int $enum_limit = 6)
> {
> if ($current_page == 1)
> {
> echo '<u style="color:grey;">&lt;&lt;</u>' . LINK_GAP;
> echo '<u style="color:grey;">&lt; Back</u>' . LINK_GAP;
> }
> else
> {
> echo '<a href="'. PAGE_LINK .'?page='. (1) .'">&lt;&lt;</a>' . LINK_GAP;
> echo '<a href="'. PAGE_LINK .'?page='. ($current_page - 1)
> .'">&lt; Back</a>' . LINK_GAP;
>
> }
>
>
> if ($enum_pages)
> {
> $enum_limit = max(1, floor($enum_limit / 2));
>
> for ($i = ($current_page - $enum_limit); $i <= ($current_page +
> $enum_limit); $i += 1)
> {
> if (($i >= 1) && ($i <= $max_page))
> {
> if ($i == $current_page)
> {
> echo "<b><u>" . $i . "</u></b>" . LINK_GAP;
> }
> else
> {
> echo '<a href="'. PAGE_LINK .'?page='. ($i) .'">' . $i . '</a>' .
> LINK_GAP;
> }
> }
> }
> }
>
>
> if ($current_page == $max_page)
> {
> echo '<u style="color:grey;">Next &gt;</u>' . LINK_GAP;
> echo '<u style="color:grey;">&gt;&gt;</u>';
> }
> else
> {
> echo '<a href="'. PAGE_LINK .'?page='. ($current_page + 1)
> .'">Next &gt;</a>' . LINK_GAP;
> echo '<a href="'. PAGE_LINK .'?page='. ($max_page) .'">&gt;&gt;</a>';
> }
> }
>
> // The function above is a beautiful pagination link generator. You may
> wish to extend it with a tooltip mechanism so you can label the last and
> first links.
>
>
>
>
>
>
>
> // Get the images
> $photo_array_result = @mysql_query("SELECT * FROM tblPhotoGallery WHERE
> catID = '$category' limit $offset,$limit");
>
> // Premake the links.
> while ($photo_array[] = @mysql_fetch_assoc($photo_array_result))

> {
> $link = VIEWER_LINK . "?returnto=view_images&page=" . $page;
>
> $photo_array[count($photo_array) - 1]['generated_link'] = $link;
> }
>
>
>
> // Later on in your code
>
> make_links($page, $max_page);
>
> foreach ($photo_array as $num => $photo)
> {
> // Display the image here
> echo '<pre>' . print_r($photo, true) . '</pre>';
> }
>
> ?>
>
>
>
>
> In your image viewer page (when you click on an image) don't worry about
> changing any pointers. Just show the image and let the user click the
> back
> button. HOWEVER, you should add a "Return to browse images" or something.
> Place this code in your single-image viewer page:
>
>
>
> if (isset($_GET['returnto']))
> {
> switch($_GET['returnto'])
> {
> case 'view_images':
> define('RETURNTO_LINK', 'view_images.php');
> break;
>
> default:
> define('RETURNTO_LINK', '');
> break;
> }
> }
> else
> {
> define('RETURNTO_LINK', '');
> }
>
>
> if (isset($_GET['page']))
> {
> $page = (int) $page;
> $page = floor(max(1, $page));
> }
> else
> {
> $page = 1;
> }
>
>
>
>
>
> // Later on:
> if (RETURNTO_LINK != '')
> {
> echo 'Return to the <a href="'. RETURNTO_LINK .'?page='. $page
> .'">Previous
> Page</a>';
> }
>
>
>
>
>
>
>
>
> A robust pagination script needs only one input, and that is the page
> number. With this information, you should be able to reproduce that page
> verbatim. One thing to note is that your select query MUST HAVE an
> order_by
> clause. It can be anything as long as it can only order the images one
> way.
> Remember this!
>
>
>
> Good luck :-)
>
>
>
>
>
>
>
>
>
>
>
>
>
> "Jon" <jonra@netins.com> wrote in message
> news:dnmns5$un1$1@news.netins.net...
> it
> a
> for
> limit
> href='$page_name?start=$next&catID=$category&i=".($eu+15)."'>Next</a></td>";
> so
> catID
> your
> 3,
> last
> the
> you
> of
> it
> other
> images.
> tried
> a
>
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com