我有三个不同类的主div:CLASS1; ClASS2和CLASS3。现在我想在所有类中使用单个while循环来从数据库和显示中获取三个图像。
第一个类是class1,然后两个div具有相同的类class2,然后最后一个div具有class3。如何通过循环,请帮助,谢谢:)
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<div class="col-md-2">
<div class="ad1">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-2">
<div class="ad2">
<img src="" alt="">
</div>
</div>
<div class="col-md-5">
<div class="ad3">
<img src="" alt="">
</div>
</div>
</div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
while ($row = mysqli_fetch_array($result)) { ?>
答案 0 :(得分:2)
更新答案以反映问题中的更改
我可以大致看到你想要达到的目标,希望这对你有帮助......
<div class="col-md-9 hidden-sm hidden-xs">
<div class="col-md-1"></div>
<?php
$query = "SELECT * FROM headerimages limit 4";
$result = mysqli_query($con, $query) or die(mysqli_error($query));
$class_number = 1;
$class_two_repeated = FALSE;
while ($row = mysqli_fetch_array($result)):
// This bit ensures the 2nd & 3rd divs have class 'ad2'
if ($class_number == 3 && !$class_two_repeated) {
$class_two_repeated = TRUE;
$class_number--;
}
?>
<div class="col-md-<?= $class_number == 3 ? 5 : 2 ?>">
<div class="ad<?= $class_number++ ?>">
<img src="/path/to/<?= $row['whatever_the_image_field_is_named'] ?>" alt="">
</div>
</div>
<?php
endwhile;
?>
</div>