Jump to content

IMAGE display table


agentjozef

Recommended Posts

I have the code below displaying images and image names. I want these to display in a table 2 rows high by the needed number of columns to show all the images in the directory. I have no idea what to do. What I am getting now is a single column with each image in its own row.

<?php

$path = "./uploaded/";

$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;

//show in a table 2 rows by required/needed number of columns

echo'<div>';

echo '<table border="1">';

echo "<img src='$path/$file' alt='$file'>"."<img src='$file' alt='$file'>";

echo'</table>';

echo '<div>';

}

?>

Link to comment
Share on other sites

sorry the above code is not what I have it is this:

<?php

$path = "./uploaded/";

$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;

//show in a table 2 rows by required/needed number of columns

echo'<div>';

echo '<table border="1">';

echo "<td><img src='$path/$file' alt='$file'><br />"

echo "<img src='$file' alt='$file'></td>";

echo'</table>';

echo '</div>';

}

?>

Link to comment
Share on other sites

You should do something like this:

 

 

Put only images and some table elements in a loop:

 

echo '<table>';

$counter = 0;

 

while(count($image) != $counter){

      echo "<tr>";

      echo "<td>". $image[$counter] . "</td>";

      ++$counter;

      echo "<td>" . $image[$counter] . "</td>";

      echo "</tr>";

      ++$counter;

}

 

echo '</table>';

Link to comment
Share on other sites

My code now looks like what is below. It seems that all of the images are being placed one on top of the other in one cell.

 

<?php

$path = "./uploaded/".$_SESSION['sec'];

$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;

//show in a table 2 rows by required/needed number of columns

 

$aaa = ("<td><img src='$path/$file' alt='$file'><br />"."<img src='$file' alt='$file'></td>");

 

}

echo '<table border="1">';

echo '<td>'.$aaa.'</td>';

echo'</table>';

Link to comment
Share on other sites

1. "dynamic" = will vary - for example... 500 images would result in 250 images wide - I am certain you might not like that.

    I know what dynamic means. I would be extremely happy with such a result.

 

2. Did you notice that Ivan's code puts the echo INSIDE the loop'; whereas your only echoes once OUTSIDE the loop?

    With all due respect, did you notice my comment after Ivan's saying I do not understand him?

 

Anyhow, I have changed the code to look like below, but still there is only one cell appearing. My expectation (although not what I want) from this was to have one row with dynamic columns - I want 2 rows. But, unfortunately that is not even happening.

 

<?php

$path = "./uploaded/";

$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;

//show in a table 2 rows by required/needed number of columns

echo '<table border="1">';

$aaa = ("<td><img src='$path/$file' alt='$file'><br />"."<img src='$file' alt='$file'></td>");

 

}

 

echo $aaa;

echo'</table>';

?>

Link to comment
Share on other sites

try (not proof read not tested)...

<?PHP
echo '<table border="1">';
echo "<tr>";
$images = glob("./uploaded/{*.jpg,*.gif,*.png}");
$count = count($images);
$half = ceil($count/2);
$half1 = ($count/2);
$i=0;
while($i<$half){
?>
<td><img src="<?PHP echo $images[$i]; ?>"></td>
<?PHP
$i++;
}
echo "</tr><tr>";

$i=$half;
while($i<$count){
?>
<td><img src="<?PHP echo $images[$i]; ?>"></td>
<?PHP
$i++;
}
if($half1 != $half) {
echo "<td></td>";
}
echo "</tr>";
echo "</table>";
?>

Link to comment
Share on other sites

Ok (6 cups of coffee later); tested the glob, can't get it quite right sooooooo....

 

tested this

<?php
$path = "images/";
$dir_handle = @opendir($path) or die("Unable to open folder");
while (false !== ($file = readdir($dir_handle))) {
if($file == "test898.php" OR $file == "." OR $file == "..")
	continue;
$images[] = $path . $file;
}
closedir($dir_handle);
$count = count($images);
$row1 = ceil($count/2);
$row2 = $count - $row1;
$display1 = "<table border='1'><tr>";
for($i=0;$i<$row1;$i++) {
$display1 = $display1 . "<td><img src='" . $images[$i] . "'</td>";
}
$display1 = $display1 . "</tr>";
if($count>1) {
$display1 = $display1 . "<tr>";
for($i=$row1;$i<$count;$i++) {
	$display1 = $display1 . "<td><img src='" . $images[$i] . "'</td>";
}
$display1 = $display1 . "</tr>";
}
$display1 = $display1 . "</table>";
echo $display1;
?>

example - http://nstoia.com/easy_family_2/easy_tree/test898.php

Link to comment
Share on other sites

Thank you for your help litebearer! It is truly appreciated. Your script does what I wanted.

I am referring to your untested one (I noticed you sent a message while I was typing this.)

Nevertheless, I got out of that mud pit and into another :)

I went ahead and changed and complicated your script (2) above by attempting to add a function to delete files.

But to no surprise, it does not function. Interestingly, it does not give me the error message associated with not deleting either.

I would like some help on getting this sucker to delete selected files. Also, in what way can I get just the file names to show rather than the whole path?

Here is the script:

 

<html>

<body>

<?php

{

    foreach($_POST['file[$i]'] as $file)

    { 

        unlink($file) or die("Failed to delete file");

    }

    header("location: " . $_SERVER['REQUEST_URI']); //this should refresh the page leaving only undeleted files

}

?>

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

<?php

echo '<table border="1">';

echo "<tr>";

$file = glob("./uploaded/*.*");

$count = count($file);

$half = ceil($count/2);

$half1 = ($count/2);

$i=0;

while($i<$half){

echo ("<td><img src=".$file[$i]."><br />"."<input type='CHECKBOX' name='file' value='$file[$i]'>".$file[$i]."</td>");

$i++;

}

echo "</tr><tr>";

 

$i=$half;

while($i<$count){

echo ("<td><img src=".$file[$i]."><br />"."<input type='CHECKBOX' name='file' value='$file[$i]'>".$file[$i]."</td>");

$i++;

}

if($half1 != $half) {

echo "<td></td>";

}

echo "</tr>";

echo "</table>";

 

//if directory is empty do not show delete button   

$dirPath = "./uploaded";

$handle = opendir($dirPath);

$c = 0;

while ($file = readdir($handle)&& $c<3) {

    $c++;

}

if ($c>2) {

echo '<input type="submit" name="Delete" value="Delete">';

} else {

  closedir($dir_handle);

}   

echo '</form>';

?>

</body>

</html>

 

Link to comment
Share on other sites

As to file name sans path, review this and see if you can incorporate it (untested)

 

while($i<$half){
$path_parts = pathinfo($file[$i]);
$image_name = $path_parts['basename']
?>
<td>
<img src="<?PHP echo $file[$i]; ?>">
<br />
<input type='CHECKBOX' name="cbox[<?PHP echo $i; ?>]" value="<? echo $file[$i]; ?>"
<?PHP echo $image_name; ?>
</td>
<?PHP
$i++;
}

Link to comment
Share on other sites

Yes lightbearer, that did the trick. Thank you.

Here is what that section looks like now.

 

<?php
echo '<table border="1">';
echo "<tr>";
$file = glob("./uploaded/*.*");
$count = count($file);
$half = ceil($count/2);
$half1 = ($count/2);
$i=0;

while($i<$half){
$path_parts = pathinfo($file[$i]);
    $image_name = $path_parts['basename'];
echo "<td><img src=".$file[$i]."><br />"."<input type='CHECKBOX' name='cbox[$i]' value='$file[$i]'>".$image_name.'</td>';
$i++;
}
echo "</tr><tr>";

$i=$half;
while($i<$count){
$path_parts = pathinfo($file[$i]);
    $image_name = $path_parts['basename'];
echo "<td><img src=".$file[$i]."><br />"."<input type='CHECKBOX' name='cbox[$i]' value='$file[$i]'>".$image_name.'</td>';
$i++;
}
if($half1 != $half) {
echo "<td></td>";
}
echo "</tr>";
echo "</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.