在bootstrap Carousel中的foreach循环

时间:2017-05-02 06:15:05

标签: php codeigniter

这是我的bootstrap Carousel代码。我想动态显示图像。但这里只显示了一张图片......我无法弄清楚这背后的问题是什么。

这是我的代码

<?php 
foreach($article_post as $row) {
    if(!isset(unserialize($row->post_image)[0])){
        echo "No Image";
    } else {
        $a=unserialize($row->post_image)[0];
        $b=unserialize($row->post_image);
    }
}
?>
<div class="col-md-8 col-xs-12 chng1"> 
    <?php if (count($b) == 1) { ?>
        <img src="<?php echo base_url()."assets/".$a; ?>" width="100%" height="450"  style="position:relative" class="img-wdth">
    <?php } elseif(count($b)>1) { ?>
        <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <!-- Wrapper for slides -->
            <div class="carousel-inner">
                <div class="item active">
                    <img src="<?php echo base_url()."assets/".$a?>" alt="Los Angeles"> 
                </div>
            </div>
        <!-- Left and right controls -->
            <a class="left carousel-control" href="#myCarousel" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#myCarousel" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>    
    <?php } ?>
</div>    

2 个答案:

答案 0 :(得分:0)

试试这个:

<?php foreach($article_post as $row){  
                    $c=0;
                   if(!isset(unserialize($row->post_image)[0])){
                      echo "No Image";
                     }
                     else{
                       $a=unserialize($row->post_image)[0];
                        $b=unserialize($row->post_image);?>

<div class="col-md-8 col-xs-12 chng1"> 
 <?php if (count($b) == 1) { ?>

 <img src="<?php echo base_url()."assets/".$a?>" width="100%" height="450"  style="position:relative" class="img-wdth">
 <?php } elseif(count($b)>1){ $c++; ?>

 <?php if($c==1){echo '<div id="myCarousel" class="carousel slide" data-ride="carousel"><div class="carousel-inner"><div class="item active">';} ?>

  <!-- Wrapper for slides -->


      <img src="<?php echo base_url()."assets/".$a?>" alt="Los Angeles"> 
<?php if($c==1){echo '</div></div>';} ?>
 <?php } } }?>
<!-- Left and right controls -->
  <a class="left carousel-control" href="#myCarousel" data-slide="prev">
    <span class="glyphicon glyphicon-chevron-left"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="right carousel-control" href="#myCarousel" data-slide="next">
    <span class="glyphicon glyphicon-chevron-right"></span>
    <span class="sr-only">Next</span>
  </a>
</div>

答案 1 :(得分:0)

<?php foreach($article_post as $row){

                   if(!isset(unserialize($row->post_image)[0])){
                      echo "No Image";
                     }
                     else{
                       $a=unserialize($row->post_image)[0];
                        $b=unserialize($row->post_image);

                                         ?>

    <div class="col-md-8 col-xs-12 chng1">
     <?php if (count($b) == 1) { ?>

     <img src="<?php echo base_url()."assets/".$a?>" width="100%" height="450"  style="position:relative" class="img-wdth">
     <?php } elseif(count($b)>1){ //Greater than 1 then use another foreach
        ?>
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
      <!-- Wrapper for slides -->
      <div class="carousel-inner">
        <div class="item active">
            <?php foreach($b as $c){ ?>
              <img src="<?php echo base_url()."assets/".$c?>" alt="Los Angeles">
            <?php }  ?>
        </div>
      </div>
    <!-- Left and right controls -->
      <a class="left carousel-control" href="#myCarousel" data-slide="prev">
        <span class="glyphicon glyphicon-chevron-left"></span>
        <span class="sr-only">Previous</span>
      </a>
      <a class="right carousel-control" href="#myCarousel" data-slide="next">
        <span class="glyphicon glyphicon-chevron-right"></span>
        <span class="sr-only">Next</span>
      </a>
    </div>


<?php } }}?>