servvs Posted May 29, 2008 Share Posted May 29, 2008 Ok so heres the deal, I want a lightweight image gallery that reads from a specified directory. I have started working on one but I just can't get past one thing. I am not really sure what I am doing wrong. I have it list the pictures correctly but when I try to modify them into a table or div so I can start working on a simple template it screws up horribly. No matter what I make the width of the table or div the pictures just stay in one line and go all the way across the screen. Here is my script w/o the div/table <?php $d = dir("./"); $pattern="(\.gif$)|(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; while (false !== ($entry = $d->read())) { if(eregi($pattern, $entry)) echo '<a border="0" href="index.php?image='.$entry.'"><img border="0" width="100" height="100" src="'.$entry.'"</a>'; } if (is_null($HTTP_GET_VARS['image'])){ $image = "<img src='1-dna005.jpg'> "; } else { $image = "<img src='$HTTP_GET_VARS[image]'><br>{$HTTP_GET_VARS['image']}"; } echo "$image"; $d->close(); ?> and here it is with a div/table <?php $d = dir("./"); $pattern="(\.gif$)|(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)"; echo '<table width="500"><tr><td>'; while (false !== ($entry = $d->read())) { if(eregi($pattern, $entry)) echo '<a border="0" href="index.php?image='.$entry.'"><img border="0" width="100" height="100" src="'.$entry.'"></a> '; } echo '</td></tr></table>'; if (is_null($HTTP_GET_VARS['image'])){ $image = "<img src='1-dna005.jpg'> "; } else { $image = "<img src='$HTTP_GET_VARS[image]'><br>{$HTTP_GET_VARS['image']}"; } echo "$image"; $d->close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/ Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Share Posted May 29, 2008 Interesting that you're working on one too. I'm just finishing up the first version of mine. FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-552998 Share on other sites More sharing options...
servvs Posted May 29, 2008 Author Share Posted May 29, 2008 lol, hows yours turning out flying? I can't get mine to work right for some reason, any help would be awesome Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553007 Share on other sites More sharing options...
FlyingIsFun1217 Posted May 29, 2008 Share Posted May 29, 2008 Well, I'm too ADD to look through your code much, but when you say that you are trying to list it in a table (also something I'm working on), and that they just shoot horizontally accross the screen, it leads me to believe that you have forgotten that you need to start a new row (<tr>) and a new data cell into it (<td>). This is all assuming that you are trying to get it to go vertically (the listings). FlyingIsFun1217 Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553010 Share on other sites More sharing options...
servvs Posted May 29, 2008 Author Share Posted May 29, 2008 Well, the html is fine. What I am trying to do is make it display like 5 across and then start a new row dynamically. Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553011 Share on other sites More sharing options...
rondog Posted May 29, 2008 Share Posted May 29, 2008 Here is what I use to make a dynamic table: columnLimit is how many columns across you want obviously. <?php $table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n"; $table .= "<tr>\n"; $count = 0; $columnLimit = 5; while($row = mysql_fetch_array($sql)) { $count += 1; if($count == $columnLimit) { //if your at the last columd, add a new row $table .= "<td>your image</td>\n"; $table .= "</tr>\n<tr>\n"; $count = 0; } else { //else just add a new cell $table .= "<td>your image</td>\n"; } } if($count != $columnLimit) { $table .= "</tr>\n"; } $table .= "</table>\n"; echo $table; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553020 Share on other sites More sharing options...
servvs Posted May 29, 2008 Author Share Posted May 29, 2008 I am trying to do this w/o the use of any databases, I just want it to read from the directory Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553034 Share on other sites More sharing options...
rondog Posted May 29, 2008 Share Posted May 29, 2008 then make the while loop a for loop and have it loop X amount of times depending on how many images are in that directory Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553040 Share on other sites More sharing options...
servvs Posted May 29, 2008 Author Share Posted May 29, 2008 Yeah, im not too entirely sure what to do with that lol. I am still learing php as a language so I am not used to a lot of things. Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553074 Share on other sites More sharing options...
rondog Posted May 30, 2008 Share Posted May 30, 2008 you can use this to count how many files are in your folder: <?php $dir = '/path/to/directory/'; $filecount = 0; $d = dir($dir); while ($f = $d->read()) { if(($f!= ".") && ($f!= "..")) { if(!is_dir($f)) $filecount++; } } echo 'there are ',$filecount,' files in this folder'; ?> now $filecount is the variable you use for your for loop so like this: <?php $dir = '/path/to/directory/'; $filecount = 0; $d = dir($dir); while ($f = $d->read()) { if(($f!= ".") && ($f!= "..")) { if(!is_dir($f)) $filecount++; } } $table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n"; $table .= "<tr>\n"; $count = 0; $columnLimit = 5; for($i = 0; $i<=$filecount; $i++) { $count += 1; if($count == $columnLimit) { //if your at the last columd, add a new row $table .= "<td>your image</td>\n"; $table .= "</tr>\n<tr>\n"; $count = 0; } else { //else just add a new cell $table .= "<td>your image</td>\n"; } } if($count != $columnLimit) { $table .= "</tr>\n"; } $table .= "</table>\n"; echo $table; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553218 Share on other sites More sharing options...
servvs Posted May 30, 2008 Author Share Posted May 30, 2008 so it should look something like this <?php $dir = './'; $filecount = 0; $d = dir($dir); while ($f = $d->read()) { if(($f!= ".") && ($f!= "..")) { if(!is_dir($f)) $filecount++; } } $table = "<table width=\"0\" border=\"0\" cellspacing=\"5\" cellpadding=\"5\">\n"; $table .= "<tr>\n"; $count = 0; $columnLimit = 5; for($i = 0; $i<=$filecount; $i++) { $count += 1; if($count == $columnLimit) { //if your at the last columd, add a new row $table .= "<td>$f</td>\n"; $table .= "</tr>\n<tr>\n"; $count = 0; } else { //else just add a new cell $table .= "<td>$f</td>\n"; } } if($count != $columnLimit) { $table .= "</tr>\n"; } $table .= "</table>\n"; echo $table; ?> Im still not getting it, lol Quote Link to comment https://forums.phpfreaks.com/topic/107878-php-image-gallery/#findComment-553249 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.