我想通过从数据库中提取图像来移动我的滑块

时间:2015-05-30 13:39:46

标签: php

<div class="carousel-inner" role="listbox">
    <div class="active item">
        <?php 
        $latest_work=mysqli_query($conn,"select * from banners where status=1"); 
        while($latest_object= mysqli_fetch_object($latest_work)){
            ?>
            <img src="<?=WEBSITE_URL?>url/<?php if(isset($latest_object->image))echo $latest_object->image;?>"alt="Image" style="width:100%;height:500px;"> 
        </div>
        <div class="item "> 
            <?php 
        }
        ?> 
        <div> 
        </div>
        ------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

不确定您要实现的目标,但您的代码看起来很混乱:

  1. 你应该正确格式化
  2. 你在while循环之前打开一个div,然后在打开另一个之前关闭它
    • 这可能有效,但后来难以阅读
  3. 图片的线条正确搞砸了
  4. 试试这个:

        <div class="carousel-inner" role="listbox">
    
            <?php 
            $latest_work=mysqli_query($conn,"select * from banners where status=1"); 
            // start a counter, at 1 to avoid confusion over 0 indexing
            $i = 1;
            while($latest_object= mysqli_fetch_object($latest_work)){
    
                // here, we have a counter, so that we can add the 'active' class to the first itteration
                if ($i == 1) {
                    // we are the first itteration, so the calss should be 'item active'
                    $class = 'item active';
                } else {
                    // otherwise, its just 'item'
                    $class = 'item';
                }
                // increment our counter $i
                $i++;
                if(isset($latest_object->image)) {
                    $image = WEBSITE_URL . 'url/' . $latest_object->image
    
                    echo '<div class="'.$class.'">';
                    echo '<img src="'.$image.'" alt="Image" style="width:100%;height:500px;"> ';
                    echo '<div>';
    
                } else {
                    // do something else
                }
    
            }
            ?> 
    </div>
            ------------------------------------------------------------------------
    

    形成这一点,你的代码应该是可读的,然后你可以尝试解释你的问题