简体   繁体   中英

PHP while loop duplicates one MySQL result

I apologize on the title as it's hard to word. All of the results look perfectly fine, until the end. After 25 items, the very first item repeats itself like 20 times and for the life of me I can't find any similar issues on here.

This is my query:

$sql = "SELECT DISTINCT NewsID, Link, Title, Thumbnail FROM `scinews` WHERE Subject = '".$articles."' AND Page = ".$page;
$result = $conn->query($sql);
while ($row = mysqli_fetch_row($result)) {                         
    
    echo '<div class="card">'.$row[3].'<p><div class="card-title">'.$row[2].'</p>
        <p><a href="'.$row[1].'"> [Read More]</a></p></div></div>';
}

If I remove that echo and replace it with this:

echo '<a href="'.$row[1].'">'.$row[2].'</a><br>';

The issue no longer exists. I thought the issue was related to MySQL so I tried switching from mysqli_fetch_assoc to mysqli_fetch_row, and even setting a LIMIT on the mysql command. Neither did anything. It seems to be related to the DIVs, but I'm just guessing a this point. Any help appreciated.

I was able to solve the problem.

There was a function in the javascript that was cloning the "card" class:

function cloneCards(count) {

  var main = document.querySelector("main");
  var card = document.querySelector(".card");

  for (var i = 0; i < count; i++) {
    main.appendChild(card.cloneNode(true));
  }  
}

The function was called when the user scrolled down and the header would shrink itself. So it was duplicating a card already created 25 times.

CloneCards(25);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM