phpmysqlwhile-loopduplicatesvbulletin

Why am I returning duplicate results?


I'll start off with this is all new to me, whether or not they are advanced actions, I have only pieced things together from other products and adapted to my needs. The big terms, I don't quite understand, I know the basics and I know what I'm trying to achieve that's about it lol.

So I've been playing with this for a while now and I've managed to get things to work, but I'm now getting duplicate results.

Here's my code:

<?php
$addonid = $db->query_read("
      SELECT drc.threadid AS threadid
      FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
      LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
      ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
      WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
    ");
while ($addons = $db->fetch_array($addonid)) {
    $ci_counter = $db->query_read("
          SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
          WHERE thread.threadid IN (" . $addons['threadid'] . ")
      ");

    while ($counter = $db->fetch_array($ci_counter)) {
        $post['addons'] .= '<li><a href="showthread.php?t=' . $addons['threadid'] . '">' . $counter['threadtitle'] . '</a></li>';
    }
}
?>

What the heck is that mess!? Well it's what I managed to get to work lol, so what does it do? In simple terms, it looks for the threadid in one column, then if its in another tables column it grabs the title column from that table. Then it creates a list link for each of them using variables for the values. It shocked me when it actually worked, and is not what I'm here about but fell free to tell me how I can improve it lol.

The problem is, it's returning double of everything, so instead of getting

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>

I am getting

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>
<li><a href="showthread.php?t=5">result 2</a></li>

Why is this? What can I do to solve it?

I have removed the query from the loop but now its only returning duplicates of the first column it finds (which is 4)

<?php
$addonid = $db->query_read("
      SELECT drc.threadid AS threadid
      FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
      LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
      ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
      WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
    ");
$addons = $db->fetch_array($addonid);
$ci_counter = $db->query_read("
          SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
          WHERE thread.threadid IN (" . $addons['threadid'] . ")
      ");

while ($counter = $db->fetch_array($ci_counter)) {
    $post['addons'] .= '<li><a href="showthread.php?t=' . $addons['threadid'] . '">' . $counter['threadtitle'] . '</a></li>';
}
?>

is returning:

<li><a href="showthread.php?t=4">result</a></li>
<li><a href="showthread.php?t=4">result</a></li>

Solution

  •     $addonid = $db->query_read ("
          SELECT drc.threadid AS threadid
          FROM `" . TABLE_PREFIX . "modsys_settings` AS drc
          LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread
          ON(drc.mod_addons=" . $threadinfo['threadid'] . ")
          WHERE thread.threadid IN (" . $threadinfo['threadid'] . ")
        ");
        while ($addons = $db->fetch_array ($addonid)) {    
            $ci_counter = $db->query_read ("
              SELECT drc.mod_addons AS addon, drc.threadid, thread.threadid AS threadid, thread.title AS threadtitle
              FROM `" . TABLE_PREFIX . "modsys_settings` AS drc 
              LEFT JOIN `" . TABLE_PREFIX . "thread` AS thread ON(drc.mod_addons=" . $threadinfo['threadid'] . ") 
              WHERE thread.threadid IN (" . $addons['threadid'] . ")
          ");
    
          $counter = $db->fetch_array ($ci_counter);
          if($counter != ''){
          $post['addons'] .= '<li><a href="showthread.php?t=' .$addons['threadid'] . '">'. $counter['threadtitle'] .'</a></li>';
          }
    
        }