2009-09-02 49 views
1

我想構建(或查找)一個php報告庫,我可以使用它來允許我的用戶報告任何類型的內容爲壞。實際上,如果他們能夠報告內容一樣好(我想這意味着評分系統),那會更好。所以如果有人知道這樣的圖書館,我很樂意聽到它。用戶報告庫

無論如何,這是我如何想象數據庫的工作,我想知道這聽起來是否正確。

  1. ID - 排
  2. USER_ID的ID - ID的用戶投票/報告
  3. ITEM_ID - 報道(後,其它用戶,鏈接等)項目的唯一表行ID
  4. TYPE - 這涉及到表的名稱(帖子,用戶,鏈接等)
  5. DATE - 報告的日期時間
  6. 價值 - 我想這可能是一個UP投票或投票DOWN

通過包含[TYPE]列,我可以在我的網站上的所有內容類型中使用此投票系統,而不僅僅是一個(如論壇帖子)。通過存儲user_id,我可以確定每個用戶每個項目只能投一個投票/報告。

+1

這是其中的一件事情是很簡單的,和特定應用足夠了,它可能是更容易自己構建它。除非您使用流行的框架或平臺,在這種情況下,可能存在一個模塊,但您需要指定您正在使用的框架/平臺 – 2009-09-02 21:27:49

回答

1

那麼,我繼續前進,就像我說的那樣。弗蘭克是對的,這不是太難。下面是代碼櫃面任何人想用它在支持AR風格DB調用像笨或MicroMVC一個MVC:

/* 
* Multi-table user voting model 
*/ 
class votes extends model { 

    public static $table = 'votes'; 


    function cast($user_id = NULL, $item_id = NULL, $type = NULL, $vote = TRUE) { 

     $where = array(
      'user_id' => $user_id, 
      'item_id' => $item_id, 
      'type'  => $type 
     ); 

     //Try to fetch this row 
     $row = $this->db->get_where(self::$table, $where); 

     //Look for an existing vote row 
     if($row && ! empty($row[0])) { 

      //Fetch row 
      $row = $row[0]; 

      //If they already voted this way... then we're done! 
      if($row->vote == $vote) { 
       return; 
      } 

      //Else update the row to the new vote 
      $row->vote = $vote; 
      $row->date = convert_time(time()); 

      $this->db->update(self::$table, $row, array('id' => $row->id)); 

      return; 
     } 

     //Else create a new vote for this item 
     $row = array(
      'user_id' => $user_id, 
      'item_id' => $item_id, 
      'type'  => $type, 
      'date'  => convert_time(time()), 
      'vote'  => $vote, 
     ); 

     $this->db->insert(self::$table, $row); 
    } 
}