在html表格中显示图像(IF循环)(Display images in html table (IF loop))

我正在编写一个PHP脚本,将一个目录中的图像发布为8列宽的表格格式,并且这些行可以扩展尽可能多的图像。 这个当前的代码我只在单独的行中发布它们。 我怎样才能将它们分成8个图像的行?

<?php $files = glob("images/*.*"); for ($i=1; $i<count($files); $i++) { $image = $files[$i]; $supported_file = array( 'gif', 'jpg', 'jpeg', 'png' ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { // print $image ."<br />"; echo '<a href="' .$image .'"><img src="'.$image .'" alt="Random image" width=200 /></a>'."<br /><br />"; } else { continue; } } ?>

I'm looking to write a PHP script that will post images from a directory into a table format that is 8 columns wide and the rows extend as many images as there are. This current code I have only posts them in separate rows. How can I divide them into rows of 8 images?

<?php $files = glob("images/*.*"); for ($i=1; $i<count($files); $i++) { $image = $files[$i]; $supported_file = array( 'gif', 'jpg', 'jpeg', 'png' ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { // print $image ."<br />"; echo '<a href="' .$image .'"><img src="'.$image .'" alt="Random image" width=200 /></a>'."<br /><br />"; } else { continue; } } ?>

最满意答案

像这样? $ i%8每8行返回0,所以我们所做的就是停止/基本启动<tr>标签。

<table> <tr> <?php $files = glob("images/*.*"); for ($i = 1; $i < count($files); $i++) { $image = $files[$i]; $supported_file = array( 'gif', 'jpg', 'jpeg', 'png' ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { // print $image ."<br />"; echo '<td><a href="' . $image . '"><img src="' . $image . '" alt="Random image" width=200 /></a></td>'; } if ($i % 8 === 0) { echo "</tr><tr>"; } } ?> </tr> </table>

Something like this? $i % 8 returns 0 every 8th row so all we do is stop stop/start the <tr> tag basically.

<table> <tr> <?php $files = glob("images/*.*"); for ($i = 1; $i < count($files); $i++) { $image = $files[$i]; $supported_file = array( 'gif', 'jpg', 'jpeg', 'png' ); $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION)); if (in_array($ext, $supported_file)) { // print $image ."<br />"; echo '<td><a href="' . $image . '"><img src="' . $image . '" alt="Random image" width=200 /></a></td>'; } if ($i % 8 === 0) { echo "</tr><tr>"; } } ?> </tr> </table>

更多推荐