php - 动态构建引导行,每行 3 个
问题描述
我不确定解决此问题的最佳方法,在我正在构建的站点(使用引导程序)上,我希望使用网格系统每行有 3 张卡,例如:
HTML:
<div class="container">
<div class="row">
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
<div class="col-sm">
One of three columns
</div>
</div>
</div>
这通常没问题,但我正在动态构建(或尝试):
PHP:
<main>
<br /><br /><br />
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
echo '<div class="container"><div class="row">';
foreach($pages as $page)
{
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
?>
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<img src="<?= $code[3]; ?>" class="card-img-top" alt="<?= $code[0]; ?>">
<div class="card-body">
<p class="card-text"><a href="<?= $page; ?>"><?= substr($code[0], 0, 25); ?> ...</a></p>
</div>
</div>
</div>
<?php
if ($count == 18) {
// The reason only 15 is showing is because we ignore ".", ".." & "index.php".
break;
}
}
}
echo '</div></div>';
?>
</main>
对于这个项目,我正在服务器上扫描 .php 页面,然后尝试将它们布置为每行 3 个,所以在每行 3 个之后我需要echo '<div class="container"><div class="row">';
从我所看到的开始一个新行,我不知道最好的方法为此,我们将不胜感激。
解决方案
main
<?php
$pages = array_slice(scandir($_SERVER['DOCUMENT_ROOT']), 2);
$mixed = shuffle($pages);
$count = 0;
$output = '';
foreach($pages as $page) {
$count++;
if (strpos($page, '.php') !== false && $page != 'index.php') {
$html = file_get_contents($page);
$code = explode("|", extractXvideos($html));
$output .= '<div class="container">';
$output .= '<div class="row">';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '<div class="col">Column</div>';
$output .= '</div>
$output .= '</div>';
}
}
echo $output;
?>
main
试试这个模板并应用你的条件。
推荐阅读
- kubernetes - 现在监控磁盘和网络 Heapster 已被弃用
- javascript - 如何动态添加 Floodlight 代码
- loops - 无限循环崩溃,但不在更新函数中
- python - 用于导出 c++ 的 vgg16 keras 保存模型
- android - 当主类自行扩展片段时如何使用时间选择器对话框?
- performance - QSGImageNode 规模性能
- java - 从输入流中连续读取 XML 作为字符串
- java - 在 n 行中拆分文件并将它们添加到字符串的 ArrayList
- angular - Angular 2 单元测试在它应该失败时总是通过
- python - 在pygame中同时检测两个CTRL