如何使用php

时间:2017-03-13 22:46:18

标签: javascript php mysqli

我正在尝试设置一个滑块。我有一个容器,有4张照片消失,4张照片淡入。我想要总共20张照片(4 X 5)我想从DESC订购的桌子上得到这张照片,但我不知道如何做到这一点我必须在每第4行后输入一些代码。因为我需要在div中放入4和4个图像。所以我到目前为止所做的是将表中的前4个放在1 div中。但我不知道如何获得最后16个..我猜最好的方法是以某种方式改变php功能或者我需要为每个第4个做一个函数吗?

HTML CODE:

<div id="slideshow">
   <div>
       <?php 
       echo getSlideshow($conn);

       ?>

   </div>
   <div>
    // in here i need to put the 5th - 8th on the list
     </div>

      <div>
    // in here i need to put the 9th - 12th on the list
     </div>

      <div>
    // in here i need to put the 13th - 16th on the list
     </div>

       <div>
    // in here i need to put the 17th - 20th on the list
     </div>


   </div>

JS / jquery代码:

$("#slideshow > div:gt(0)").hide();

setInterval(function() {
  $('#slideshow > div:first')
    .fadeOut(1500)
    .next()
    .fadeIn(1500)
    .end()
    .appendTo('#slideshow');
}, 5000);

php功能:

function getSlideshow($conn) {
    $sql = "SELECT * FROM status WHERE status_image!='noimage.png' ORDER BY likes DESC LIMIT 4";
    $query = mysqli_query($conn, $sql);
    while ($row = $query->fetch_assoc()) {
        echo "
       <div class='imagebox'>
     <img src='images/".$row['status_image']."'>
     </div>";
    }

}

1 个答案:

答案 0 :(得分:1)

使用模数运算符:

function getSlideshow($conn) {
    $sql = "SELECT * FROM status WHERE status_image!='noimage.png' ORDER BY likes DESC LIMIT 4";
    $query = mysqli_query($conn, $sql);
    $i = 0;
    while ($row = $query->fetch_assoc()) {
        if($i % 4 == 0 && $i) echo '</div>';
        if($i % 4 == 0) echo '<div>';
        echo "
        <div class='imagebox'>
            <img src='images/".$row['status_image']."'>
        </div>";
        $i++;
    }
    if($i % 4) echo '</div>';
}

array_chunk功能:

function getSlideshow($conn) {
    $sql = "SELECT * FROM status WHERE status_image!='noimage.png' ORDER BY likes DESC LIMIT 4";
    $query = mysqli_query($conn, $sql);
    $slides = array();
    while ($row = $query->fetch_assoc()) {
       $slides[] = $row;
    }
    foreach(array_chunk($slides, 4) as $chunk) {
        echo '<div>';
        foreach($chunk as $slide) {
            // code here
        }
        echo '</div>';
    }
}