从数据库中获取数据后如何在div中滑动

时间:2016-05-13 10:57:17

标签: php jquery html mysql

我试图找到如何在从数据库中获取数据后滑动数据块。 我尝试了很多,但没有得到结果。请帮助我找出解决方案 这是我的代码

<html>
<head>

</head>
<body>
<?php 
$servername="localhost";
$username="root";
$password="";
$db="lalcoresidency";

$conn=mysqli_connect($servername,$username,$password,$db);

if(!$conn){
    die("connection failed:".mysqli_connect_error());
}
?>
<div class="flexslider">
<ul class="slides">
<?php
$query="select * from testimonial order by r_id desc";
$result = mysqli_query($conn, $query);

while($row=mysqli_fetch_array($result)){
    ?>
    <li>
    <?php echo $row['review'];?>
    </li>
<?php
    }
?>


<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
    $('.flexslider').carousel({
      interval: 3200
    });
  });
</script>
</body>

它已经花了我的时间,但我没有得到解决方案。这段代码只能逐行显示数据

my code output
"Loving Staff Welcoming and helpful... Kind and welcome the food is great. Breakfast included."
"Total value for money."
"Excellent facilities with good location and very co-operative and efficient staff."
"Accommodation worth the money paid for... Staff were very helpful and in-house car hire rates were so reasonable."
"An excellent stay... Loved the stay and definitely look forward to keep going back for our next stay."

它逐个显示数据。但我想显示第一行数据,然后滑动后需要显示第二行。然后滑动第三行。 请帮我找到解决方案

3 个答案:

答案 0 :(得分:0)

那是因为.carousel没有在jQuery中定义。

这是jQuery的插件,您可以从他们的网站下载。

https://plugins.jquery.com/jcarousel/

另外,请不要忘记关闭<div><ul>代码。

答案 1 :(得分:0)

jQuery本身并不提供.carousel - 函数,你使用的插件是什么?

还尝试使用控制台查找javascript错误(Ctrl+Shift+K in FirefoxCtrl+Shift+J in Chrome

答案 2 :(得分:0)

您需要先纠正代码,这是错误的编程方式。只需使用下面的代码,它就可以完美运行。

始终尝试从html中分离代码逻辑。创建函数来处理代码逻辑。

示例更正后的代码:

<html>
    <head>
        <title>Testimonials</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.1/flexslider.css"/>
    </head>
    <body>


        <?php
        $dbConnectionObject = getConnection('localhost', 'root', '', 'lalcoresidency');
        if ($dbConnectionObject != false) {

            //Get all data for slides
            $slides = getAllTestimonialsReviews($dbConnectionObject);


            //Check if slides count greater thab zero
            if (count($slides) > 0) {
                ?>
                <div class="flexslider">
                    <ul class="slides">

                        <?php
                        foreach ($slides as $slide) {
                            ?>
                            <li><?php echo $slide['review']; ?></li>
                        <?php } ?>
                    </ul>
                </div>
                <?php
            }
        }
        ?>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/flexslider/2.6.1/jquery.flexslider.js"></script>
        <script>

            (function ($) {
                jQuery(document).ready(function () {
                    jQuery('.flexslider').flexslider({
                        animation: "slide",
                        animationLoop: true,
                        itemWidth: 200,
                        itemMargin: 5
                    });
                });

            }(jQuery));
        </script>
    </body>


    <?php
     // Turn on error reporting 
    error_reporting(1);
    ini_set('display_errors', true);

    //Get database connection object
    function getConnection($host, $user, $pass, $databasename) {

        //Check empty
        if (empty($host) || empty($user) || empty($pass) || empty($databasename)) {
            die("Invalid db connection parameters ");
        }

        //vars
        $servername = $host;
        $username = $user;
        $password = $pass;
        $db = $databasename;

        //conenction object
        $connectionObject = mysqli_connect($servername, $username, $password, $db);

        if (!$connectionObject) {
            die("connection failed:" . mysqli_connect_error());
        }

        return $connectionObject;
    }

    //Get all testimonials
    function getAllTestimonialsReviews($connectionObject = null) {

        //empty check
        if (empty($connectionObject)) {
            die("No conenction object in getALLTestimonialsReviews function");
        }

        //Query
        $query = "select `review` from testimonial order by r_id desc";

        //query result set
        $result = mysqli_query($connectionObject, $query);

        $resultSet = array();
        while ($row = mysqli_fetch_assoc($result)) {

            //grab all results in array
            $resultSet[] = $row;
        }


        //return result
        return $resultSet;
    }