如何只在旋转木马的第一个div中获得活动类?

问题描述 投票:1回答:4

尝试使用bootstrap-4的动态轮播,其中图像来自数据库。但是只允许单个div在旋转木马中激活,那么我该如何实现呢?如何在foreach循环中的第一个div中激活。

<?php 
    include '../employee.php';
    $obj= new employees();
    $result=$obj->select_image();
 ?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Hello, world!</title>
  </head>
  <body>
<div id="carouselExampleInterval" class="carousel slide" data-ride="carousel">
  <div class="carousel-inner">
  <?php 
    foreach ($result as $values){
  ?>
  <?php echo $values['profile_pic']; ?> 
    <div class="carousel-item active" data-interval="2000">
      <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
    <?php 
    }
    ?>

   </div>
  <a class="carousel-control-prev" href="#carouselExampleInterval" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleInterval" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>
php bootstrap-4 carousel
4个回答
1
投票

php中的一个简单方法是使用布尔值并使用它来确定它是否是循环的第一次迭代。

<?php
    $isFirst = True;

    foreach ($result as $values){
        echo $values['profile_pic']; 

        if ($isFirst == True) {
            $isFirst == False;
?>  
    <div class="carousel-item active" data-interval="2000">
    <?php
        } else {
    ?>
    <div class="carousel-item" data-interval="2000">
    <?php
        }
    ?>
      <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
<?php 
    }
?>

0
投票

您可以在for循环中使用索引并检查它是否为0(第一个循环)。

<?php
    foreach ($result as $index => $values) {
?>
<?php echo $values['profile_pic']; ?>
    <div class="carousel-item <?php echo $index === 0 ? 'active' : ''; ?>" data-interval="2000">
        <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
<?php 
    }
?>

0
投票

有几种方法可以解决这个问题。我将描述解决它的最简单方法,但如果您的阵列不符合我的条件,请告诉我。

1. If keys of your array is numeric, you can use this type of condition

  • PHP 7.3
<?php
foreach ($result as $key =>  $values) {
    ?>
    <?php echo $values['profile_pic']; ?>
    <div class="carousel-item <?php if ($key === array_key_first($result)): ?>active<?php endif;?>" data-interval="2000">
        <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
    <?php
}
?>

我添加了php <?php if ($key === array_key_first($result)): ?>active<?php endif; ?>,这意味着active类应该只放在数组的第一个元素

  • PHP <= 7.3
<?php
foreach ($result as $key => $values) {
    ?>
    <?php echo $values['profile_pic']; ?>
    <div class="carousel-item <?php if ($key === key($result)): ?>active<?php endif; ?>"
         data-interval="2000">
        <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
    <?php
}

关键条件是php <?php if ($key === key($result)): ?>active<?php endif; ?>

  • 所有PHP版本(最简单)
<?php
foreach ($result as $index => $values) {
    ?>
    <?php echo $values['profile_pic']; ?>
    <div class="carousel-item <?php if ($index === 0): ?>active<?php endif; ?>"
         data-interval="2000">
        <img src="<?php echo $values['profile_pic']; ?>" class="d-block w-100" alt="...">
    </div>
    <?php
}

在这里你只检查index这是数字,并将active类只放在你的数组的第一个([0])元素


0
投票

我感谢所有这些努力都是可行的,但我找到了另一个解决方案,我认为这是最简单的。

我只是在这里使用$ count!

<?php 
    include '../employee.php';
    $obj= new employees();
    $result=$obj->select_image();
 ?>
<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

    <title>Dhruv</title>
  </head>
  <body>

<div class="container-fluid">
    <div class="row">
            <div class="col-lg-12">
                <div id="carouselExampleInterval" class="carousel slide" data-ride="carousel">
                      <div class="carousel-inner">
                      <?php 
                      $count = 0;
                        foreach ($result as $values){
                      ?>
                        <div class="carousel-item <?php 
                            if($count==0){
                              echo "active";  
                            }
                            else{
                                echo " ";
                            }
                        ?>" data-interval="2000">
                          <img src="<?php echo $values['profile_pic']; ?>" class=" w-100 d-block " alt="...">
                        </div>
                        <?php 
                        $count++;
                        }
                        ?>
                       </div>
                      <a class="carousel-control-prev" href="#carouselExampleInterval" role="button" data-slide="prev">
                        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
                        <span class="sr-only">Previous</span>
                      </a>
                      <a class="carousel-control-next" href="#carouselExampleInterval" role="button" data-slide="next">
                        <span class="carousel-control-next-icon" aria-hidden="true"></span>
                        <span class="sr-only">Next</span>
                      </a>
                    </div>
            </div>
    </div>
</div>
</div>

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  </body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.