Jump to content

Help counting variables and applying a rule


Chappers

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.