2011-12-13 51 views
0

我使用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循環嗎?我如何返回所有評論以及相關的用戶信息?

提前

回答

1

while循環是好的,什麼是不罰款是您getcomment構造,
因爲你是在循環過程覆蓋每個$this財產。

此外,$comments是一個對象,而不是一個數組。
可能的解決辦法: -

Constrcutor: -

$this->comments = array(); 
$idx = 0; 
while($comment = mysql_fetch_object($commentresult)) 
{ 
    $this->comments[$idx] = $comment; 
    $this->comments[$idx]->getuser = new getuser($comment->uid); 
    ++$idx; 
} 

訪問: -

$obj = new getcomment($postid); 
foreach($obj->comments as $comment) 
{ 
    echo $comment->getuser->username."<br>"; 
} 
+0

感謝您的答覆,我將如何改進的地方,我上面寫的?以及如何循環訪問「對象」? – Tim

+0

我只是更新了一個例子,實際上你有更多的問題,而不僅僅是我提到的問題。你應該花一些時間閱讀一些OO設計的例子,這會幫助你更好地理解。 – ajreal

+0

如何在不使用$ comment的情況下執行此操作[1] - > getuser-> username?沒有數組編號它似乎不工作... – Tim

0

非常感謝也許它會更好,如果你想創建一個「的getUser」的功能,而不是一個性質的getUser。或者至少不要將類和屬性命名爲「getUser」。

而你真的不應該有多個記錄共享相同的索引。使用UNIQUE索引。還要在註釋表的userid字段上分配一個外鍵。

0

你做了它的方式,你應該echo'ing $ comment-> getuser- >用戶名。

你在這裏使用的類結構有點奇怪。我建議如下:

<?php 

class Comment { 

    public $uid, $comment, $timestamp, $likes, $reported; 
    protected $user; 

    public function __construct($result_data) 
    { 
    foreach ($result_data as $k => $v) 
    { 
     $this->$k = $v; 
    } 
    } 

    public function getUser() 
    { 
    if (empty($this->user)) 
    { 
     $userquery = "SELECT * FROM users WHERE id = ".$this->uid.""; 
     $userresult = mysql_query($userquery); 
     $this->user = mysql_fetch_object($userresult); 
    } 

    return $this->user; 
    } 

    public static function get_by_post_id($postid) 
    { 
    $commentquery = "SELECT * FROM comments WHERE postid = ".$postid.""; 
    $commentresult = mysql_query($commentquery); 
    $results = array(); 
    while($comment_data = mysql_fetch_array($commentresult)) 
    { 
     $results[] = new Comment($comment_data); 
    } 
    return $results; 
    } 
} 

然後使用它:

<?php 
$comments = Comment::get_by_post_id($postid); 
foreach ($comments as $comment) 
{ 
    echo $comment->getUser()->username; 
}