yellowepi Posted May 12, 2010 Share Posted May 12, 2010 I'm attempting to create code that will take the file names from a folder, insert the names into an image tag, and insert the images into a table. The problem with my code is that it uses the same image in every row. What would an easy way to be to fix this? <?php function printtd() { $dirname = "imagegallery"; $dh = opendir($dirname) or die("couldn't open directory"); while ($file = readdir($dh)) { if(preg_match("(jpg|bmp|jpeg|png|gif)", $file)) { echo "<img src='imagegallery/".$file."' height='15 %'/>"; } } closedir($dh); } ?> <?php echo "<table> \n"; for ($y=1;$y<=2;$y++) { echo "<tr> \n"; for ($x=1;$x<=6;$x++) { echo "<td> \n"; printtd(); echo"</td> \n"; } echo"</tr> \n"; } echo"</table> \n"; ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 13, 2010 Share Posted May 13, 2010 Your logic is wrong. You have to get the list of files first and then use them in the for loops. Also, I would use the glob function to get the files: <?php $images = glob('imagegallery/*.{jpg,bmp,jpeg,png,gif}',GLOB_BRACE); $i = 0; echo "<table> \n"; for ($y=1;$y<=2;$y++) { echo "<tr> \n"; for ($x=1;$x<=6;$x++) { echo '<td><img src="' . $images[$i++] . '">' . "</td>\n"; } echo"</tr> \n"; } echo"</table> \n"; ?> Ken Quote Link to comment Share on other sites More sharing options...
yellowepi Posted May 13, 2010 Author Share Posted May 13, 2010 ty, I was wondering if i could create an array, but just could not figure out how to do it. Quote Link to comment Share on other sites More sharing options...
plugnz Posted May 16, 2010 Share Posted May 16, 2010 Hi there, Hope you don't mind but I've been trying to do this too without success. I tried kenrbnsn's version which is closer but I get the first image of each row repeated. Which seems to be what yellowepi was saying was happening originally. here's my code: echo "<table>"; echo "<tr ><td colspan = '5'>"; echo "<h1>Category: " . $category_name . " </h1>\n"; echo "</td></tr>"; $ImageDir = "img/category/"; $ImageThumb = $ImageDir . "/thumbnails/"; $getpic = "SELECT image_id, image_group, image_name, location_no " . //$getpic = "SELECT image_id " . "FROM cms_images_category " . "WHERE category_name = '" . $_GET['category'] . "'"; "ORDER BY image_group "; $results = mysql_query($getpic,$conn) or die(mysql_error()); while ($row = mysql_fetch_array($results)) { extract($row); $images = $ImageThumb . $image_id; $i = 0; //echo "<table> \n"; for ($y=1;$y<=1;$y++) { echo "<tr> \n"; for ($x=1;$x<=3;$x++) { echo "<td><h2>" . $image_group . "</h2></td>\n"; echo "<td><a href =\"".$ImageDir . $image_id . ".jpg\" target='top'>"; echo "<img src=\"".$images . ".jpg\"></a></td>\n"; echo "<td><h2>" . $image_name . "</h2></td>\n"; } echo"</tr> \n"; } } echo "<tr><td><p>Click <a href ='search.php'>here</a> to return to categories<p></td></tr></table>"; probably not very tidy code either. If someone could point me in the right direction that would be awesome. Also the amount of images in a row seems to be determined by the $x<=3, is there a way of the screen width determining this? Thanks in advance. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 16, 2010 Share Posted May 16, 2010 Please start a new topic instead of hijacking a solved topic. Ken Quote Link to comment Share on other sites More sharing options...
plugnz Posted May 16, 2010 Share Posted May 16, 2010 Ok, sorry. Quote Link to comment 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.