This is a discussion on Photo Gallery Display question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; All, I'm putting the final touches on a MySQL/PHP driven photo gallery I have built, and am down ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
All,
I'm putting the final touches on a MySQL/PHP driven photo gallery I have built, and am down to one last task. I need to be able to display thumbnails from the gallery in rows of 5 on the main gallery page. I've found many scripts out there that do this, but can't find one that works when the photo data (fileName in this case) is coming from a MySQL DB where I need to loop through values in a set of records. Does anyone have a script for doing this? Or maybe can throw me the algorithm I'll need to use? Thank you in advance. |
|
|||
|
echo "<table>\n";
$picnum = 0; $maxpics = 25; while (($record = mysql_fetch_assoc($result)) && ($picnum < $maxpics)) { if (($picnum / 5) == floor($picnum / 5)) { echo "<tr>\n"; } echo "<td>\n"; // GENERATE THUMBNAIL HERE echo "</td>\n"; if (($picnum / 5) == floor($picnum / 5)) { echo "/<tr>\n"; } $picnum += 1; } echo "</table>\n"; "Jon" <jonra@netins.com> wrote in message news:dn9g8n$drg$1@news.netins.net... > All, > > I'm putting the final touches on a MySQL/PHP driven photo gallery I have > built, and am down to one last task. I need to be able to display thumbnails > from the gallery in rows of 5 on the main gallery page. I've found many > scripts out there that do this, but can't find one that works when the photo > data (fileName in this case) is coming from a MySQL DB where I need to loop > through values in a set of records. Does anyone have a script for doing > this? Or maybe can throw me the algorithm I'll need to use? Thank you in > advance. > > |
|
|||
|
I ended up with a slightly different solution:
<table><tr> <?php $counter = 1; while($row = mysql_fetch_array($resultGetPhotos)){ echo "<td>fileFromDB</td>"; if($counter % 5 == 0){ echo "</tr><tr>"; } $counter++; $i++; } ?> </tr></table> Seems to work. Thank you for the help either way. "Connector5" <junkmilenko@charter.net> wrote in message news:_vYlf.2736$Xx3.1801@fe03.lga... > echo "<table>\n"; > > $picnum = 0; > $maxpics = 25; > > while (($record = mysql_fetch_assoc($result)) && ($picnum < $maxpics)) > { > if (($picnum / 5) == floor($picnum / 5)) > { > echo "<tr>\n"; > } > > echo "<td>\n"; > > // GENERATE THUMBNAIL HERE > > echo "</td>\n"; > > if (($picnum / 5) == floor($picnum / 5)) > { > echo "/<tr>\n"; > } > > $picnum += 1; > } > > echo "</table>\n"; > > > "Jon" <jonra@netins.com> wrote in message > news:dn9g8n$drg$1@news.netins.net... >> All, >> >> I'm putting the final touches on a MySQL/PHP driven photo gallery I have >> built, and am down to one last task. I need to be able to display > thumbnails >> from the gallery in rows of 5 on the main gallery page. I've found many >> scripts out there that do this, but can't find one that works when the > photo >> data (fileName in this case) is coming from a MySQL DB where I need to > loop >> through values in a set of records. Does anyone have a script for doing >> this? Or maybe can throw me the algorithm I'll need to use? Thank you in >> advance. >> >> > > |
|
|||
|
> <table><tr>
> <?php > $counter = 1; > while($row = mysql_fetch_array($resultGetPhotos)){ > echo "<td>fileFromDB</td>"; > if($counter % 5 == 0){ > echo "</tr><tr>"; > } > $counter++; > $i++; What's that $i for? > } > ?> > </tr></table> Check the HTML that gets generated by your code when there's 5, 10, 15 etc. photos - you'll get extra "<tr></tr>" in those cases. Try this: <table> <?php $counter = 0; while($row = mysql_fetch_array($resultGetPhotos)) { if ($counter % 5 == 0) echo "<tr>\n"; echo "<td>fileFromDB</td>\n"; $counter++; if ($counter % 5 == 0) echo "</tr>\n"; } ?> </table> Hilarion |
|
|||
|
$i is actually a counter for another function of the page - I'm passing it
within a link for the actual photo display page. Thanks for the info on the table row issue, I hadn't thought of that. Also - if I start $counter at 0, won't the first pass automatically ring true and start a new row each time? "Hilarion" <hilarion@SPAM.op.SMIECI.pl> wrote in message news:dn9rco$j0m$1@news.onet.pl... >> <table><tr> >> <?php >> $counter = 1; >> while($row = mysql_fetch_array($resultGetPhotos)){ >> echo "<td>fileFromDB</td>"; >> if($counter % 5 == 0){ >> echo "</tr><tr>"; >> } >> $counter++; >> $i++; > > What's that $i for? > >> } >> ?> >> </tr></table> > > > Check the HTML that gets generated by your code when there's > 5, 10, 15 etc. photos - you'll get extra "<tr></tr>" in > those cases. > > Try this: > > <table> > <?php > $counter = 0; > while($row = mysql_fetch_array($resultGetPhotos)) > { > if ($counter % 5 == 0) > echo "<tr>\n"; > echo "<td>fileFromDB</td>\n"; > $counter++; > if ($counter % 5 == 0) > echo "</tr>\n"; > } > ?> > </table> > > > Hilarion |
|
|||
|
[Message reformatted.]
>>> <table><tr> >>> <?php >>> $counter = 1; >>> while($row = mysql_fetch_array($resultGetPhotos)){ >>> echo "<td>fileFromDB</td>"; >>> if($counter % 5 == 0){ >>> echo "</tr><tr>"; >>> } >>> $counter++; >>> $i++; >> What's that $i for? > $i is actually a counter for another function of the page - I'm passing it > within a link for the actual photo display page. >>> } >>> ?> >>> </tr></table> >> Check the HTML that gets generated by your code when there's >> 5, 10, 15 etc. photos - you'll get extra "<tr></tr>" in >> those cases. > Thanks for the info on the table row issue, I hadn't thought of that. No problem. >> Try this: >> >> <table> >> <?php >> $counter = 0; >> while($row = mysql_fetch_array($resultGetPhotos)) >> { >> if ($counter % 5 == 0) >> echo "<tr>\n"; >> echo "<td>fileFromDB</td>\n"; >> $counter++; >> if ($counter % 5 == 0) >> echo "</tr>\n"; >> } >> ?> >> </table> > Also - if I start $counter at 0, won't the first pass automatically ring > true and start a new row each time? Yes. Notice that there's no "<tr>" after "<table>", so triggering at the first pass is a correct behavior. There's also one more thing that comes to my mind... What if the count of records returned by the query is not multiplication of 5? The "<tr>" will not be closed and this way the HTML will be invalid (in your previous code this would not happen). My solution for this would be: <table> <?php $counter = 0; while($row = mysql_fetch_array($resultGetPhotos)) { if ($counter % 5 == 0) echo "<tr>\n"; echo "<td>fileFromDB</td>\n"; $counter++; if ($counter % 5 == 0) echo "</tr>\n"; } /// <added_code> if ($counter % 5 != 0) { // fill missing cells for( ; $counter % 5 != 0; $counter++ ) echo "<td></td>\n"; // add missing row closing tag echo "</tr>\n"; } /// </added_code> ?> </table> Ths should render valid HTML for any non-zero amount of results. If it's possible that there'll be no results, then no <table> should be rendered. Consider NOT using <table> at all, but wrap each result in <div> or <span> tags (each with same width and height). They will fill available width and wrap when needed. Example: <html> <head> <style type="text/css"> div.images span { width: 120px; height: 250px; padding: 5px; text-align: center; } </style> </head> <body> <div class="images"> <?php $pattern = <<<EOD <span> <a href="%1\$s"><img src="%2\$s" width="100" height="100" alt="%3\$s" /></a><br /> <a href="%1\$s">%4\$s</a> </span> EOD; // Connect and query here. while($row = mysql_fetch_array($resultGetPhotos)) { printf( $pattern, htmlspecialchars( $row['image_path'] ), htmlspecialchars( $row['image_thumbnail_path'] ), htmlspecialchars( $row['image_name'] ), htmlspecialchars( $row['image_description'] ), ); } ?> </div> </body> </html> The flaw of this solution is that you have to know how much text can there be as the image description and adjust height of the <span> tag so that the text fits inside. With <table> design you do not have this problem (and also when the texts in some specific row of the table are shorter, then this specific row will be rendered with lower height and this way conserve page height). With texts of same average lenght the <span> solution gives you flexible page width. Hilarion |