动态拆分div列

时间:2019-06-12 09:43:09

标签: javascript php html while-loop

我在下面编写的代码可以正常工作,但是在循环结束时,div没有关闭,仍在打开循环

<div class="carousel-inner">
    <div class="item active">
       <div class="row">
        <?php 
        $recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";   
        $recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error()); 
        $i=0; $split=0;

        while($projects=mysql_fetch_array($recent_projects_conn)) {   
             $i++; 
             ?>
             <div class="col-sm-3">
                <div class="col-item" style="">
                    <div class="photo-shadow"></div>
                    <div class="photo">
                        <img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
                    </div>
                    <div class="info">
                        <div class="name">
                            <?php echo $projects['service_name']; ?>
                        </div>
                        <div class="degination">
                            <?php echo $projects['sub_title']; ?>
                        </div>
                        <div class="buttons">
                            <a class="btn btn-theme ripple-effect" href="#">View More</a>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
            <?php 
            $split++;   
            if ($split % 4 == 0){  
               echo '</div></div><div class="item"><div class="row">';  
            }   
       }
       ?>
        </div>
    </div>
</div> 

div分割得很好,但是在循环结束时div尚未关闭。多数民众赞成在唯一的问题,请为我提供帮助以解决问题

当我检查元素时,最后一个循环将以给定的结果显示如下:

<div class="col-sm-3">
    <div class="col-item">
        <div class="photo-shadow"></div>
        <div class="photo">
            <img src="admin/assets/images/uploads/projects/1557301934.jpg" alt="User one">
        </div>
        <div class="info">
            <div class="name">UPHOLSTERY</div>
            <div class="degination">UPHOLSTERY</div>
            <div class="buttons">
                <a class="btn btn-theme ripple-effect" href="#">View More</a>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
</div> 
</div></div><div class="item"><div class="row">

我想动态删除两个开头的div。我该如何设置以在循环结束时删除打开的div

1 个答案:

答案 0 :(得分:-1)

您可以这样做:

<div class="carousel-inner">
<?php 
$recent_projects_sql="SELECT * from recent_projects where service_type='upholstery'";   
$recent_projects_conn=mysql_query($recent_projects_sql) or die(mysql_error()); 
$i=0; $split=0;

while($projects=mysql_fetch_array($recent_projects_conn)) {   
     $i++; 
?>
    <div class="item <?php if($i==1) echo "active";?>">
       <div class="row">
             <div class="col-sm-3">
                <div class="col-item" style="">
                    <div class="photo-shadow"></div>
                    <div class="photo">
                        <img src="admin/assets/images/uploads/projects/<?php echo $projects['attachment1']; ?>" alt="User one">
                    </div>
                    <div class="info">
                        <div class="name">
                            <?php echo $projects['service_name']; ?>
                        </div>
                        <div class="degination">
                            <?php echo $projects['sub_title']; ?>
                        </div>
                        <div class="buttons">
                            <a class="btn btn-theme ripple-effect" href="#">View More</a>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<?php 
}
?>
</div>