2009-09-02 107 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); 
    } 
}