【发布时间】:2011-12-13 08:17:14
【问题描述】:
我正在使用 PHP 制作一个简单的 cmets 系统。对于我的每条评论,我都想查找发表该评论的用户并获取他们的详细信息。
我的课程如下:
class getuser {
public function __construct($userid) {
$userquery = "SELECT * FROM users WHERE id = ".$userid."";
$userresult = mysql_query($userquery);
while($user = mysql_fetch_array($userresult)){
$this->uid = $user['id'];
$this->username = $user['username'];
$this->avatar = $user['avatar'];
}
}
}
class getcomment {
public function __construct($postid) {
$commentquery = "SELECT * FROM comments WHERE postid = ".$postid."";
$commentresult = mysql_query($commentquery);
while($comment = mysql_fetch_array($commentresult)){
$this->uid = $comment['uid'];
$this->comment = $comment['comment'];
$this->timestamp = $comment['timestamp'];
$this->likes = $comment['likes'];
$this->reported = $comment['reported'];
//get user info for this comment
$this->getuser = new getuser($this->uid);
}
}
}
我在访问我的页面上的这些信息时遇到问题...
$comments = new getcomment($postid);
foreach($comments as $comment){
echo $comment->username."<br>";
}
数据库中有两个具有此帖子 ID 的 cmets。是导致问题的while循环吗?如何返回所有附有相关用户信息的 cmets?
非常感谢提前
【问题讨论】: