2016-03-21 12 views
-2

veritabanında her satır için (içeriği ile birlikte) yeni bir div oluşturma Çalışma tarzı comments bağlantı olduğunda, yani tıklandığında, ilgili thought_id ile ilişkili tüm yorumlar yüklenecek. İşte Ben bir özellik, kullanıcıların <code>thoughts</code> gönderebilir ve kullanıcıların her <code>thought</code> için <strong>Yorumlara</strong> ekleyebilir sahip

benim user_comments tablosunun yapısı şöyledir:

id 
    body_of_msg 
    comment_posted_by 
    comment_posted_to 
    post_id (which is the id of a thought from the `user_thoughts` table) 

aşağıdaki verileri göz önünde bulundurun:

user_thoughts tablo (1 satır):

id: 184 
thought: "hello, this is a thought from anderson." 

ben de iki satır olduğunu varsayalım user_comments tablo:

id: 1 
    body_of_msg: Comment assigned to thought_id of 184 
    comment_posted_by: conor 
    comment_posted_to: anderson 
    post_id: 184 

    id: 2 
    body_of_msg: Another comment assigned to thought_id of 184 
    comment_posted_by: alice 
    comment_posted_to: anderson 
    post_id: 184 

Sorun: Ben comments bağlantıyı tıkladığında Şu anda, yorumların sadece bir (son açıklama gösteriliyorsa Dolayısıyla bu durumda, Alice'in yorumunu) gösteriliyor. , Bu sadece bana bir çözüm bulmak için mücadele edilebilir, ancak:

$count = mysqli_query ($connect, "SELECT * FROM user_thoughts"); 
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) { 
    $thought_id  = $row['id']; 
} 

ben düşünüyorum Ne: $thought_id gelen

<?php 
    // Get the comments attached to a users post... 
    $get_comm = mysqli_query ($connect, "SELECT * FROM user_comments WHERE post_id='$thought_id' ORDER BY post_id DESC"); 
    $num_of_comments = mysqli_num_rows($get_comm); // get number of comments for each post by post_id 
    // if there are comments for the post, get its content 
    if ($num_of_comments !=0 || $num_of_comments == 0){ 
    while( $comment = mysqli_fetch_assoc ($get_comm)){ 
      $comment_body   = $comment['body_of_msg']; 
      $comment_posted_to  = $comment['comment_posted_to']; 
      $comment_posted_by  = $comment['comment_posted_by']; 
      $removed    = $comment['comment_removed']; 
    } 
    echo "";   
      /** There are other divs and content echo'd here**/ 
      //////////////////////////////////////////// 
      // this is where each comment is displayed 
      echo " 
      <div id='toggleComment$thought_id' class='new_comment' style='display:none;'> 
       <br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php 
        if ($comment_posted_by == $username){ 
          echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";       
         } echo " 
      </div>"; 
      ///////////////////////////////////////////// 
    } 
?> 

:

İşte kod olduğunu Her yorumun diğeriyle çakışması olabilir mi? Yorum özellikim, düşüncelerin altında dinamik olarak görünen yorumlar içeriyordu, bu yüzden bunu elde etmek için Javascript'i kullandım. Sadece bloğun düşünülmesi yeni bir yorum ile değiştiriliyor olabilir? Ben denedi Ne

: İki gösterilecek varken

while( $comment = mysqli_fetch_assoc ($get_comm)){ 
     $comment_body   = $comment['body_of_msg']; 
     $comment_posted_to  = $comment['comment_posted_to']; 
     $comment_posted_by  = $comment['comment_posted_by']; 
     $removed    = $comment['comment_removed']; 
     // this is where each comment is displayed 
     echo " 
     <div id='toggleComment$thought_id' class='new_comment' style='display:none;'> 
      <br/><b><a href = 'profile_page/$comment_posted_by'> $comment_posted_by said</a></b>: $comment_body "; ?><?php 
       if ($comment_posted_by == $username){ 
         echo "<a id='remove_comment' href = '/inc/remove_comment.php'> Delete </a>";       
        } echo " 
     </div>"; 
     } 
    } 

Bu hala sadece bir yorumunu gösterir.

enter image description here

+4

Yorumlar döngüsünde yorumları yankılanmalıdır. Şu anda, her yineleme sırasında değişkenleri ayarlıyorsunuz, ancak döngüden çıktıktan sonra yalnızca sonuncusu divinizde yankılanıyor. –

+0

@AdamKonieska - Bunu denedim, ama hala sadece bir satır gösteriyor. Sorularımı, echo'nun div olduğu yeri tam olarak göstermek için düzenledim. – Freddy

+0

Onlarda 'display: none;' ayarını yapıyorsunuz. Yankılanmadığından emin misiniz? $ Num_of_comments' değerinin değeri nedir? –

cevap

İlgili konular