我正在为我的网站创建仪表板功能。我无法正确排序。仪表板将包含他们所关注的人员的所有最新状态更新。我正在使用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.
需要按发布日期排序。我遇到麻烦的原因是因为我必须得到用户关注的人。有什么帮助吗?
答案 0 :(得分: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>';
}
}
}
我测试了它似乎工作得很好
希望这是你所需要的:)