我正在构建一个显示给定目录中最新图像的部分。
总共有7个当前目录(稍后会添加更多目录,因此这个数字会有所不同)。
我有一个工作代码可以将最近的图像拉到我已经变成函数的目录中(displayRecent($directory);
)。
我已经构建了一个foreach
循环,为数组中的每个目录运行该函数。
<div class="row">
内,我希望最多有<div class="col-md-3">
。
因此,如果有9个目录,则总共有3个<div class="row">
,而最后一个目录只包含一个<div class="col-md-3">
。
我希望完成的是以下标记:
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth/5361af753f447.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
但我的代码只包含<div class="row">
和</div>
实际输出是:
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/nude//5361b0351fcb4.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
以下是我正在使用的代码:
$directories = array("goth", "nude", "goth", "goth", "goth", "goth", "goth", "goth", "goth");
$i = 0;
foreach ($directories as $directory) {
if($i == 4) {
echo '<div class="row">' . "\r\n";
}
echo '<div class="col-md-3">' . "\r\n";
displayRecent($directory);
echo '</div>' . "\r\n";
if($i == 4) {
echo '</div>' . "\r\n";
}
$i++;
}
displayRecent($directory)
函数的内容,以防它相关或可以构建:
function displayRecent($recentDir) {
$dir = 'gallery/'.$recentDir . '/';
$base_url = 'http://example.com/gallery/'.$recentDir.'/';
$newest_mtime = 0;
$show_file = 'BROKEN';
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime("$dir/$file");
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = "$base_url/$file";
}
}
}
}
echo '<img src="' .$show_file. '" alt="Image Title Here" class="img-thumbnail img-responsive">' . "\r\n";
}
修改
将foreach
循环更改为以下输出结果:
$directories = array("goth", "nude", "goth", "goth", "goth", "goth", "goth", "goth", "goth");
$i = 0;
foreach ($directories as $directory) {
if( !($i % 4) ) {
echo '<div class="row">' . "\r\n";
}
echo '<div class="col-md-3">' . "\r\n";
displayRecent($directory);
echo '</div>' . "\r\n";
if( !($i % 4) ) {
echo '</div>' . "\r\n";
}
$i++;
}
输出 - 出于某种原因,当同一个col-md-3
元素中应该还有三个row
时,第一个col-md-3
位于其自己的row
中:
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/nude//5361b0351fcb4.jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
<div class="row">
<div class="col-md-3">
<img src="http://example.com/gallery/goth//5361af753f447 (1).jpg" alt="Image Title Here" class="img-thumbnail img-responsive">
</div>
</div>
注意我在阵列中多次列出相同目录的原因是因为所有目录尚未构建,所以我重复一个用于测试目的。
注意2 仅仅因为我知道一些小丑可能会问,这不是一个色情网站,它只是一个模特网站。 :)
答案 0 :(得分:1)
在我的评论中解释并改进我的答案。
这是* .phtml语法:
<div class="row"> <!-- open first row -->
<?php $i = 1; ?>
<?php foreach ($directories as $directory) { ?>
<div class="col-md-3">
<?php displayRecent($directory); ?>
</div>
<?php if ( !($i % 4) ) { ?>
</div><div class="row"> <!-- close prev row, open next -->
<?php } ?>
<?php $i += 1; ?>
<?php } ?>
</div> <!-- close last row -->