ich eine Funktion haben, wo Benutzer können thoughts
und Benutzer können Kommentare zu jedem thought
Die Funktionsweise Post hinzufügen, dass, wenn die comments
Link geklickt, werden alle damit verbundenen Kommentare geladen thought_id
. HierErstellen eines neuen div (mit Inhalt) für jede Zeile in der Datenbank
ist die Struktur meiner user_comments
Tabelle:
id
body_of_msg
comment_posted_by
comment_posted_to
post_id (which is the id of a thought from the `user_thoughts` table)
Betrachten Sie die folgenden Daten:
user_thoughts
Tisch (1 row):
id: 184
thought: "hello, this is a thought from anderson."
Angenommen, ich habe zwei Reihen in dem user_comments
Tabelle:
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
Problem: Im Moment, wenn ich auf den comments
Link klicke, wird nur einer der Kommentare angezeigt (der letzte Kommentar wird angezeigt, also in diesem Fall Alice 'Kommentar).
Hier ist der Code:
<?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>";
/////////////////////////////////////////////
}
?>
Wo $thought_id
herkommt:
$count = mysqli_query ($connect, "SELECT * FROM user_thoughts");
while ($row = mysqli_fetch_assoc($get_thoughts_from_db)) {
$thought_id = $row['id'];
}
Was ich denke,: eine Lösung finden Dies könnte nur werden mir zu kämpfen, aber, könnte es sein, dass jeder Kommentar den anderen überlappt? Meine Kommentarfunktion umfasste Kommentare, die dynamisch unterhalb des Gedankens erschienen. Daher habe ich Javascript verwendet, um dies zu erreichen. Denken Sie nur, der Block könnte durch einen neuen Kommentar ersetzt werden?
Was ich versucht:
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>";
}
}
Dies zeigt immer noch nur einen Kommentar, wenn es zwei gezeigt werden.
Sie müssen die Kommentare in Ihrer while-Schleife wiederholen. Im Moment setzen Sie die Variablen während jeder Iteration, aber nur die letzte wird in Ihrem Div nach dem Verlassen der Schleife gechattet. –
@AdamKonieska - Ich habe versucht, dass, aber es zeigt immer noch nur eine Zeile. Ich habe meine Frage bearbeitet, um genau zu zeigen, wo ich Echo das Div habe. – Freddy
Sie setzen 'display: none; 'auf ihnen. Bist du sicher, dass sie nicht wiederholt werden? Was ist der Wert von $ num_of_comments? –