fricx Posted September 28, 2009 Share Posted September 28, 2009 Hello, I am listing images from folder with this code: $imgdir = 'uploads/'.$nick.'/'; // the directory, where images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show $dimg = opendir($imgdir); while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$allowed_types)) { $a_img[] = $imgfile; sort($a_img); reset ($a_img); } } $totimg = count($a_img); // total image number for($x=0; $x < $totimg; $x++){ echo ' <div class="malaslika" style="width:100px;height:75px"><a href="../index.php?slika='.$a_img[$x].'"><img src="../uploads/'.$nick.'/'.$a_img[$x].'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></div>'; } Now, I want to list this in table with max 4 columns and not important how many rows. So the thing is how to insert images from for loop into table with 4 columns? Anyone can help? Thank you in advance. Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/ Share on other sites More sharing options...
MatthewJ Posted September 28, 2009 Share Posted September 28, 2009 Make a counter... and write a loop... check the counter, if it is eqaul to 4, start a new row? Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926640 Share on other sites More sharing options...
.josh Posted September 28, 2009 Share Posted September 28, 2009 I think that stuff between the td tags can be cleaned up but I don't know your dir struct so I left it alone... $imgdir = 'uploads/'.$nick.'/'; // the directory, where images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show $a_img = glob($imgdr."*.{".implode(',',$allowed_types)."}",GLOB_BRACE); sort($a_img); echo "<table><tr>"; foreach ($a_img as $img) { if ($colCount % 4 == 0) echo "</tr><tr>"; $colCount++; echo ' <td><a href="../index.php?slika='.$img.'"><img src="../uploads/'.$nick.'/'.$img.'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></td>'; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926644 Share on other sites More sharing options...
fricx Posted September 28, 2009 Author Share Posted September 28, 2009 I think that stuff between the td tags can be cleaned up but I don't know your dir struct so I left it alone... $imgdir = 'uploads/'.$nick.'/'; // the directory, where images are stored $allowed_types = array('png','jpg','jpeg','gif'); // list of filetypes I want to show $a_img = glob($imgdr."*.{".implode(',',$allowed_types)."}",GLOB_BRACE); sort($a_img); echo "<table><tr>"; foreach ($a_img as $img) { if ($colCount % 4 == 0) echo "</tr><tr>"; $colCount++; echo ' <td><a href="../index.php?slika='.$img.'"><img src="../uploads/'.$nick.'/'.$img.'" width="100" height="75" border="0" onclick="changeImg(velika, "'.$kojaslika.'.jpg")" /></a></td>'; } echo "</tr></table>"; It makes table right, but it doesn't insert images from folder I specified but from my main folder. Don't know why it does that? But thanks for the code. Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926647 Share on other sites More sharing options...
.josh Posted September 28, 2009 Share Posted September 28, 2009 that's because your path/to/image is probably wack. offhand I see in $imgdr you have it listed as a subfolder of the current working directory (no ../) but in the path/to/image in your image tag, you build it it again instead of using your $imgdr, and it has that "../" at the beginning, which makes it look for the directory above the current working directory. I would suggest changing your img src to src="'$imgdr.$img.'" edit: oops, forgot the concat dot src="'.$imgdr.$img.'" Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926649 Share on other sites More sharing options...
fricx Posted September 28, 2009 Author Share Posted September 28, 2009 that's because your path/to/image is probably wack. offhand I see in $imgdr you have it listed as a subfolder of the current working directory (no ../) but in the path/to/image in your image tag, you build it it again instead of using your $imgdr, and it has that "../" at the beginning, which makes it look for the directory above the current working directory. I would suggest changing your img src to src="'$imgdr.$img.'" edit: oops, forgot the concat dot src="'.$imgdr.$img.'" Yeahh, but somehow $img variable contains full path to image "uploads/nick/image.jpg" and I need only "image.jpg". Is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926655 Share on other sites More sharing options...
.josh Posted September 28, 2009 Share Posted September 28, 2009 ah yah true forgot about that technicality. You can use basename to get just the image name or just remove $imgdr from the src="..." altogether. Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926672 Share on other sites More sharing options...
fricx Posted September 28, 2009 Author Share Posted September 28, 2009 ah yah true forgot about that technicality. You can use basename to get just the image name or just remove $imgdr from the src="..." altogether. Did it ! Thank you Crayon once more ! You are a real PHP guru =)) Quote Link to comment https://forums.phpfreaks.com/topic/175862-solved-list-images-in-table/#findComment-926688 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.