943,674 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 713
  • PHP RSS
Apr 3rd, 2009
0

Php Gallery

Expand Post »
I'm trying to develop a web gallery, but have been struggling with laying out the thumbnails, and paginating the results. I found a tutorial, but have been experiencing problems with it. Can anyone point me in the right direction?

Current result: http://www.equinefocus.co.uk//photos...21/gallery.php

Current code:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // Connects to your Database
  4. mysql_connect("*****", "*****", "*****") or die(mysql_error());
  5. mysql_select_db("*****")
  6. or die ("Unable to connect to MySQL");
  7.  
  8. // How many pictures to display on each page.
  9. $picsperpage = 12;
  10.  
  11. // How many pictures to display per row.
  12. $picsperrow = 4;
  13.  
  14. // images
  15. $thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
  16. $url ='/photos/'.$info['date'].'/'.$info['url'];
  17.  
  18. // If the page number is set...
  19. if(isset($_GET['p'])) {
  20.  
  21. // $page is now the page number.
  22. $page = $_GET['p'];
  23. } else {
  24.  
  25. // Otherwise the page is 1.
  26. $page = 1;
  27. }
  28.  
  29. // What the starting number will be for the query.
  30. $start = ($page - 1) * $picsperpage;
  31.  
  32. // Counts all of the rows in the table.
  33. $res= @mysql_query("SELECT COUNT(id) AS numrows FROM photos")
  34. or die("Sorry, but there was an error retrieving the gallery.");
  35.  
  36. // Gets the number of rows from the array.
  37. $fetchres = @mysql_fetch_array($res, MYSQL_ASSOC) or die("Sorry, but there was an error retrieving the
  38. gallery.");
  39. $numrows = $fetchres['numrows'];
  40.  
  41. // How many pages there will be (total pictures / # of pictures per page).
  42. $totalpages = ceil($numrows/$picsperpage);
  43. echo '<ul>';
  44.  
  45. // Echos our page numbers so that we can navigate.
  46. for($i=1; $i <= $totalpages; $i++) {
  47. if($i == $page) {
  48.  
  49. // If it's the current page, don't make a link since we are already there.
  50. echo '<li>' . $i . '</i>';
  51. } else {
  52.  
  53. // Make all other pages links.
  54. echo '<li><a href="main.php?p=' . $i . '">' . $i . '</a></li>';
  55. }
  56. }
  57. echo '</ul><br />
  58.  
  59. <div class="gallery">';
  60.  
  61. // Get all of the pictures for the current page.
  62. $allphotos = @mysql_query("SELECT * FROM photos LIMIT $start, $picsperpage")
  63. or die("Sorry, but there was an error retrieving the gallery.");
  64.  
  65. // How many pictures there are right now.
  66. $rowcount = 0;
  67.  
  68. // For each row in the gallery table...
  69. while ($row = @mysql_fetch_array($allphotos)) {
  70.  
  71. // If $rowcount divided by $picsperrow has a remainder of 0...
  72. if (($rowcount % $picsperrow) == 0) {
  73.  
  74. // Make a <br /> tag with a clear both style, so that there is a new row of images
  75. echo '<br style="clear: both;" />';
  76. }
  77.  
  78. // Displaying each image with this code.
  79. echo '<a href="' . $row['thumb']. '"><img src="' . $row['url']. '" /></a>';
  80.  
  81. // $rowcount + 1.
  82. $rowcount++;
  83. }
  84.  
  85. // After all the images are done, end the div.
  86. echo '</div>';
  87. ?>

Attached is a screenshot of my current database table.
Attached Thumbnails
Click image for larger version

Name:	table.jpg
Views:	14
Size:	113.8 KB
ID:	9656  
Similar Threads
Reputation Points: 11
Solved Threads: 1
Light Poster
Borderline is offline Offline
49 posts
since Apr 2008
Apr 4th, 2009
1

Re: Php Gallery

Looking at your code, I think you have your variables in a mix:
php Syntax (Toggle Plain Text)
  1. echo '<a href="' . $row['thumb']. '"><img src="' . $row['url']. '" /></a>';
Would output (for row 1)
html Syntax (Toggle Plain Text)
  1. <a href="001.jpg"><img src="001.php" /></a>

Swap over the variables so that the thumb is in the img tag and the url is in the a tag.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Apr 5th, 2009
0

Re: Php Gallery

Excellent, thanks. Unfortunately, it hasn't made any significant change to the result I'm getting.
Reputation Points: 11
Solved Threads: 1
Light Poster
Borderline is offline Offline
49 posts
since Apr 2008
Apr 5th, 2009
0

Re: Php Gallery

Can you post the rest of your script?

I have setup a table on my database and a slightly modified script on the server and it works for me.

Here's the edited code
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Connects to your Database
  3. mysql_connect("***", "***", "***")or die(mysql_error());
  4. mysql_select_db("***")or die ("Unable to connect to MySQL");
  5.  
  6. // How many pictures to display on each page.
  7. $picsperpage = 12;
  8.  
  9. // How many pictures to display per row.
  10. $picsperrow = 4;
  11.  
  12. // images
  13. $thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
  14. $url ='/photos/'.$info['date'].'/'.$info['url'];
  15.  
  16. // If the page number is set...
  17. if(isset($_GET['p'])) {
  18. // $page is now the page number.
  19. $page = $_GET['p'];
  20. } else {
  21. // Otherwise the page is 1.
  22. $page = 1;
  23. }
  24.  
  25. // What the starting number will be for the query.
  26. $start = ($page - 1) * $picsperpage;
  27.  
  28. // Counts all of the rows in the table.
  29. $res= @mysql_query("SELECT COUNT(id) AS numrows FROM photos")or die("Sorry, but there was an error retrieving the gallery.");
  30.  
  31. // Gets the number of rows from the array.
  32. $fetchres = @mysql_fetch_array($res, MYSQL_ASSOC)or die("Sorry, but there was an error retrieving the
  33. gallery.");
  34. $numrows = $fetchres['numrows'];
  35.  
  36. // How many pages there will be (total pictures / # of pictures per page).
  37. $totalpages = ceil($numrows/$picsperpage);
  38. echo '<ul>';
  39.  
  40. // Echos our page numbers so that we can navigate.
  41. for($i=1; $i <= $totalpages; $i++) {
  42. if($i == $page) {
  43. // If it's the current page, don't make a link since we are already there.
  44. echo '<li>' . $i . '</i>';
  45. } else {
  46. // Make all other pages links.
  47. echo '<li><a href="main.php?p=' . $i . '">' . $i . '</a></li>';
  48. }
  49. }
  50. echo '</ul><br />
  51. <div class="gallery">';
  52.  
  53. // Get all of the pictures for the current page.
  54. $allphotos = @mysql_query("SELECT * FROM photos LIMIT $start, $picsperpage")or die("Sorry, but there was an error retrieving the gallery.");
  55.  
  56. // How many pictures there are right now.
  57. $rowcount = 0;
  58.  
  59. // For each row in the gallery table...
  60. while ($row = @mysql_fetch_array($allphotos)) {
  61. // If $rowcount divided by $picsperrow has a remainder of 0...
  62. if (($rowcount % $picsperrow) == 0) {
  63. // Make a <br /> tag with a clear both style, so that there is a new row of images
  64. echo '<br style="clear: both;" />';
  65. }
  66. // Displaying each image with this code.
  67. echo '<a href="' . $row['url'] . '"><img src="' . $row['thumb'] . '" /></a>';
  68.  
  69. // $rowcount + 1.
  70. $rowcount++;
  71. }
  72.  
  73. // After all the images are done, end the div.
  74. echo '</div>';
  75. ?>

See it here: clicky
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008
Apr 5th, 2009
0

Re: Php Gallery

Thanks, appreciate the trouble you're going to.

I've looked at your link, and attach a screenshot of how I see it. The code in it's entirety is below.

PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
  4.  
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  7. <title>Equine Focus</title>
  8. </head>
  9.  
  10. <body>
  11.  
  12. <?php
  13.  
  14. // Connects to your Database
  15. mysql_connect("*****", "*****", "*****") or die(mysql_error());
  16. mysql_select_db("*****")
  17. or die ("Unable to connect to MySQL");
  18.  
  19. // How many pictures to display on each page.
  20. $picsperpage = 12;
  21.  
  22. // How many pictures to display per row.
  23. $picsperrow = 3;
  24.  
  25. // images
  26. $thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
  27. $url ='/photos/'.$info['date'].'/'.$info['url'];
  28.  
  29. // If the page number is set...
  30. if(isset($_GET['p'])) {
  31.  
  32. // $page is now the page number.
  33. $page = $_GET['p'];
  34. } else {
  35.  
  36. // Otherwise the page is 1.
  37. $page = 1;
  38. }
  39.  
  40. // What the starting number will be for the query.
  41. $start = ($page - 1) * $picsperpage;
  42.  
  43. // Counts all of the rows in the table.
  44. $res= @mysql_query("SELECT COUNT(id) AS numrows FROM photos")
  45. or die("Sorry, but there was an error retrieving the gallery.");
  46.  
  47. // Gets the number of rows from the array.
  48. $fetchres = @mysql_fetch_array($res, MYSQL_ASSOC) or die("Sorry, but there was an error retrieving the
  49. gallery.");
  50. $numrows = $fetchres['numrows'];
  51.  
  52. // How many pages there will be (total pictures / # of pictures per page).
  53. $totalpages = ceil($numrows/$picsperpage);
  54. echo '<ul>';
  55.  
  56. // Echos our page numbers so that we can navigate.
  57. for($i=1; $i <= $totalpages; $i++) {
  58. if($i == $page) {
  59.  
  60. // If it's the current page, don't make a link since we are already there.
  61. echo '<li>' . $i . '</i>';
  62. } else {
  63.  
  64. // Make all other pages links.
  65. echo '<li><a href="main.php?p=' . $i . '">' . $i . '</a></li>';
  66. }
  67. }
  68. echo '</ul><br />
  69.  
  70. <div class="gallery">';
  71.  
  72. // Get all of the pictures for the current page.
  73. $allphotos = @mysql_query("SELECT * FROM photos LIMIT $start, $picsperpage")
  74. or die("Sorry, but there was an error retrieving the gallery.");
  75.  
  76. // How many pictures there are right now.
  77. $rowcount = 0;
  78.  
  79. // For each row in the gallery table...
  80. while ($row = @mysql_fetch_array($allphotos)) {
  81.  
  82. // If $rowcount divided by $picsperrow has a remainder of 0...
  83. if (($rowcount % $picsperrow) == 0) {
  84.  
  85. // Make a <br /> tag with a clear both style, so that there is a new row of images
  86. echo '<br style="clear: both;" />';
  87. }
  88.  
  89. // Displaying each image with this code.
  90. echo '<a href="' . $url. '"><img src="' . $thumb. '" /></a>';
  91.  
  92. // $rowcount + 1.
  93. $rowcount++;
  94. }
  95.  
  96. // After all the images are done, end the div.
  97. echo '</div>';
  98. ?>

I've been playing all afternoon, and have almost given up - I've reproduced the gallery with CSS to position the items - out of interest, is it possible to paginate results without use of a table, just stipulate how many thumbnails are to appear on each page?

Look forward to hearing your advice.
Attached Thumbnails
Click image for larger version

Name:	timetest.jpg
Views:	10
Size:	66.7 KB
ID:	9681  
Reputation Points: 11
Solved Threads: 1
Light Poster
Borderline is offline Offline
49 posts
since Apr 2008
Apr 5th, 2009
0

Re: Php Gallery

Click to Expand / Collapse  Quote originally posted by xan ...
Can you post the rest of your script?

I have setup a table on my database and a slightly modified script on the server and it works for me.

Here's the edited code
php Syntax (Toggle Plain Text)
  1. <?php
  2. // Connects to your Database
  3. mysql_connect("***", "***", "***")or die(mysql_error());
  4. mysql_select_db("***")or die ("Unable to connect to MySQL");
  5.  
  6. // How many pictures to display on each page.
  7. $picsperpage = 12;
  8.  
  9. // How many pictures to display per row.
  10. $picsperrow = 4;
  11.  
  12. // images
  13. $thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
  14. $url ='/photos/'.$info['date'].'/'.$info['url'];
  15.  
  16. // If the page number is set...
  17. if(isset($_GET['p'])) {
  18. // $page is now the page number.
  19. $page = $_GET['p'];
  20. } else {
  21. // Otherwise the page is 1.
  22. $page = 1;
  23. }
  24.  
  25. // What the starting number will be for the query.
  26. $start = ($page - 1) * $picsperpage;
  27.  
  28. // Counts all of the rows in the table.
  29. $res= @mysql_query("SELECT COUNT(id) AS numrows FROM photos")or die("Sorry, but there was an error retrieving the gallery.");
  30.  
  31. // Gets the number of rows from the array.
  32. $fetchres = @mysql_fetch_array($res, MYSQL_ASSOC)or die("Sorry, but there was an error retrieving the
  33. gallery.");
  34. $numrows = $fetchres['numrows'];
  35.  
  36. // How many pages there will be (total pictures / # of pictures per page).
  37. $totalpages = ceil($numrows/$picsperpage);
  38. echo '<ul>';
  39.  
  40. // Echos our page numbers so that we can navigate.
  41. for($i=1; $i <= $totalpages; $i++) {
  42. if($i == $page) {
  43. // If it's the current page, don't make a link since we are already there.
  44. echo '<li>' . $i . '</i>';
  45. } else {
  46. // Make all other pages links.
  47. echo '<li><a href="main.php?p=' . $i . '">' . $i . '</a></li>';
  48. }
  49. }
  50. echo '</ul><br />
  51. <div class="gallery">';
  52.  
  53. // Get all of the pictures for the current page.
  54. $allphotos = @mysql_query("SELECT * FROM photos LIMIT $start, $picsperpage")or die("Sorry, but there was an error retrieving the gallery.");
  55.  
  56. // How many pictures there are right now.
  57. $rowcount = 0;
  58.  
  59. // For each row in the gallery table...
  60. while ($row = @mysql_fetch_array($allphotos)) {
  61. // If $rowcount divided by $picsperrow has a remainder of 0...
  62. if (($rowcount % $picsperrow) == 0) {
  63. // Make a <br /> tag with a clear both style, so that there is a new row of images
  64. echo '<br style="clear: both;" />';
  65. }
  66. // Displaying each image with this code.
  67. echo '<a href="' . $row['url'] . '"><img src="' . $row['thumb'] . '" /></a>';
  68.  
  69. // $rowcount + 1.
  70. $rowcount++;
  71. }
  72.  
  73. // After all the images are done, end the div.
  74. echo '</div>';
  75. ?>

See it here: clicky
as far as my tiny knowledge is concern I think your code is not working. You just display 2 pictures but in your code
#
// How many pictures to display on each page.
#
$picsperpage = 12;
#
// How many pictures to display per row.
#
$picsperrow = 4;
should be 12 and 4 per rows

may I request to "Borderline" can you dump your SQL file.
can you post also your <div class="gallery">'; on your CSS.
Last edited by rm_daniweb; Apr 5th, 2009 at 6:07 pm.
Reputation Points: 13
Solved Threads: 12
Junior Poster
rm_daniweb is offline Offline
165 posts
since Jan 2007
Apr 5th, 2009
0

Re: Php Gallery

http://www.equinefocus.co.uk//photos...21/gallery.php

why is it there are 2 double '//' on your link?

I think you have errors on your path...
Last edited by rm_daniweb; Apr 5th, 2009 at 6:23 pm.
Reputation Points: 13
Solved Threads: 12
Junior Poster
rm_daniweb is offline Offline
165 posts
since Jan 2007
Apr 5th, 2009
0

Re: Php Gallery

I think I see the problem:

You have this at the top of the script:
php Syntax (Toggle Plain Text)
  1. // images
  2. $thumb ='/photos/'.$info['date'].'/thumbs/'.$info['thumb'];
  3. $url ='/photos/'.$info['date'].'/'.$info['url'];

But at that point $info has not been declared/defined, you can confirm this by looking at your source on the actual page, it is missing the values so you end up with the image src being '/photos//thumbs/' for every image.

This should be within the while statement after the SQL query, so change:
php Syntax (Toggle Plain Text)
  1. // Displaying each image with this code.
  2. echo '<a href="' . $url. '"><img src="' . $thumb. '" /></a>';
To
php Syntax (Toggle Plain Text)
  1. // Image Declarations
  2. $thumb ="/photos/" . $row['date'] . "/thumbs/" . $row['thumb'];
  3. $url ="/photos/" . $row['date'] . "/" . $row['url'];
  4. // Displaying each image with this code.
  5. echo '<a href="' . $url. '"><img src="' . $thumb. '" /></a>';

That should work.
Last edited by Will Gresham; Apr 5th, 2009 at 7:02 pm.
Reputation Points: 96
Solved Threads: 124
Master Poster
Will Gresham is offline Offline
728 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Subdomain Drupal
Next Thread in PHP Forum Timeline: Calling PHP function Onchange on Javascript





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC