agentjozef Posted January 5, 2012 Share Posted January 5, 2012 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/ Share on other sites More sharing options...
agentjozef Posted January 5, 2012 Author Share Posted January 5, 2012 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1304415 Share on other sites More sharing options...
Ivan Ivković Posted January 6, 2012 Share Posted January 6, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1304878 Share on other sites More sharing options...
agentjozef Posted January 6, 2012 Author Share Posted January 6, 2012 Sorry, but I am new to this and I do not understand what you are telling me. Could you elaborate please? Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1304985 Share on other sites More sharing options...
agentjozef Posted January 6, 2012 Author Share Posted January 6, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305012 Share on other sites More sharing options...
litebearer Posted January 6, 2012 Share Posted January 6, 2012 curious ... how many potential images will there be? Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305091 Share on other sites More sharing options...
agentjozef Posted January 7, 2012 Author Share Posted January 7, 2012 A dynamic amount. Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305100 Share on other sites More sharing options...
litebearer Posted January 7, 2012 Share Posted January 7, 2012 1. "dynamic" = will vary - for example... 500 images would result in 250 images wide - I am certain you might not like that. 2. Did you notice that Ivan's code puts the echo INSIDE the loop'; whereas your only echoes once OUTSIDE the loop? Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305133 Share on other sites More sharing options...
agentjozef Posted January 7, 2012 Author Share Posted January 7, 2012 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305236 Share on other sites More sharing options...
litebearer Posted January 7, 2012 Share Posted January 7, 2012 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305253 Share on other sites More sharing options...
litebearer Posted January 7, 2012 Share Posted January 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305274 Share on other sites More sharing options...
agentjozef Posted January 7, 2012 Author Share Posted January 7, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305285 Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2012 Share Posted January 7, 2012 In the future, when posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305286 Share on other sites More sharing options...
litebearer Posted January 7, 2012 Share Posted January 7, 2012 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305351 Share on other sites More sharing options...
agentjozef Posted January 7, 2012 Author Share Posted January 7, 2012 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305378 Share on other sites More sharing options...
agentjozef Posted January 9, 2012 Author Share Posted January 9, 2012 I figured the delete function out. At the top, $_POST['file[$i]'] Inside [] should be 'cbox'. Quote Link to comment https://forums.phpfreaks.com/topic/254393-image-display-table/#findComment-1305686 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.