php - 如何在循环中仅显示一次 div 并在仅运行一次的 div 内显示下一个 div
问题描述
我尝试在 google 和 SO 上找到解决方案,但我没有找到解决方案。
我有一个类似的代码。
$index = 0;
while (some condition here) {
if ($index < 4) {?>
<div class="first4">
<p>Some text here</p>
</div>
<?php }
else{
$check=0;
if ($check==0){?>
<div class="displayOnceInwhile">
<?php $check=1; }?>
<div class="InsideaboveClass"></div>
<?php }
$index++;}?>
我对上述代码所做的是,如果$index
小于 4,则内部文本将显示 else$check
将仅在循环中运行一次,但它不起作用。另外,请注意这里我混淆了我应该在哪里关闭displayOnceInwhile
关闭</div>
。
预期结果
<!--first 4 will display-->
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<div class="first4"><p>Some text here</p></div>
<!--Set will display like this-->
<div class="displayOnceInwhile">
<div class="InsideaboveClass"></div>
</div>
解决方案
希望这就是你想要做的。
<?php
$check = 0;
$index = 0;
while (some condition here) {
if ($index < 4) {
echo '<div class="first4"><p>Some text here</p></div>';
} else {
if ($check==0){
echo '<div class="displayOnceInwhile">';
$check=1;
}
echo '<div class="InsideaboveClass"></div>';
}
$index++;
}
echo '</div>';
?>
推荐阅读
- ios - UIScrollView Lag 中的嵌套 UIStackViews
- excel - 删除excel单元格中以相同字符开头的所有单词
- zend-framework - Zend 框架 3 ORDER BY FIELD
- angular - 在 scss 文件中添加字体时出现 Angular 9 错误
- php - HTML/PHP 电子邮件表单无法正常工作
- javascript - 如何遍历 Extjs 网格的 DOM 元素?
- angular - 如何以角度检查 sessionStorage 中的值?
- visual-foxpro - 如何使用visual foxpro中的任何条件根据另一个表中另一列的计数来更新列?
- javascript - Angular,asp.net.core webApi,显示服务器角度错误
- yii2 - Yii2 codecept 错误 - 调用未定义的方法 Codeception\Util\PathResolver::isPathAbsolute