如何为来自 Mysql 数据库的图像实现轮播


How to implement a carousel for images from a Mysql database

我正在尝试从Mysql数据库中获取数据(特别是图像),然后将它们显示在轮播中,例如在 amazon.com 主页上。

我是CSS的初学者。我没怎么用过。

我看过之前的问题,但我的问题不是图像列表。我只是通过数据库中的数据。

任何帮助将不胜感激。

根据我在下面的评论中的理解,您的问题与 CSS 严格相关。图像垂直显示,因为<li>没有彼此相邻浮动。

代码如下:

<html>
    <head>
        <title>Title</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="/path/to/jquery.jcarousel.js"></script>
        <style type="text/css">
        .jcarousel {position:relative; overflow:hidden;}
        .jcarousel ul {width: 20000em; position:relative; list-style:none; margin:0; padding:0;}
        .jcarousel li {float:left;}
        </style>
    </head>
    <body>
        <div class="jcarousel">
            <ul>
                <li><img src="image1.jpg" alt="" /></li>
                <li><img src="image2.jpg" alt="" /></li>
            </ul>
        </div>
        <script type="text/javascript">
            $(function(){
                $('.jcarousel').jcarousel();
            });
        </script>
    </body>
</html>
我知道

这是一个老问题,但它可能对未来的用户有用。以下是实现引导轮播以显示数据库中图像的简单解决方案:

从数据库中获取所有 imageID 并将其插入数组中(请注意,我将图像作为 BLOB 存储在我的数据库中:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <script src="jquery-3.1.0.min.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
    <style>
        .myCarousel{
            height: 100%;
            width: 700px ;
            margin-top: 5px;
        }
        .slideimage{
            width: 700px;
            height: 320px !important;
        }
    </style>
</head>
<body>

<?php
include ('DBconnection.php');
$ImageArray = array();
$queryImages = "SELECT * FROM imagetable ";
$result = mysqli_query($conn,$queryImages);
if(mysqli_num_rows($result) >0){
    while ($row = mysqli_fetch_array($result)){
        $ImageArray[] = $row;
    }
}
?>
                         <div id="myCarousel"  class="myCarousel carousel
                                    slideCarousel" data-ride="carousel" data-interval="5000" >
                                        <ol class="carousel-indicators">
                                        <?php
                                            // creating indicators - note that at least one must be set as active
                                            for($j=0;$j<count($ImageArray);$j++){
                                                    if($j==0){
                                                        echo ' <li data-target="#myCarousel" data-slide-to="'.$j.'" class="active"></li>';
                                                    }else{
                                                        echo ' <li data-target="#myCarousel" data-slide-to="'.$j.'"></li>';
                                                    }
                                            }
                                        ?>

                                        </ol>
                                        <div class="carousel-inner" role="listbox">

                                    <?php
                                          for($j=0;$j<count($ImageArray);$j++){
                                                 //If it is the first image set it as active
                                                  if($j==0){
                                                      echo '<div class="item active">
                                                            <!--Using image id and url  -->
                                                          <img class="slideimage" src="GetImageID.php?id=' .$ImageArray[$j]['ImageID'].'" /> 
                                                           <!-- or using base64_encode
                                                           <img src="data:image/jpeg;base64,'.base64_encode($ImageArray[$j]['Image']).'"/>
                                                           -->
                                                            </div>';
                                                  }
                                                  else{
                                                      echo '<div class="item">
                                                              <img class="slideimage" src="GetImageID.php?id=' .$ImageArray[$j]['ImageID'].'" /> 
                                                            </div>';
                                                  }
                                          }
                                            ?>

                                        </div>

                                        <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
                                            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                            <span class="sr-only">Previous</span>
                                        </a>
                                        <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
                                            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                            <span class="sr-only">Next</span>
                                        </a>
                                    </div>
</body>
</html>

GetImageID.php:

include ('DBconnection.php');
$imageid = $_GET['id'];
$query = "Select Image from imagetable WHERE ImageID = '$imageid'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_array($result);
header("Content-type: image/jpeg");
echo $row['Image'];