检索Mysql数据并按特定顺序打印

时间:2014-08-23 16:32:43

标签: php mysql while-loop

我遇到了一个让我疯狂过去4或5天的问题。我正在建立一个Facebook风格的发布系统,用户可以在他们的时间轴上发帖,该用户的朋友可以评论每个特定的帖子。我和#39;实际上有问题要在php中正确打印。您的帮助将不胜感激。非常感谢您的时间。

'状态'我为所有帖子创建的表包含以下值

id   osid   author  type  data    postdate
1     1     helal    a    Hi...  2014-08-20 
2     1     Abdul    b    Hey..  2014-08-20
3     1     helal    b    Good..     "
4     4     helal    a    Hello..    "
5     4     Irin     b    Hi...      "

所以,基本上,所有新帖都有类型' a'并且所有回复都有类型'而且,所有单独的对话(帖子和回复)都有相同的' osid'因此用户可以在单独的div上看到单独的对话每个帖子附有评论框(后跟对话)

我编写了以下代码,但它没有给我预期的结果。

$sql="SELECT * FROM status WHERE type='a'";
$query=mysqli_query($connect_dude,$sql);
$numrow=mysqli_num_rows($query);
if($numrow>0){

  while($row=mysqli_fetch_assoc($query)){
  $id=$row["id"];
  $osid=$row["osid"];
  $name=$row["author"];
  $data=$row["data"];
  $date=$row["postdate"];

    $query_replies = mysqli_query($connect_dude, "SELECT * FROM status WHERE type='b' AND osid='$id' ");

    $replynumrows = mysqli_num_rows($query_replies);
     if($replynumrows > 0){
        while ($row2 = mysqli_fetch_assoc($query_replies) ) {
        $statusreplyid = $row2["id"];
        $statusreplyosid = $row2["osid"];
        $replyauthor = $row2["author"];
        $replydata = $row2["data"];
        $replydata = nl2br($replydata);
        $replypostdate = $row2["postdate"];
        $replydata = str_replace("&","&",$replydata);
        $replydata = stripslashes($replydata);




        $status_replies .= '<div id="reply_'.$statusreplyid.'" class="reply_boxes"><div><b>Reply by <a href="user.php?u='.$replyauthor.'">'.$replyauthor.'</a> '.$replypostdate.':</b><br />'.$replydata.'</div></div>';


           }
         }



    $statuslist .= '<div id="status_'.$id.'" class="status_boxes"><div><b>Posted by <a href="user.php?u='.$name.'">'.$name.'</a> '.$date.':</b> '.$statusDeleteButton.' <br />'.$data.'</div>'.$status_replies.'</div>';



    if($logged == $username){
    $statuslist .= '<form id="posting1" action="user.php?u='.$logged.'" method="post" enctype="multipart/form-data"><textarea id="replytext" name="replytext" class="replytext" placeholder="write a comment here '.$osid.'"></textarea><input id="hel1" name="hel1" type="hidden" value="'.$osid.'"><input type="submit" id="replyBtn" name="replyBtn" value="reply"></form>'; 
      }

    }


$postbox="<form id='posting' action='user.php?u=$logged' method='post' enctype='multipart/form-data'><input id='hid' name='hid' type='hidden' value='<?php echo $iid;?>' ><textarea id='taxi' name='taxi' rows='15' cols='40' placeholder='Say something to your Buddies'></textarea></br><input id='hel' name='hel' type='submit' value='post'></form>";
 }
}

我已经使用了echoed&#39; $ statuslist&#39;在html部分。

它会在所有帖子中复合所有回复。

期望的结果形式,比如说

    For post no 1      
    helal:Hi...
    Abdul:hello...
    helal:Good....
    "then the comment box(reply text area)"

    For post no 2      
    helal:Hello...
    Irin:Hi...
    "then the comment box(reply text area)"

等等在单独的div中进行单独的对话

1 个答案:

答案 0 :(得分:0)

我说你需要在外循环中重置$status_replies。添加:

$status_replies = '';

两条while行之间的任何地方。