我似乎无法获得我正在寻找的IF条件

时间:2017-02-21 15:05:33

标签: mysql if-statement while-loop conditional-statements vbulletin

我的功能正常,我似乎无法获得正确的IF条件将其全部包装。我想要实现的只是创建链接列表如果WHILE有任何要显示的内容。

所以如果我在线程ID 3并且它有2个addon线程ID 4,5它将创建:

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

如果我在线程2上并且它没有插件线程ID,则它应该不返回任何内容。

这是我目前无条件的。

$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>';

2 个答案:

答案 0 :(得分:1)

我不玩PHP,所以我会给你一般指导。

只需使用布尔变量来控制何时创建init标记并将其关闭。

 @i_create_a_tag = false;

 while ( something ) {
     -- open the tag only once
     if (!@i_create_a_tag) {
         $post['addons'] = '<ul>';
         @i_create_a_tag = true;
     }

     ....          
 }

 -- close the tag if was open
 if (@i_create_a_tag) {
    $post['addons'] = '</ul>';
 }

答案 1 :(得分:1)

你可以做$addonid->num_rows吗?

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

然后过了一会儿

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