我有用户表 users, ,我存储像 post_count 等等。我想拥有约50个徽章,它将比将来还要多。

因此,我想拥有一个页面,网站成员可以拿到徽章,而不是自动给予他这样的页面。在他单击一个名为SMTH的按钮之后表徽章的ID和USER_ID无法接受两次。

但是我有很多徽章,所以我真的需要放很多徽章吗?您对此有什么建议?如果可能的话,如何使其更加最佳?

谢谢你。

有帮助吗?

解决方案

最佳将是以下内容:

为用户提供一个函数的对象,该函数可以返回使用适当的用户ID初始化的用户特定属性/指标(您可能想使其成为某些元素的单例/静态...):):

<?
class User {
 public function initUser($id) {
  /* initialise the user. maby load all metrics now, or if they 
  are intensive on demand when the functions are called.
  you can cache them in a class variable*/

 }
 public function getPostCount() {
  // return number of posts
 }
 public function getRegisterDate() {
  // return register date
 }
 public function getNumberOfLogins() {
  // return the number of logins the user has made over time
 }
}
?>

具有一个用ID/键初始化的徽章对象,并从数据库中加载依赖项:

<?
class Badge {
 protected $dependencies = array();
 public function initBadge($id) {
  $this->loadDependencies($id);
 }
 protected function loadDependencies() {

  // load data from mysql and store it into dependencies like so:

  $dependencies = array(array(
   'value' => 300,
   'type' => 'PostCount',
   'compare => 'greater',
  ),...);
  $this->dependencies =  $dependencies;

 }
 public function getDependencies() {
  return $this->dependencies;
 }
}
?>

然后,您可以拥有一个控制批处理授予的课程(您也可以在用户内部进行...),并检查依赖项和打印措施失败的依赖项等...

  <?
        class BadgeAwarder {
         protected $badge = null;
         protected $user = null;

         public function awardBadge($userid,$badge) {
          if(is_null($this->badge))  {
           $this->badge = new Badge; // or something else for strange freaky badges, passed by $badge
}
           $this->badge->initBadge($badge);

          if(is_null($this->user))  {
           $this->user = new User;
           $this->user->initUser($userid);
          }
          $allowed = $this->checkDependencies();
          if($allowed === true) {
           // grant badge, print congratulations
          } else if(is_array($failed)) {
           // sorry, you failed tu full fill thef ollowing dependencies: print_r($failed);
          } else {
           echo "error?";
          }
         }
         protected function checkDependencies() {
          $failed = array();
          foreach($this->badge->getDependencies() as $depdency) {
           $value = call_user_func(array($this->badge, 'get'.$depdency['type']));
           if(!$this->compare($value,$depdency['value'],$dependency['compare'])) { 

            $failed[] = $dependency;
           }
          }
          if(count($failed) > 0) {
           return $failed;
          } else {
           return true;
          }
         }
    protected function compare($val1,$val2,$operator) {
     if($operator == 'greater') {
      return ($val1 > $val2);
    }
    }
        }
        ?>

如果您有非常自定义的批次需要奇怪的计算,则可以扩展到此类。

希望我把你带到正确的道路上。未经测试的且充满语法错误。欢迎来到面向对象的编程世界。还想这样做吗?

其他提示

也许将信息扔进桌子上并对此进行检查?如果是基于帖子的数量,则有字段 badge_namepost_count 并以这种方式检查?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top