php - 我想创建类似于facebook的状态和评论系统

标签 php mysql

我只是想建立类似于facebook的评论和状态系统。当我给出一个状态时,它的显示和评论有关该状态的显示。但当我给出新的状态时,它不会显示。

<?php 

// show status.

mysql_connect("localhost","root","");
mysql_select_db("saif");
$sql="select id, st from status ";
$result=mysql_query($sql);


while($row= mysql_fetch_array($result))
{
    echo $row['id']. " " .$row['st'];
    echo "<br>";

    //show comment;

    mysql_connect("localhost","root","");
    mysql_select_db("saif");
    $sql="select com from comment ";
    $result=mysql_query($sql);


    while($row= mysql_fetch_array($result))
    {
        echo $row['com'];
        echo "<hr>";    
    }
    // end of show comment

   //new comment area.
   include('textarea.php');
   echo "<hr>"; 
}

?>

最佳答案

好的,这是我的解决方案: 首先,我在数据库中创建两个表并填充它们,然后使用 MySQLi 创建到数据库的连接(不推荐使用 MySQL)。 我选择了所有状态,并为每个状态选择了相关评论。为此,在评论表中,我需要一些东西来在状态和评论之间建立关系。 只是为了了解评论与哪个状态相关。

首先让我们创建并填充表:

SQL:

CREATE TABLE `status` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `st` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `status` (`id`, `st`)
VALUES
    (1, 'This is the first status in your DB, let\'s say \"Hello World\"'),
    (2, 'This is the second status');


CREATE TABLE `comment` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `id_status` int(11) DEFAULT NULL,
  `com` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `comment` (`id`, `id_status`, `com`)
VALUES
    (1, 1, 'This is just a comment to the first status'),
    (2, 1, 'This is another comment to the first status'),
    (3, 2, 'This is a comment to the second status'),
    (4, 1, 'This is the third comment to the first status');

好的,php 文件:

<?php

$con = mysqli_connect("localhost","YourUsername","YourPassword","YourDB");

// Check connection
if (mysqli_connect_errno()) {
        echo "Connection error: ".mysqli_connect_error();
        exit();
}

$sql = "SELECT * FROM status";
$result = $con->query($sql);

while($row = $result->fetch_array(MYSQLI_ASSOC))
{
    echo $row['id']." ".$row['st']."<br>";

//show comment
    echo "<ol>";
    $id_status = $row['id'];
    $query = "SELECT com FROM comment WHERE id_status = $id_status";
    $comments = $con->query($query);
    while($comment = $comments->fetch_array(MYSQLI_ASSOC))
    {
        echo "<li>".$comment['com']."</li>";
    }
    echo "</ol>";
// end of show comment

//new comment area.
    include('textarea.php');
    echo "<hr>";    
}
?>

这就是结果(我的输出中没有文本区域,因为我没有你的 textarea.php)

enter image description here

我希望这会有所帮助。

关于php - 我想创建类似于facebook的状态和评论系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34702675/

相关文章:

sql - 3 MySQL 中的表连接

php - 身份验证返回 false Laravel 4

php - 如何在html中显示textarea字段?

php json解码包含破折号的变量

php - SQL Server查询错误日志

mysql - Access SQL 日期格式不一致

php - MSXML2.XMLHTTP发送数据到PHP脚本

mysql - 将 MySQL 触发器转换为 SQLite

php - JQuery AJAX + PHP 登录

php - mysql_fetch_array 返回重复数据