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

时间:2016-01-10 06:14:33

标签: 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>"; 
}

?>

1 个答案:

答案 0 :(得分:1)

好的,这是我的解决方案: 首先,我在我的数据库中创建两个表,然后填充它们,然后使用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

我希望这会有所帮助。