如何仅在轮播的第一个div中获得活动课程?

时间:2019-05-01 06:30:40

标签: php bootstrap-4 carousel

尝试启动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>

4 个答案:

答案 0 :(得分: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 
    }
?>

答案 1 :(得分: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 
    }
?>

答案 2 :(得分:0)

有几种方法可以解决此问题。我将描述解决该问题的最简单方法,但请告知您的数组是否不适合我的条件。

1。如果数组的键是数字键,则可以使用这种条件

  • PHP 7.3

edHex

我添加了<?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; ?>类应该只放在数组的第一个元素上

  • PHP <= 7.3

active

关键条件为<?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 <?php if ($key === key($result)): ?>active<?php endif; ?>

在这里,您仅检查数字的<?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)元素中

答案 3 :(得分: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>