如何使我的PHP滑块工作

时间:2015-01-16 08:12:21

标签: php jquery mysql slider nivo-slider

我的php滑块有点工作,我设法让它链接到我的数据库。但是,我需要它来循环我的滑块内的所有图像,我的代码不起作用,但我相信我错过了一个循环查询?!

这是我的幻灯片所在的代码:

<div class="theme-dark 16 columns">
        <div id="slider" class="nivoSlider">

            <img src="<?php print $row['image']?>"/>
           <!--  <img src="images/nivo/arts.png" alt="the grand theatre and nothern ballet">
            <img src="images/nivo/slider3.png" alt="leeds night light slider image">
            <img src="images/nivo/slider2.png" alt="Leeds Trinity slider image">
            <img src="images/nivo/slider4.png" alt="leeds art hotels">
            <img src="images/nivo/slider1.png" alt="leeds art slider image"> -->
        </div>
</div>

这是我的代码,以便在一瞬间运行我的幻灯片:

<?php

$myQuery = "SELECT * FROM SliderImg";

$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));


$row = mysqli_fetch_array($result);
?>

3 个答案:

答案 0 :(得分:0)

你的php需要一个循环:

<?php
$myQuery = "SELECT * FROM SliderImg";

$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($con));

$rows = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $rows[] = array(
        'image' => $row['image'];
    )
}
?>

在你的HTML中:

<div class="theme-dark 16 columns">
    <div id="slider" class="nivoSlider">
        <?php 
        foreach($rows as $row) { ?>
            <img src="<?php print $row['image']?>"/>
        <?php 
        } ?>
    </div>
</div>

答案 1 :(得分:0)

<?php    
   $myQuery = "SELECT * FROM SliderImg";
   $result = $con->query($myQuery);
   if (!$result) die('Query error: ' . mysqli_error($con));
?>
<div class="theme-dark 16 columns">
    <div id="slider" class="nivoSlider">
      // use a while here
       <?php  while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){ ?>
           <img src="<?php print $row['image']?>"/>
       <?php  } ?>
    </div>
</div>

答案 2 :(得分:0)

您可以使用foreach循环浏览数据,例如

<div class="theme-dark 16 columns">
    <div id="slider" class="nivoSlider">

        <?php foreach ($row as $key => $item): ?>
            <img src="<?php echo $item['image']?>"/>
        <?php endforeach; ?>

    </div>
</div>
希望它有所帮助..