【发布时间】:2014-06-29 06:58:42
【问题描述】:
好的,所以我将尝试重新解释这个问题,因为似乎没有人理解我的目的。现在是凌晨 3 点,所以这可能是我有点松懈的地方……但我会在这里尽力而为。
我目前正在开发一个帖子和评论部分,用户可以在他们自己或其他用户的墙上发帖,因此其他用户可以对这些帖子发表评论。与社交网络状态或一般论坛相同。我有 3 张桌子...
Users
Statuses
Comments
用户包含标准...
userid
username
first_name
last_name
photo
状态包含有关每个用户发布的主要状态的任何信息,这些状态显示在用户个人资料中。因此,用户 1 可以在用户 2 的个人资料上发布状态。这是状态表设计...
status_id (auto-i)
user_id (The users ID whos profile the post was added too)
sender_id (The user who sent the post or wrote it)
date (Date/Time was sent)
rate (This doesn't matter for a moment)
comments (This will count all the comments and display the number)
status (The actual status written out)
这些表在我的脚本中一起工作得很好,该脚本连接了两个表并显示了用户信息(发布状态的人),例如他们的个人资料照片和姓名等...这是我当前的脚本,在所有...
//// GET STATUSES
$user_id = $profile_data['userid'];
$data = mysql_query("
SELECT
statuses.status_id,
statuses.user_id,
statuses.sender_id,
statuses.date,
statuses.rate,
statuses.comments,
statuses.status,
users.userid,
users.username,
users.first_name,
users.last_name,
users.photo
FROM
statuses
LEFT JOIN
users
ON
statuses.sender_id=users.userid
WHERE
statuses.user_id = '{$profile_data['userid']}'
ORDER BY
`statuses`.`date` DESC
") or die(mysql_error());
while($status = mysql_fetch_array( $data ))//added this
{
$statusid = $status['status_id'];
$date = $status['date'];
$rate = $status['rate'];
$comments = $status['comments'];
$userid = $status['user_id'];
$senderid = $status['sender_id'];
$statusbody = $status['status'];
$username = $status['username'];
$firstname = $status['first_name'];
$lastname = $status['last_name'];
$photo = $status['photo'];
?>
<form action="" method="POST" role="form" enctype="multipart/form-data" id="statusupdate" class="facebook-share-box">
<div class="share">
<div class="panel panel-default">
<div class="panel-heading"><a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><img alt="" align="left" hspace="20" height="70" width="70" src="<? echo 'http://basecentre.co.uk/userimages/',$status["photo"]; ?>"> </a> <font size="+2"><i class="icon icon-comment-o"></i></font> <a href="<? echo 'http://basecentre.co.uk/',$status["username"]; ?>"><font size="+2"><?php echo $status['first_name']; ?> <?php echo $status['last_name'] ; ?></font></a> | <i class="icon icon-clock-o"></i> <a rel="tooltip" href="#" data-original-title="<? echo "". date('F j, Y, g:i a', strtotime($status['date']) + 60*60) .""; ?>"><?php echo "<strong>". date('j F', strtotime($status['date']) + 60*60) ."</strong>"; ?></a></div>
<div class="panel-body">
<div class="">
<?php echo $status['status']; ?>
</div>
</div>
<div class="panel-footer">
<div class="row">
<div class="col-md-7">
Comment | Like
</div>
</div>
</div>
</div>
</div>
</form>
</br>
<?
}
?>
我为 cmets 建立了一个新表。所以我可以去一个用户个人资料,阅读一个“状态”,然后我可以给它添加评论......这是那个新表......
comment_id (auto-i)
status_id (added depending on which status you comment on. If the comment is on status id #5, the same number will be sent to this to connect the comments with the correct statuses)
sender_id (the id of the user who is sending the comment which would be $_session['userid'];
date (date the comment was sent)
rate (Doesn't matter yet)
comment (the actual comment written out).
我需要将新的 cmets 表加入到其他两个表中,不知何故?我尝试添加另一个“左连接”,但没有奏效?而且我对这些东西不太擅长。 我的目标是让每条评论按照发布日期的顺序正确显示在其连接状态上。最近在底部...显示用户信息,以便您知道谁发表了评论。
希望这能给你更多的理解,我希望有人能提供帮助!!抱歉,它很长,但是你们要求提供更多信息...谢谢!
编辑添加数据库记录:
状态表:
CREATE TABLE IF NOT EXISTS `statuses` (
`status_id` int(25) NOT NULL AUTO_INCREMENT,
`user_id` int(25) NOT NULL,
`sender_id` int(25) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`rate` int(25) NOT NULL DEFAULT '0',
`comments` int(25) NOT NULL DEFAULT '0',
`status` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`status_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=30 ;
--
-- Dumping data for table `statuses`
--
INSERT INTO `statuses` (`status_id`, `user_id`, `sender_id`, `date`, `rate`, `comments`, `status`) VALUES
(15, 11, 22, '2014-05-11 21:22:00', 0, 0, 'This is pretty damn cool! '),
(16, 11, 91, '2014-05-11 21:22:35', 0, 0, 'LOL how did you do this shit man?? Alll you gotta do now is add the comments ;) and likes! '),
(14, 11, 22, '2014-05-11 21:21:35', 0, 0, 'Hey budddy how are ya ? '),
(13, 11, 11, '2014-05-11 21:18:10', 0, 0, 'eerer'),
(11, 11, 11, '2014-05-11 21:10:33', 0, 0, 'Ho Ho!'),
(10, 11, 11, '2014-05-11 21:10:27', 0, 0, 'Hey hey ! '),
(28, 11, 11, '2014-05-12 00:47:02', 0, 0, 'LOL just another quick test ;) '),
(29, 22, 22, '2014-05-12 02:30:44', 0, 0, 'I should be able to delete this status... '),
(20, 11, 11, '2014-05-11 21:30:17', 0, 0, 'LOL WINNER'),
(21, 22, 11, '2014-05-11 23:31:18', 0, 0, 'Hey mate :D '),
(19, 11, 11, '2014-05-11 21:24:47', 0, 0, 'Not bad eh guys? ;P ');
评论表:
--
-- Table structure for table `comments`
--
CREATE TABLE IF NOT EXISTS `comments` (
`comment_id` int(100) NOT NULL AUTO_INCREMENT,
`status_id` int(25) NOT NULL,
`user_id` int(25) NOT NULL,
`sender_id` int(25) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`rate` int(25) NOT NULL DEFAULT '0',
`comment` text COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`comment_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
--
-- Dumping data for table `comments`
--
INSERT INTO `comments` (`comment_id`, `status_id`, `user_id`, `sender_id`, `date`, `rate`, `comment`) VALUES
(1, 28, 11, 11, '2014-05-12 01:23:58', 0, 'Hmmm shall we see if we can get this working too!? Comments, here we come! '),
(2, 28, 11, 22, '2014-05-12 02:55:33', 0, 'This is not aidans comment! another username should appear!');
用户表:
CREATE TABLE IF NOT EXISTS `users` (
`userid` int(25) NOT NULL AUTO_INCREMENT,
`first_name` varchar(25) COLLATE latin1_general_ci NOT NULL,
`middle_name` varchar(25) COLLATE latin1_general_ci NOT NULL,
`last_name` varchar(25) COLLATE latin1_general_ci NOT NULL,
`email_address` varchar(50) COLLATE latin1_general_ci NOT NULL,
`username` varchar(25) COLLATE latin1_general_ci NOT NULL,
`password` varchar(255) COLLATE latin1_general_ci NOT NULL,
`photo` varchar(50) COLLATE latin1_general_ci NOT NULL DEFAULT 'default.png',
`date1` varchar(25) COLLATE latin1_general_ci NOT NULL,
`date2` varchar(25) COLLATE latin1_general_ci NOT NULL,
`date3` varchar(25) COLLATE latin1_general_ci NOT NULL,
`birthplace` varchar(50) COLLATE latin1_general_ci NOT NULL,
`gender` varchar(25) COLLATE latin1_general_ci NOT NULL,
`about` varchar(250) COLLATE latin1_general_ci NOT NULL DEFAULT 'This information has not yet been updated.',
`user_level` enum('0','1','2','3','4') COLLATE latin1_general_ci NOT NULL DEFAULT '0',
`signup_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`last_login` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`activated` enum('0','1') COLLATE latin1_general_ci NOT NULL DEFAULT '0',
`location` varchar(30) COLLATE latin1_general_ci NOT NULL,
`premium` enum('0','1') COLLATE latin1_general_ci NOT NULL DEFAULT '0',
`blocked` varchar(5) COLLATE latin1_general_ci NOT NULL DEFAULT '0',
`fr_alert` varchar(6) COLLATE latin1_general_ci NOT NULL DEFAULT '0',
`um_alert` varchar(6) COLLATE latin1_general_ci NOT NULL DEFAULT '0',
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci COMMENT='Membership Information' AUTO_INCREMENT=92 ;
【问题讨论】:
-
您只希望显示有评论的帖子吗?还是你想要一些没有 cmets 的?
-
我们需要查看您当前的查询
-
我希望所有帖子都显示有或没有 cmets。
-
我当前的查询在帖子中