我有以下数组:
<?php
$sets = array (
array (
'img' => 'file.png',
'heading' => 'Slide Title 1',
'lead' => 'Slide leadription 1',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 2',
'lead' => 'Slide leadription 2',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 3',
'lead' => 'Slide leadription 2',
),
array (
'img' => 'file.png',
'heading' => 'Slide Title 3',
'lead' => 'Slide leadription 2',
)
);
?>
提供此
的输入<?php
foreach ($sets as $set) {
?>
<!-- START THE FEATURETTES -->
<div class="row featurette">
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
</div>
<?php
}
?>
现在这完全正常,但我想要md-7
HTML和md-5
HTML交替,所以现在每个其他人都会
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
</div>
所以基本上在左右图片和细节之间交替
更新
根据Jhansen的建议,这不起作用。它只需要第一组,它不会在两者之间交替。
<?php
foreach ($sets as $set) {
?>
<!-- START THE FEATURETTES -->
<?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
</div>
<?php else: ?>
<hr class="featurette-divider">
<div class="row featurette">
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
</div>
<?php endif; ?>`
<?php
}?>
答案 0 :(得分:2)
如果您只是想要替代,为什么不将html封装在模数声明中呢?
IE,
<?php $count = 1; ?>
<?php if( $count % 2 != 0 ): ?>
... HTML for first arrangement ...
<?php else: ?>
... HTML for second arrangement ...
<?php endif; ?>`
答案 1 :(得分:1)
如果要切换所有循环,建议的jhansen是正确的:
<?php
$count=1;
foreach ($sets as $set) { ?>
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<?php if($count % 2 != 0){ ?>
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<?php }else{ ?>
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<?php } ?>
</div>
<?php $count++; } ?>
如果你只想切换第一个,应该这样做:
<?php
foreach ($sets as $k => $set) { ?>
<!-- START THE FEATURETTES -->
<hr class="featurette-divider">
<div class="row featurette">
<?php if($k==0){ ?>
<div class="col-md-7">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<div class="col-md-5">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<?php }else{ ?>
<div class="col-md-7">
<h2 class="featurette-heading"><?php echo $set['heading']?></h2>
<p class="lead"><?php echo $set['lead']?></p>
</div>
<div class="col-md-5">
<img class="featurette-image img-responsive center-block" src="<?php echo $set['img']?>" alt="Feature">
</div>
<?php } ?>
</div>
<?php } ?>