简体   繁体   中英

While Loop Inside a While Loop

I have a comment box wherein users can post comments. These comments will be displayed in a while loop.

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

echo "<div id='comments'>";
echo "<table class='table11'>";
echo "<tr>";
echo "<td class='sss'><img src='img/user.jpg'>";
echo "<p class='p999'>"  .$rowsx['username'];  
echo  "</p> </br>";
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
echo "</td>";
echo "<td>";
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


echo "</td>";

echo "</tr>";
echo "</table>";

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";
echo "</div>";

}

?>

Now other users can also reply to the comments of the user. I want to display their replies below each comments of other users but I can't seem to figure out how to do that.

Should I use another while loop inside the first while loop?

Sorry if I'm using mysql functions I know it's depreciated but its just a school project.

Here is my query for displaying comments:

$display_comments1="Select username,prod_id,comment,DATE_FORMAT(date_posted, '%m/%d/%Y %H:%i:%s' ) AS date_posted from comment where prod_id='$prod_id' order by date_posted DESC";
$resultss1=mysql_query($display_comments1);

if($resultss1 === FALSE) {
die(mysql_error()); // TODO: better error handling
}

Yes you can put the while loop inside the while loop looking like this:

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

echo "<div id='comments'>";
echo "<table class='table11'>";
echo "<tr>";
echo "<td class='sss'><img src='img/user.jpg'>";
echo "<p class='p999'>"  .$rowsx['username'];  
echo  "</p> </br>";
echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
echo "</td>";
echo "<td>";
echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


echo "</td>";

echo "</tr>";
echo "</table>";

echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";
echo "</div>";

   $commentId = $rowsx['id'];
   $replies = mysql_query('SELECT * FROM replies WHERE comment_id = $commentId');

   while ($rowsx=mysql_fetch_array($replies)){

   }

}

?>

create this type code :

<?php

while ($rowsx=mysql_fetch_array($resultss1)){

    print_comment($rowsx)

}

function print_comment($rowsx) {
    echo "<div id='comments'>";
    echo "<table class='table11'>";
    echo "<tr>";
    echo "<td class='sss'><img src='img/user.jpg'>";
    echo "<p class='p999'>"  .$rowsx['username'];
    echo  "</p> </br>";
    echo "<p class='p9date'>" .$rowsx['date_posted']. "</p> ";
    echo "</td>";
    echo "<td>";
    echo "<p class='p9'>" .$rowsx['comment']. "</p> </br></br></br>";


    echo "</td>";

    echo "</tr>";
    echo "</table>";

    echo "<a href='reply1.php?r_comment=".$rowsx['comment']."&prod_id=".$row['prod_id']."'    class='reply_button'> REPLY </a>  ";

    while ($sub_comment_result=mysql_fetch_array($reply)){

        print_comment($sub_comment_result)

    }
    echo "</div>";
}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM