使用循环打印轮播图像

时间:2018-06-09 19:15:24

标签: javascript php

因此,我正在尝试自动化我的网站,以显示与库存项目(车辆)相关的所有图像。我能够显示图像,但由于一个项目需要活动类,因此它们都出现在一个块中。这有可能实现自动化吗?或者有没有办法在循环中使用'active'一次?

下面是代码(更新!),而不仅仅是我正在处理的所有内容。是的,我已经把jQuery和JavaScript拉进来了!:)谢谢!

<?php

            include 'Login-System/db.php';

            $query = 'SELECT * FROM forsale ORDER by StockID ASC';
            $result = mysqli_query($conn, $query);

            if($result):
                if(mysqli_num_rows($result)>0):
                    while($forsale = mysqli_fetch_assoc($result)):

                        ?>

                        <div class="col-md-6" >
                            <div class="forsale">
                                <?php
                                    $match = $forsale['StockID'];

                                    $queryimg = "SELECT * FROM forsaleimg WHERE StockID = ".$match." ORDER BY 'Order' ASC ";
                                    $resultimg = mysqli_query($conn, $queryimg);


                                ?>

                                <!--Image Carousel-->
                                <div id="myCarousel" class="carousel slide" data-ride="carousel">
                                    <!-- Indicators -->
                                    <ol class="carousel-indicators">
                                        <?php 
                                            for($i=0; $i <4; $i++){
                                        ?>
                                        <li data-target="#myCarousel" data-slide-to="<?php echo $i;?>" <?php if ($i == 0) {
                                            echo 'class="active"';
                                        } ?>></li>
                                        <?php   

                                            }
                                        ?>
                                    </ol>

                                    <!-- Wrapper for slides -->
                                    <div class="carousel-inner" role="listbox" style="width:auto; height:auto;" >
                                         <?php
                                            $active = 0; 
                                        if($resultimg):
                                            if(mysqli_num_rows($resultimg)>0):
                                                while($forsaleimg = mysqli_fetch_assoc($resultimg)):

                                                ?>

                                                <div class="carosuel-item <?php if ($active == 0){echo 'active';}?>">
                                                    <img class="d-block w-100 img-responsive" src="<?php echo $forsaleimg['FileDestination']; ?>">
                                                </div>

                                                <?php 
                                                    $active= $active + 1;
                                                endwhile;
                                            endif;
                                        endif;
                                        ?>

                                    </div>

                                    <!-- Left and right controls -->
                                    <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                                        <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                        <span class="sr-only">Previous</span>
                                    </a>

                                    <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                                        <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                        <span class="sr-only">Next</span>
                                    </a>
                                </div>

1 个答案:

答案 0 :(得分:0)

在班级轮播指标中。只需从0循环到3,并在每次迭代中输出您需要的li。如果你做一个简单的for循环(i = 0; i&lt; 4; i ++)等,那么你可以检查i == 0,如果是,将活动类添加到li。