PHP的引导轮播指标

问题描述

我阅读了将php代码放入此处以创建引导轮播的解决方案:

PHP with Bootstrap Carousel

但是我对指示器有点麻烦,如果我将php代码放在那里,只会出现1个指示器。我希望显示的指标与数据库中的图像一样多。

希望有人可以提供帮助。

我现在拥有的代码只是指标的非PHP代码,我最终总是只有1个指标。

<!-- Wrapper for slides -->
<div class="carousel-inner">
  <?php
    $counter = 1;
    foreach($images->results() as $image){
  ?>
                    
  <div class="item<?php if($counter <= 1){echo " active"; } ?>">
    <img class="carouselimg" src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
  </div>
                    
  <!-- Indicators --> //This is what I can't find
  <ol class="carousel-indicators">
    <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
    //also tried <li data-target="#myCarousel" data-slide-to=<?php echo $counter-1;?> class=<?php if($counter <= 1){echo 'class="active"'}?>></li>
    <li data-target="#myCarousel" data-slide-to="1"></li>
    <li data-target="#myCarousel" data-slide-to="2"></li>
    <li data-target="#myCarousel" data-slide-to="3"></li>
  </ol>
                    
                    <?php
                        $counter++;
                        }
                    ?>
                </div>

解决方法

主要问题是您要为每张幻灯片重复生成整个指标<ol>。您应该只生成一次<ol>,并且在其中应该为每个幻灯片生成一个<li>

<!-- Wrapper for slides -->
<div class="carousel-inner">
<?php
    // Carousel items:

    $counter = 1;
    foreach($images->results() as $image){
?>
                                        
    <div class="item<?php if($counter <= 1){echo " active"; } ?>">
        <img class="carouselimg" src="<?php echo $image->url; ?>" alt="<?php echo $image->description; ?>">
    </div>
<?php
        $counter++;
    }
?>
    <!-- Indicators --> //This is what I can't find
    <ol class="carousel-indicators">
<?php

    $counter = 1;
    foreach($images->results() as $image){
?>
        <li data-target="#myCarousel" data-slide-to="<?= $counter ?>" <?php if ($counter==1){ ?>class="active"<?php } ?>></li>
    </ol>
                                        
<?php
        $counter++;
    }
?>
</div>

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...