我使用PHP創建了一個簡單的評論系統。隨着我的每一條評論,我想查找發表該評論的用戶並獲得他們的詳細信息。在PHP中使用MYSQL查找訪問類
我班如下:
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>";
}
有在DB兩點意見與這個帖子ID 。它是導致問題的while循環嗎?我如何返回所有評論以及相關的用戶信息?
提前
感謝您的答覆,我將如何改進的地方,我上面寫的?以及如何循環訪問「對象」? – Tim
我只是更新了一個例子,實際上你有更多的問題,而不僅僅是我提到的問題。你應該花一些時間閱讀一些OO設計的例子,這會幫助你更好地理解。 – ajreal
如何在不使用$ comment的情況下執行此操作[1] - > getuser-> username?沒有數組編號它似乎不工作... – Tim