使用PHP制作动态的Twitter Bootstrap轮播

时间:2012-09-07 08:52:45

标签: php mysql twitter-bootstrap carousel

我正在尝试制作一个使用Twitter Bootstrap默认JavaScript和CSS的旋转木马,但应该从MySQL数据库中提取图像。

我试图用php制作动态的旋转木马的HTML是这样的:

<div class="carousel-inner">
   <div class="active item">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginLast">
   </div>
   <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
   <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
</div>

我期待的PHP文件的最终结果是这样的

<div class="carousel-inner">
   <?php renderAds(); ?>
</div>

我写的PHP函数是这样的:

function renderAds(){
    $query = queryAds();
    $query_exe = mysql_query($query);

    if(mysql_num_rows($query_exe) == 0){
        echo '<img src="images/sampleAd.jpg" >';
    }else{

        $numb =1;
        $flag =1;
        while($fetched_data = mysql_fetch_array($query_exe)){

            if($numb == 1){
                if($flag == 1){
                    echo '<div class="active item">';
                }else{
                    echo '<div class="item">';
                }
            }elseif($numb == 4){
                echo '<a href="'.$fetched_data['url'].'"  class="pull-left marginLast" ><img src="'.$fetched_data['image_url'].'"></a>';
                echo '</div>';
                $numb =0;
            }else{
                echo '<a href="'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
            }
            $numb++;
            $flag++;
        }


        echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>';
        echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>';
    }


}

这里,通过与twitter bootstrap的默认轮播不同,我用4张图片制作每张幻灯片,我希望每张幻灯片过渡包含4张图片,而不是默认情况下一张幻灯片的单张图片。在HTML中它可以工作,但是当我用我的PHP函数替换它时,第一张幻灯片可以工作,但是如果我按下轮播中的下一个箭头整个div崩溃。

我怎样才能让它发挥作用?

  

注意:此处最后一张图片必须包含marginLast类和   其他应该有marginTop和pull-left类。

我期望在html中呈现的内容是这样的:

<div class="carousel-inner">

            <div class="active item">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginLast">
            </div>

            <div class="item">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginLast">
            </div>

            <div class="item">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginTop">
                <img src="images/sampleAd.jpg" class="pull-left marginLast">
            </div>

            <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
            <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>
   </div>

如何使我的PHP函数显示如上所示的结果标记?

编辑:我得到的输出是这样的:

<div id="myCarousel" class="carousel slide">
          <!-- Carousel items -->
          <div class="carousel-inner">

                <div class="active item">
                    <a href="xx2" class="pull-left marginTop" ><img src="images/sampleAd.jpg"></a>
                    <a href="xx" class="pull-left marginTop" ><img src="images/sampleAd.jpg"></a>
                    <a href="sample.com"  class="pull-left marginLast" ><img src="images/sampleAd.jpg"></a>
                </div>

                <a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>
                <a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>             
          </div><!-- end carousel-inner -->

  </div><!-- myCarousel -->

根据标记,这应该有效,但它不起作用,默认情况下,如果没有幻灯片,那么箭头应该被禁用,但它是可点击的。

4 个答案:

答案 0 :(得分:5)

你有MySQL查询只返回一个数组。这样,您的查询可以重复使用(然后可以成为一个函数),您可以根据自己的需要呈现数据。

<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

$sql = "SELECT * FROM table";
$res = $mysqli->query($sql);

$rows = array();
while ($row = $res->fetch_assoc()) {
    $rows[] = $row;
}
?>
<div class="carousel">
  <div class="carousel-inner">
<?php $i = 1; ?>
<?php foreach ($rows as $row): ?>
<?php $item_class = ($i == 1) ? 'item active' : 'item'; ?>
    <div class="<?php echo $item_class; ?>">
      <a href="<?php echo $row['url']; ?>">
        <img src="<?php echo $row['image']; ?>" alt="<?php echo $row['title']; ?>" />
      </a>
    </div>
<?php $i++; ?>
<?php endforeach; ?>
  </div>
</div>

您显然需要更改数据库查询以及foreach循环中使用的值。我也使用了mysqli函数,因为mysql函数现在已被弃用,而有利于MySQLi(改进了MySQL)。

答案 1 :(得分:2)

我想我已回答了我自己的问题,尽管以上所有答案都给了我许多想法。谢谢大家

function renderAds(){
    $query = queryAds();
    $query_exe = mysql_query($query);
    if(mysql_num_rows($query_exe) == 0){
        echo '<img src="images/sampleAd.jpg" >';
    }else{
        $numb =1;
        $flag =1;
        echo '<div class="carousel-inner">';
        while($fetched_data = mysql_fetch_array($query_exe)){
            if($numb == 1){
                if($flag == 1){
                    echo '<div class="active item">';
                }else{
                    echo '<div class="item">';
                }
                echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
            }elseif($numb == 4){
                echo '<a target="_blank" href="http://'.$fetched_data['url'].'"  class="pull-left marginLast" ><img src="'.$fetched_data['image_url'].'"></a>';
                $numb = 0;
                echo '</div>';
            }else{
                echo '<a target="_blank" href="http://'.$fetched_data['url'].'" class="pull-left marginTop" ><img src="'.$fetched_data['image_url'].'"></a>';
            }

            $numb++;
            $flag++;
        }

        if($numb > 1 && $numb < 4 ){
            echo '</div>';
            echo '</div>';
        }elseif($numb == 1){
            echo '</div>';
        }elseif($numb == 4){
            echo '</div>';
            echo '</div>';
        }

        echo '<a class="carousel-control left" href="#myCarousel" data-slide="prev">&lsaquo;</a>';
        echo '<a class="carousel-control right" href="#myCarousel" data-slide="next">&rsaquo;</a>';
    }
}

答案 2 :(得分:0)

<div class="carousel-inner">

<?php 
// Twitter Bootstrap Carousel.
$caseStudies = get_posts(array(
  'numberposts' => 12,
  'post_type'   => 'case-study',
  'meta_key'    => 'show_case_study_home',
  'meta_value'  => 'yes',
  'post_status' => 'publish',
));

$open = 0;
$close = 1;
$active = 0;
$didClose = false;
foreach($caseStudies as $caseStudy){

  $image  = get_field('image_home', $caseStudy->ID);
  $header = get_field('header_home', $caseStudy->ID);
  $intro  = get_field('intro_home', $caseStudy->ID);

  $activeCss = "";

    if(($open)%4 == 0) {
      if ($active == 0) $activeCss = " active";
     echo '<div class="item'. $activeCss .'"><ul class="thumbnails">';
    }
?>

<li class="span3">
  <div class="case-study">
  <img src="<?php echo $image['url']; ?>" alt="">
  <a href="<?php echo get_permalink($caseStudy->ID); ?>" class="btn-more read-hover">Read More</a>
  <h4><?php echo $header; ?></h4>
  <p><?php echo $intro; ?></p>
  </div>
</li>

<?
    if(($close)%4 == 0) {
     echo "<ul></div>";
     $didClose = true; // Maybe the carousel has less than 4 items ...
    }

  $open++;
  $close++;
  $active++;
}
if($didClose == false) echo "</div></div>"; // If has less than 4 items in the carousel close the tags.
?>

</div><!--/.caroussel-inner -->

答案 3 :(得分:0)

我解决了它:

<div id="myCarousel" class="carousel slide" data-ride="carousel">
        <ol class="carousel-indicators">
            <?php $slider = $guest->getAllData("slider"); ?>
            <?php foreach( $slider as $slide) : ?>
                <li data-target="#myCarousel" data-slide-to="<?=$slide['id']; ?>" class=""></li>
            <?php endforeach; ?>
        </ol>
        <div class="carousel-inner">
            <?php foreach($slider as $slide) : ?>
                <div class="item ">
                    <img src="images/slider/<?=$slide['img']; ?>" alt="Los Angeles">
                </div>
            <?php endforeach; ?>
        </div>

        <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>

并添加JS代码:

$('.carousel-indicators>li:first-child').addClass('active');
$('.carousel-inner>div:first-child').addClass('active');