为什么我的所有数据都显示出来,即使我已经将条件显示在幻灯片中每3个数据显示一次?

时间:2018-03-30 13:26:16

标签: javascript php for-loop if-statement onload

这是我的代码

$produk = $_GET['produk'];
                $sql = "select * from tbproduk where namaproduk = '$produk' ";
                $query = mysqli_query($con,$sql) or die("error $sql");
                $num = mysqli_num_rows($query);
                echo $num;
                if(!empty($num)) {
                    for ($x = 1; $x <= $num / 3; $x++) {
                        echo '<div class="w3-content w3-display-container  mySlides">
                <div class="row">';
                        for ($i = 0; $i <= 5; $i++) {
                            $result = mysqli_fetch_array($query);
                            $namaproduk = $result['namaproduk'];
                            $harga = $result['harga'];
                            $pembeli = $result['pembeli'];
                            if(!empty($harga)) {
                                echo '<div class="col-4">
                             <img class="gbr"/>
                             <span> ' . $harga . ' </span><button class="tengbr" onclick="beli(' . $namaproduk . ',' . $pembeli . ')">Beli</button>
                            </div>';
                            }else{
                                echo "";
                            }
                        }
                        echo '</div>
                        </div>';
                    }
                }else{
                    echo "
                    <script>
                    alert('tidak ada produk yang dimaksud');
                    document.getElementsByClassName('navigasi').style.display = 'none';
                    </script>
                    ";
                }

它确实有效,但我在变量$num中的所有数据都显示为onload,我必须移动到另一个slideindex,这将是正常的。这是我的脚本代码

var slideIndex = 1;
showDivs(slideIndex);
function plusDivs(n) {
    showDivs(slideIndex += n);
    document.getElementById('slideindex').innerText = slideIndex;
}

function showDivs(n) {
    var i;
    var x = document.getElementsByClassName("mySlides");
    if (n > x.length) {slideIndex = 1}
    if (n < 1) {slideIndex = x.length}
    for (i = 0; i < x.length; i++) {
        x[i].style.display = "none";
    }
    x[slideIndex-1].style.display = "block";
}

这是我移动到另一个幻灯片索引的HTML代码

<div class="navigasi">
<a style="border: 1px solid black;padding: 10px;background-color: grey;color: white;cursor: pointer;" onclick="plusDivs(-1)">&#10094;</a>
<span id="slideindex"></span>
<a style="border: 1px solid black;padding: 10px;background-color: grey;color: white;cursor:pointer;" onclick="plusDivs(1)">&#10095;</a>

<span id="slideindex">将显示索引的位置

1 个答案:

答案 0 :(得分:1)

你有2个人,首先你要划分X / 3,这并不代表你只有3. 50/3 = 16.并且在你的内部为你重复6次for ($i = 0; $i <= 5; $i++)

为什么不简单地

 $sql = "select * from tbproduk where namaproduk = '$produk' LIMIT 0,3";

甚至

for ($x = 1; $x <= 3; $x++)