Jump to content

Display Image Directory Listing in Grid


JacobRyan

Recommended Posts

<?php
$path = "../assets/tattoos/";
$path2 = "../assets/tattoos-thumbs/"; 
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	unlink($path. "/" . $file) or die("Failed to delete file");

	unlink($path2. "/" . $file) or die("<meta http-equiv=\"refresh\" content=\"0; url=index.php\" />");
}

}
?>

<form name="form1" method="post">

<?php
$imageDir = "../assets/tattoos/";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;
echo "<input type='CHECKBOX' name='file[]' value='$file'>";
echo "<img src='../assets/tattoos/$file' style='height:auto;width:8%;' alt='$file'>";
}
closedir($dir_handle);
?>

<input type="submit" name="Delete" value="Delete">
</form>

 

I have this code, which calls an image directory and adds a checkbox next to it, you check the boxes you wish, hit delete and the pictures are removed from the server. I need them to display in a grid view, like 4 columns by X amount of rows. Any help would be greatly appreciated.

Link to comment
Share on other sites

example:

echo "<table>";
echo "<tr>";
for ($y = 0; $y < 50; $y++) {
  echo ($x % 4 == 0) ? "</tr><tr>" : "";
  echo "<td>" . $y . "</td>";
  $x++;
}
echo "</tr>";
echo "</table>";

 

Sorry for my lack of knowledge (I'm still fairly new to PHP and kind of stumbling my way through it for now), where would I place that in my existing code?

Link to comment
Share on other sites

you don't place that directly in your code.  You learn from the principle and apply it to your code.

 

The principle is this: use a variable to act as a counter, that increments every iteration of the loop.  Also within your loop, use the modulus operator to check if the current value of your counter variable is divisible by 4 (the number of columns you want). If it is, end the current table row and start a new one.

 

Link to comment
Share on other sites

Welp, I'm here to help people learn, not hand out freebies, as I do this sort of thing for a living.  I'll be more than happy to apply the concept to your posted code and hand you the resulting code if you want to pay me for it.  Or perhaps a more altruistic soul will do it for you for free.  I do suggest you try to learn it though, otherwise what is the point in coding it yourself?

Link to comment
Share on other sites

Welp, I'm here to help people learn, not hand out freebies, as I do this sort of thing for a living.  I'll be more than happy to apply the concept to your posted code and hand you the resulting code if you want to pay me for it.  Or perhaps a more altruistic soul will do it for you for free.  I do suggest you try to learn it though, otherwise what is the point in coding it yourself?

 

Question, what would be the purpose of echoing the table tag if it will be just one table?

Link to comment
Share on other sites

Not sure I understand the question.  The example code only outputs one table.  It starts out by echoing out the opening table tag and opening table row tag.  Then in the loop it outputs a closing and then opening table row tag based on the condition, meanwhile outputting individual table cell tags for each value.  Then after the loop, it finishes off by outputting a final closing table row tag and finally the closing tag for the table.

Link to comment
Share on other sites

Not sure I understand the question.  The example code only outputs one table.  It starts out by echoing out the opening table tag and opening table row tag.  Then in the loop it outputs a closing and then opening table row tag based on the condition, meanwhile outputting individual table cell tags for each value.  Then after the loop, it finishes off by outputting a final closing table row tag and finally the closing tag for the table.

 

Ahhh okay. The other examples I've seen a lot of people just began the <table> tag outside of the php tag. That makes much more sense now.

Link to comment
Share on other sites

<?php
$path = "../assets/tattoos/";
$path2 = "../assets/tattoos-thumbs/"; 
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	unlink($path. "/" . $file) or die("Failed to delete file");

	unlink($path2. "/" . $file) or die("<meta http-equiv=\"refresh\" content=\"0; url=index.php\" />");
}

}
?>

<form name="form1" method="post">
<?php
$imageDir = "../assets/tattoos/";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {

if($file == "index.php")
continue;
if($file == ".")
continue;
if($file == "..")
continue;
echo "table";
echo "<tr>";
for ($y = 0; $y < 4; $y++) {
echo ($x % 4 == 0) ? "</tr><tr>" : "";
echo "<td><input type='CHECKBOX' name='file[]' value='$file'><img src='../assets/tattoos/$file' style='height:auto;width:50%;' alt='$file'></td>";
  $x++;
}
}
echo "</tr>";
echo "</table>";
closedir($dir_handle);
?>

346mk2c.png

 

That's what I have it to now. It's repeating the directory in a vertical row, then making a new one next to it.

Link to comment
Share on other sites

Doesn't seem to matter where I move the echo for ending the table row I seem to terminate it to earlier then I'm left with a long list of the same picture repeating based on whatever value y has. And if I leave it outside the loop then, I get a table of the same image, but I guess at least it's a table?

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.