Search code examples
mysqlif-statementwhile-loopconditional-statementsvbulletin

I can't seem to get the IF condition I'm looking for


I have the function working properly, I just cant seem to get the right IF condition to wrap it all in. What I am trying to achieve is only create the link list IF the WHILE has anything to display.

so if I'm on thread id 3 and it has 2 addon thread ids 4,5 it will create:

<ul>
  <li><a href="showthread.php?t=4">Link 4</a></li>
  <li><a href="showthread.php?t=5">Link 5</a></li>
</ul>

if I'm on thread 2 and it has no addon thread ids it should return nothing.

This is what I currently have with no conditions.

$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'] . ")
");
  $post['addons'] = '<ul>';

  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);
    $post['addons'] .= '<li><a href="showthread.php?t=' .   $addons['threadid'] . '">'. $counter['threadtitle'] .'</a></li>';
  }

  $post['addons'] .= '</ul>';

Solution

  • Can you do something like $addonid->num_rows ?

    $show_list = $addonid->num_rows;
    if($show_list)
         $post['addons'] = '<ul>';
    

    And then after the while

    if($show_list)
        $post['addons'] = '</ul>';