下面的PHP代码总共生成240个DIV。我想在7,10,21,135和201 DIV之后插入不同的DIV。我该怎么办?
这些是我想要展示的DIV:
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="div7">div 7 description goes here</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="col-xs-3">field</div>
<div class="div10">div 10 description goes here</div>
<div class="col-xs-3">field</div>
...goes on
这是PHP代码:
<div class="row">
<?php foreach ($this->fields as $field) { ?>
<div class="col-xs-3 EditFormClass">
<?php echo $field[0]; ?>
</div>
<div class="col-xs-3 EditFormClass">
<?php echo $field[1]; ?>
</div>
<?php } ?>
</div>
答案 0 :(得分:1)
您可以将异常情况放在数组中,然后运行循环:
<?php
$difDiv = [];
$difDiv[7] = 'div 7 description goes here';
$difDiv[10] = 'div 10 description goes here';
?>
<div class="row">
<?php $i = 0; ?>
<?php foreach ($this->fields as $field) { ?>
<div class="col-xs-3 EditFormClass">
<?php echo $field[0]; ?>
</div>
<div class="col-xs-3 EditFormClass">
<?php echo $field[1]; ?>
</div>
<?php
$i++;
if (isset($difDiv[$i])) { ?>
<div class="div<?php echo $i ?> "><?php echo $difDiv[$i]; ?></div>
<?php } ?>
<?php } ?>
</div>