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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.