0

this is my code

$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>
                    ";
                }

it's actually worked, but all of my data in variabel $num is showed onload and i have to move to another slideindex and it will be normal. This is my script code

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";
}

and this is my html code to move to another slide index

<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>

the <span id="slideindex"> is to show where was the index is

  • Where is your javascript code in your file? In the ``, at the end of the ``, somewhere else? Your code may be executed before the page is fully loaded, which is why it doesn’t hide all at first – Sean Mar 30 '18 at 13:37
  • 1
    Not related, but please note that this code is insecure and should not be used in production environments. If it is a school exercise, it is fine. Otherwise try to read up on sql injection. – Jakub Judas Mar 30 '18 at 13:40
  • @JakubJudas yea it's just for school exercise so don't worry – Eric Anthony Wu Mar 30 '18 at 14:15
  • @Sean well it is put in `` tag – Eric Anthony Wu Mar 30 '18 at 14:16
  • Then you need to add an event listener to wait to run your function *after* the page has fully loaded. See https://stackoverflow.com/a/3842895/689579 – Sean Mar 30 '18 at 14:26

1 Answers1

1

You have 2 fors, in first you're dividing X/3 which doesn't mean you will have only 3. 50/3 = 16. And inside that for you're iterating 6 times for ($i = 0; $i <= 5; $i++)

Why don't you simply

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

Or even

for ($x = 1; $x <= 3; $x++)
Javier S
  • 379
  • 3
  • 13
  • The OP wants to load all data, in 3 blocks of 6, and then `display:none` the 2nd/3rd. Then use javascript to rotate through those blocks showing/hiding them. Your suggested query would require a page reload to get more than the 1st 3. – Sean Mar 30 '18 at 13:43