duande9301 2016-09-27 08:44
浏览 8

如何使mysql生成的帖子独立于自己

i am retrieving posts from a table called post . I wanted to add a commenting feature to this code, The problem is that the latest posts variables are dominant over others in that if you comment on an older post, the variables of the "latest post(which appears first because of the ORDER BY ID DECS criteria" Someone help me make this posts independent on their own...Thanks

 <?php

    $sqlhash="SELECT * FROM posts ORDER BY id DESC ";
        $result_hash = mysqli_query($conn,$sqlhash);
        while($rowhash = mysqli_fetch_assoc( $result_hash))
        {
            $user = $rowhash['user'];
            $message = $rowhash['post'];
            $time =  $rowhash['time'];
        $id=  $rowhash['id'];
            $tt = date('m/d/Y H:i:s',$time);

    $my_id = $_SESSION['user_id'];




     $sql="SELECT * FROM users WHERE Full_name='$user'  ";
      $result_set = mysqli_query($conn,$sql);
      while($row = mysqli_fetch_assoc( $result_set))
      {

        $select_username = $row['Full_name'];
        $profp = $row['pic'];



    $my_id = $_SESSION['user_id'];


    ?>

<center><div class='post' style='width:48%; height:40%; align:middle; text-align:left; margin:30px 0'>
<img width="70" height="70" src='uploads/<?php echo $profp; ?>'><br><p style='color:#0c88b5'><b>  <?php echo $id ;?><br><?php echo $user ;?>On:<?php echo $tt ; ?><h4><b><?php echo $message 
;?></h4></p><hr>
<?php 

$sqlcom="SELECT * FROM comment WHERE hash='$id' ";
    $result_com = mysqli_query($conn,$sqlcom);
    while($rowcom = mysqli_fetch_assoc( $result_com))
    {
        $usercom = $rowcom['comment'];
        $comenter = $rowcom['user'];

echo "<p style='color:#0c88b5' $comenter</p>". ":" ;
 echo $usercom. "<br>" ;

 }
if (isset($_POST['commen'])) {


$sqlhash= "SELECT * FROM posts WHERE post= '$message' ";
    $result_hash = mysqli_query($conn,$sqlhash);
    while($rowhash = mysqli_fetch_assoc( $result_hash))
    {
        $user1 = $_SESSION['username'];
        $comment = $_POST['commen'];
        $time =  $rowhash['time'];



        $time2 = $_SESSION['time'];



 $sql3 = "INSERT INTO comment (user, comment, hash) VALUES
             ('$user1','$comment', '$id')";

             if($conn->query($sql3) === TRUE) {

header('Location: timeline.php');

}else {
    echo"error";
}
  • 写回答

1条回答 默认 最新

  • dtn55928 2016-09-27 08:59
    关注

    Make some changes at database level in your post table to manage the reply thread on a post.

    1. Add a parent_id column with default null that contains the post id for which the reply or comment is made.
    2. For each comment or reply, put an entry in parent_id column.
    3. To show post, fetch the records having parent_id IS NULL. By this you will get the post only, put order by on id to get in a proper order.
    4. For each post you can retrieve comment or reply by putting a condition like parent_id IS NOT NULL

    This will help you.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错