Chappers Posted May 30, 2011 Share Posted May 30, 2011 Hi, I'm writing my own code for my website but have come across something I can't fathom and don't know what search terms I'd need to describe what I want, to allow me to Google it. I've got some code that retrieves the image names of pictures inside a directory, that then prints them on the screen as links. Then people can click whichever link to see that pic, and I can just upload new images to the images folder without needing to add the links to the code manually. All is working, but I want to use a table for neatness and put a certain number of the image links in each row. So, say I have 20 images in the folder, but want to display only 5 image links per row in the table, I need 4 rows to be created with 5 links in each one. I know I can use count() to count the number of images found in the folder, but I don't know how to use PHP to make a new row every 5 image links and put the 5 links in the row. This is my code so far: <?php $dir = '/home/vol7/myhost.com/b12345678/htdocs/images/'; $files = scandir($dir); foreach ($files as $pic) { if (is_dir($dir.$pic)) { unset($pic);} else { echo "$pic"; }} ?> So, if I got it doing what I want, it'd end up outputting something like this if I had 7 image links: <table> <tr> <td><a href=etc">image 1</a> - <a href=etc">image 2</a> - <a href=etc">image 3</a> - <a href=etc">image 4</a> - <a href=etc">image 5</a></td> </tr> <tr> <td><a href=etc">image 6</a> - <a href=etc">image 7</a></td> </tr> </table> Any help appreciated, even a link to a webpage explaining something similar that gives me enough information to alter it and make it work for what I want. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/237893-help-counting-variables-and-applying-a-rule/ Share on other sites More sharing options...
wildteen88 Posted May 30, 2011 Share Posted May 30, 2011 Have a look at this snippet. Have a play with it so you understand how it works. Then see if you can apply it your code. Quote Link to comment https://forums.phpfreaks.com/topic/237893-help-counting-variables-and-applying-a-rule/#findComment-1222456 Share on other sites More sharing options...
Chappers Posted May 31, 2011 Author Share Posted May 31, 2011 Excellent, many thanks for your time and help. I've been able to learn from that and do my own code now, to suit my needs. I hadn't come across the FOR function before. Thanks again, I owe you one. Quote Link to comment https://forums.phpfreaks.com/topic/237893-help-counting-variables-and-applying-a-rule/#findComment-1223206 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.