mhnewmedia Posted February 28, 2006 Share Posted February 28, 2006 I have written a simple script that acts as a very basic image gallery that basically just reads what files are contained within a directory and displays them as clickable thumbnails which when rolled over display a full size image.[code] <?phpwhile($file = readdir($dh)){if($file != "." && $file != ".."){$FileName = $file;$NoJPG = str_replace(".jpg","",$FileName);$NoUS = str_replace("_"," ",$NoJPG);print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";$sep = ",\n";}}closedir($dh);?>[/code]At the moment the images are dispolyed in table cells that spread across the page horizontally, what i would like to be able to do is display the results in a four column table with as many rows as is needed.Any ideas?? Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/ Share on other sites More sharing options...
AV1611 Posted February 28, 2006 Share Posted February 28, 2006 you might need to tweek this a little, but it creats a new row after each forth picture...[code] <?php$i=1;while($file = readdir($dh)){if($file != "." && $file != ".."){$FileName = $file;$NoJPG = str_replace(".jpg","",$FileName);$NoUS = str_replace("_"," ",$NoJPG);if ($i==1){print "<tr>";}print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";if ($i==4){print "<\tr>"; $i=0; }$i++;$sep = ",\n";}}closedir($dh);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/#findComment-13033 Share on other sites More sharing options...
mhnewmedia Posted March 1, 2006 Author Share Posted March 1, 2006 That Works Like a dream, slight typo on the closing table row tag [code]if ($i==4){print "<\tr>"[/code] should be a forward slash [code]if ($i==4){print "</tr>";[/code]The only other issue is if the number of images is not divisible by 4 then the last table row does not have a closing </tr> tag, I was wondering if there is a way of checking if the number of files is divisible by four and if it isn't then printing a final </tr> Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/#findComment-13261 Share on other sites More sharing options...
AV1611 Posted March 1, 2006 Share Posted March 1, 2006 Yes, the way I do that is a series of clauses at the end.When you end the loop, do something like this: (this is concept, not hard code)[code]if ($i==1){echo "<td></td><td></td><td></td></tr></table>}if ($i==2){echo "<td></td><td></td></tr></table>}if ($i==3){echo "<td></td></tr></table>}if ($i==4){echo "</tr></table>}[/code]You get the idea...Let me know how it works out! Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/#findComment-13273 Share on other sites More sharing options...
mhnewmedia Posted March 1, 2006 Author Share Posted March 1, 2006 Same result but different method I have set two variables $rowstart and $rowend then when a row is started $rowstart is increased by 1 and when a row is ended $rowend is increased by one then at the end if $rowstart isn't equal to $rowend we print a </tr>I love PHP but sometimes you need a little poke in the right direction!![code]<?php$i=1;$rowstart = 0;$rowend = 0;while($file = readdir($dh)){if($file != "." && $file != ".."){$FileName = $file;$NoJPG = str_replace(".jpg","",$FileName);$NoUS = str_replace("_"," ",$NoJPG);if ($i==1){echo "<tr>"; $rowstart++;}print "<td><a href=\"javascript:;\" onmouseover=\"MM_swapImage('MainImage','','gallery/$file',1)\"><img src=\"gallery/thumbs/$file\" border=\"0\" alt=\"$NoUS\"></a></td>";if ($i==5){echo "</tr>"; $i=0; $rowend++; } $i++;$sep = ",\n";}}if ($rowstart != $rowend){echo "</tr>";}closedir($dh);?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/#findComment-13280 Share on other sites More sharing options...
AV1611 Posted March 2, 2006 Share Posted March 2, 2006 With PHP, there are literally a hundred way to skin a cat... You are limited only by your imagination... I LOVE PHP! Quote Link to comment https://forums.phpfreaks.com/topic/3738-displaying-results-in-a-table/#findComment-13707 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.