1

所以我有一个脚本,可以显示发布的所有主题以及有关该主题的信息,我想知道是否有人可以帮助我制作它,以便每当有人对其发表评论或编辑帖子时,它会将其撞到第一行回显的查询?

    function getReplies($id){
        $q = @mysql_query("SELECT reply_content, reply_date, reply_by FROM replies WHERE reply_id='$id'");
        if(!$q){
            echo 'Error: '.mysql_error();
        }

        $res = mysql_fetch_array($q);
        $q2 = @mysql_query("SELECT `topic_subject` FROM `topics` WHERE `topic_id`='$id'");
        if(!$q2){
            echo 'Error: '.mysql_error();
        }

        $res2 = mysql_fetch_array($q2);
        if($_SESSION['id'] == $res['reply_by']){
            echo '<div class="panel panel-default">
  <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div>
  <div class="panel-body">
   '.nl2br($res['reply_content']).'
  </div>
                <div class="panel-footer">
                    Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                    on <strong>'.$res['reply_date'].'</strong><br />
                    <a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a>
                </div>
</div>
            </div>';
        } else {
            echo'<div class="panel panel-default">
  <div class="panel-heading"><b>'.$res2['topic_subject'].'</b></div>
  <div class="panel-body">
   '.nl2br($res['reply_content']).'
  </div>
                <div class="panel-footer">
                    Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                    on <strong>'.$res['reply_date'].'</strong><br />
                </div>
</div>
            </div>';
        }

    }

注释: DB中的评论结构

帖子: 数据库中的帖子结构

话题: DB中的主题结构

4

1 回答 1

1

尝试更改您的第一个查询并授予它一个 ORDER BY,也许?

$q = "SELECT   reply_content, reply_date, reply_by
      FROM     replies
      WHERE    reply_id='$id'
      ORDER BY reply_date DESC";

在我看来,您正在使用两个查询,而您实际上只需要使用一个。

UPDATE:优化代码,尽量不要重复,加入查询

function getReplies($id) {

      $query = "SELECT   replies.reply_content,
                         replies.reply_date,
                         replies.reply_by
                         topics.topic_subject
                FROM     replies,
                         topics
                WHERE    replies.reply_id = topics.topic_id
                  AND    replies.reply_id = '$id' " . //<== PS: You should sanitize this $id parameter
              " ORDER BY replies.reply_date DESC";
                // You can replace the replies.reply_date column by whatever you end up with for sorting

      // Important note - Look into mysqli, mysql is deprecated and too old to be used these days.
      $q = @mysql_query($query);

      if(!$q) {
          echo 'Error: '.mysql_error();
      }

      $res = mysql_fetch_array($q);

      $output = '
        <div class="panel panel-default">
          <div class="panel-heading"><b>'.$res['topic_subject'].'</b></div>
            <div class="panel-body">
              '.nl2br($res['reply_content']).'
            </div>
            <div class="panel-footer">
                Posted By <strong><a href="../public.php?id='.$res["reply_by"].'">'.getOwner($res['reply_by']).'</stong></a>
                on <strong>'.$res['reply_date'].'</strong><br />';

      if($_SESSION['id'] == $res['reply_by'])
        $output .= '<a href="edit.php?id='.$id.'">Edit</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="delete.php?id='.$id.'">Delete</a>';

      $output .= '
            </div>
          </div>
        </div>';

      echo $output;

    }
于 2013-10-13T23:35:00.043 回答