can i use while loop inside another while loop in php -
i'm creating comment system in project. want each posted question have comment. able post comment, having problem displaying comment it's respective answers. able display 1 row not row of comment. try use while loop nested inside while echo's each question, hangs. when use if displays first row of comment of each question.
so question how can display them all?
<div class="answer"> <?php include 'db.php'; $sql = "select * answers questionrid in(select id question id='$qid')"; $result = mysqli_query($con,$sql); if($result){ while($ro = mysqli_fetch_assoc($result)){ ?> <div class="a_view"> <pre> <?php echo $ro["answer"]; ?> </pre> </div> <div class="ans_comment"> <?php if($ro["id"]){ $id = $ro["id"]; $sqli = "select * comments answerid='$id'"; $query = mysqli_query($con,$sqli); $row = mysqli_fetch_assoc($query); $num = mysqli_num_rows($query); while($row){ ?> <div><?php echo $row["comments"];?></div> <?php } } ?> </div> <div class="add"><div class="coment">add comment</div> <div id="coment"> <form class="cform" method="post" action="acomment.php"> <textarea type="text" name="comment" class="tcomment" placeholder="add comment here,your required give correction or more information problem"></textarea><br><br> <input type="hidden" value="<?php echo $ro["id"]; ?>" name="userid"> <input type="submit" value="post comment"> </form> </div></div> <?php } }else{ echo "no record"; } ?> <?php $con->close(); ?>
this section made hang
while($row){ ?> <div><?php echo $row["comments"];?></div> <?php }
when use if
, echos 1 row.
instead of
while($row){
do you're doing in while
loop above
while($row = mysqli_fetch_assoc($query)){
the way have now, $row
never changing, , therefore evaluates true
, leaving stuck in loop.
Comments
Post a Comment