2013-07-08 91 views
-3

基本上,我試圖遍歷所有帖子並找到其合適的標籤。 $ id變量是返回的所有帖子ID的數組。因此,第一個for循環應該爲帖子找到所有標籤,並且如果沒有任何該帖子將標籤設置爲0(對於$ post_tags ['這是帖子ID'])通過多維關聯數組循環並輸出結果

看起來似乎如果沒有帖子的標籤,它會簡單地打印出「沒有指定的標籤」(如預期的那樣)。但是,似乎正在發生的事情是,打印的標籤是來自相應後期PLUS的所有標籤,返回的是之前帖子的標籤。它似乎增加了已經返回的內容。

我真的不明白爲什麼這樣做,並考慮到沒有錯誤提出我覺得很難解決。任何幫助或指導將不勝感激!

if($stmt = $mysqli->prepare("SELECT username, avatar FROM members WHERE id = ? LIMIT 1")){       
// Get the posts tags 
if($tgs = $mysqli->prepare("SELECT tag FROM tags WHERE post_id = ?")){ 
    for($i = 0; $i < count($id); $i++){ 
     $tgs->bind_param('i', $id[$i]); 
     $tgs->execute(); 
     $tgs->store_result(); 
     $tgs->bind_result($tag); 
     while($tgs->fetch()){ 
      $tags[] = $tag; 
     } 
     if($tgs->num_rows > 0){ 
      $post_tags[$id[$i]] = $tags; 
     }else{ 
      $post_tags[$id[$i]] = 0; 
     } 
    } 
    $tgs->close(); 
    for($i = 0; $i < count($id); $i++){    
     $stmt->bind_param('i',$by[$i]); 
     $stmt->execute(); 
     $stmt->store_result(); 
     $stmt->bind_result($username, $avatar); 
     $stmt->fetch(); 
     echo '<ul class="f_ul_subject" style="margin-top:3px"> 
        <li class="f_cat_subject" style="font-size:10px;line-height:12px;">'; 
          for($j = 0; $j < count($post_tags[$id[$i]]); $j++){ 
           if($post_tags[$id[$i]] != 0){ 
            echo '<a class="post_tag" href="http://localhost/Site/NetPerry/forum/search.php?v=' . $post_tags[$id[$i]][$j] . '&amp;tags=true"> 
              <span>' . $post_tags[$id[$i]][$j] . '</span> 
             </a>'; 
           }else{ 
            echo 'No specified tags'; 
           } 
          } 
        echo '</li> 
       </ul>              
      </li>'; 
    } 
} 

}

+0

'$標籤[] = $標籤;'是一個問題 –

+1

當然,太本地化問題,「發現我的錯誤我碼」。 –

+0

感謝您的回覆,它讓我走向了正確的方向,現在看起來很有效。儘管沒有義務回覆,但如果您覺得「找到我的代碼中的錯誤」對於本網站來說是合適的,那麼您沒有任何幫助。雖然,謝謝你,你做到了。 – iyop45

回答

1

$tags[] = $tag;被填滿陣列而沒有排空。這就是標籤不斷堆積的原因。您可以通過聲明數組空解決此之前,取環,就像這樣:

$tags = array(); 
while($tgs->fetch()){ 
... 
+0

我想知道爲什麼這個答案被接受,因爲它不會有任何幫助 –

+0

@YourCommonSense在意詳細說明嗎?我不明白爲什麼 – thibauts

+0

嗯可能是我錯了。我不認爲每個帖子都會有明確的查詢 –