我正在尝试创建照片滑块。当我对其进行硬编码时,一切都很好。但是当我尝试从数据库滑块中获取值时,无法以正确的方式查看。请看一下并给我一个解决方案。问题是这两个类分别称为item active和item.so,因此我无法申请每个循环。
有问题的代码
<div class="container">
<!--The main div for carousel-->
<div class="container text-center">
<div class="carousel slide row" data-ride="carousel" data-type="multi" data-interval="2000" id="fruitscarousel">
<?php foreach($this->data['image_results']->result() as $image_result):?><?php echo $image_result->Url;?>
<div class="carousel-inner">
<div class="item active">
<div class="col-md-3 col-sm-4 col-xs-12">
<a class="item active" href="Counting?img_id=<?php echo $image_result->ImgId;?>&href=<?php echo $image_result->Url;?>"><img class="img-responsive" src="<?php echo $image_result->ImgPath; ?>"></a>
</div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12">
<a href="Counting?img_id=<?php echo $image_result->ImgId;?>&href=<?php echo $image_result->Url;?>"><img class="img-responsive" src="<?php echo $image_result->ImgPath; ?>"></a>
</div>
</div>
</div>
<?php endforeach; ?>
<a class="left carousel-control" href="#fruitscarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control" href="#fruitscarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div>
</div>
</div>
正常工作的代码
<div class="container">
<h1>Bootstrap Multiple image sliding demo</h1>
<!--The main div for carousel-->
<div class="container text-center">
<div class="carousel slide row" data-ride="carousel" data-type="multi" data-interval="2000" id="fruitscarousel">
<div class="carousel-inner">
<div class="item active">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/1.jpg" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/2.jpg" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/2.jpg" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/3.jpg" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/4.jpg" class="img-responsive"></a></div>
</div>
<div class="item">
<div class="col-md-3 col-sm-4 col-xs-12"><a href="#"><img src="images/5.jpg" class="img-responsive"></a></div>
</div>
</div>
<a class="left carousel-control" href="#fruitscarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control" href="#fruitscarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div>
</div>
</div>
jQuery和样式
<script>
jQuery(document).ready(function() {
jQuery('.carousel[data-type="multi"] .item').each(function(){
var next = jQuery(this).next();
if (!next.length) {
next = jQuery(this).siblings(':first');
}
next.children(':first-child').clone().appendTo(jQuery(this));
for (var i=0;i<2;i++) {
next=next.next();
if (!next.length) {
next = jQuery(this).siblings(':first');
}
next.children(':first-child').clone().appendTo($(this));
}
});
});
</script>
<style>
.carousel-control.left, .carousel-control.right {
background-image:none;
}
.img-responsive{
width:100%;
height:auto;
}
@media (min-width: 992px ) {
.carousel-inner .active.left {
left: -25%;
}
.carousel-inner .next {
left: 25%;
}
.carousel-inner .prev {
left: -25%;
}
}
@media (min-width: 768px) and (max-width: 991px ) {
.carousel-inner .active.left {
left: -33.3%;
}
.carousel-inner .next {
left: 33.3%;
}
.carousel-inner .prev {
left: -33.3%;
}
.active > div:first-child {
display:block;
}
.active > div:first-child + div {
display:block;
}
.active > div:last-child {
display:none;
}
}
@media (max-width: 767px) {
.carousel-inner .active.left {
left: -100%;
}
.carousel-inner .next {
left: 100%;
}
.carousel-inner .prev {
left: -100%;
}
.active > div {
display:none;
}
.active > div:first-child {
display:block;
}
}
</style>
邮递区号
$imagefromdatabse_query="SELECT * FROM Advertisements WHERE Active='1' ORDER BY ImgId DESC";
$this->data['image_results']=$this->db->query($imagefromdatabse_query);
最佳答案
在轮播内部进行循环:如果要选择第一个图像作为活动图像,请添加活动类
<div class="container">
<!--The main div for carousel-->
<div class="container text-center">
<div class="carousel slide row" data-ride="carousel" data-type="multi" data-interval="2000" id="fruitscarousel">
<div class="carousel-inner">
<?php foreach($this->data['image_results']->result() as $key => $image_result):?><?php echo $image_result->Url;?>
<div class="item <?php if($key == 0) {echo 'active';}?>">
<div class="col-md-3 col-sm-4 col-xs-12">
<a href="Counting?img_id=<?php echo $image_result->ImgId;?>&href=<?php echo $image_result->Url;?>"><img class="img-responsive" src="<?php echo $image_result->ImgPath; ?>"></a>
</div>
</div>
<?php endforeach; ?>
</div>
<a class="left carousel-control" href="#fruitscarousel" data-slide="prev"><i class="glyphicon glyphicon-chevron-left"></i></a>
<a class="right carousel-control" href="#fruitscarousel" data-slide="next"><i class="glyphicon glyphicon-chevron-right"></i></a>
</div>
</div>
</div>