-3

我正在为我的网站创建仪表板功能。我无法正确排序。仪表板将包含他们关注的人的所有最近状态更新。我为此使用 MySQL 和 PHP。

Status Table:

id: key value, auto-increments
user: the username of the poster of the status
status: the actual status that was posted
date: an int value, has the time that the status was posted

Users table: (Unneeded rows are excluded)
username: The user's name
following: All of the users that he is following. This is a text field, and is delimited by semicolons.

它需要按发布日期排序。我遇到麻烦的原因是我必须让用户关注的人。有什么帮助吗?

4

1 回答 1

1

在这里我会试一试。我不得不稍微改变一下 sql 表

继承人SQL:

CREATE TABLE IF NOT EXISTS `status` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `user_id` int(3) NOT NULL,
  `user_status` text NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


INSERT INTO `status` (`id`, `user_id`, `user_status`, `date`) VALUES
(1, 1, 'Hi, Im Dave, User One.', '2012-05-03'),
(2, 2, 'Hi, Im Amy, User Two.', '2012-05-01'),
(3, 3, 'Hi, Im Lisa user 3', '2012-05-01'),
(4, 4, 'Hi, Im Joe user 4', '2012-05-02');


CREATE TABLE IF NOT EXISTS `users` (
  `id` int(3) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `following` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


INSERT INTO `users` (`id`, `username`, `following`) VALUES
(1, 'Dave', '2:3'),
(2, 'Amy', '1:4'),
(3, 'Lisa', '1:4'),
(4, 'Joe', '1:2:3');

继承人是php(这假设您已经连接到数据库):

    // Get user 2 (id 2) details from user table
    $res = mysql_query("SELECT * FROM users WHERE id='2'");
    while ($row = mysql_fetch_assoc($res)) {

        $username = $row['username'];
        $following = $row['following'];

    }

    if(!empty($following)) {
        $data = explode(':',$following);

        foreach($data as $user){

            // Now get user 2 (id 2) followers statuses
            $res = mysql_query("SELECT * FROM status WHERE user_id='$user'");
            while ($row = mysql_fetch_assoc($res)) {
                echo $user_status = $row['user_status'].'<br>';
                echo $date = $row['date'].'<br>';

            }

        }
    }

我测试过,它似乎工作得很好

希望这是你需要的:)

于 2012-05-03T01:41:14.537 回答