2010-09-24 53 views
1

我有用户的表users,其中我存储信息如post_count等。我希望拥有50张徽章,这将会比以后更多。使用PHP和MYSQL实现不自动徽章

所以,我想有一个页面,网站的成员可以去拿徽章,而不是像SO一样自动给他。当他点击一个名为smth的按钮,比如“Take'Made 10 posts'badge'时,系统会检查他是否发布了10个帖子,并且没有此徽章,如果没有问题,请给他徽章并插入新的列出徽章的id和user_id,该成员无法将其重复两次。

但我有这么多的徽章,所以我真的需要把这么多如果是检查所有徽章?你对此有何建议?如果甚至有可能,我怎样才能使它更优化?

谢谢。

+0

查看http://stackoverflow.com/questions/3764979/a-easier-or-better-way-of-doing-this – 2010-09-24 14:49:17

回答

2

优化将是恕我直言如下:

具有与功能的用户的对象返回您用正确的用户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); 
    } 
    } 
     } 
     ?> 

如果你有非常自定义的批次需要奇怪的计算。

希望我带给你正确的轨道。 没有经过测试和可执行的语法错误。 欢迎来到面向对象编程的世界。还想要这样做吗?

+0

嘿它帮助了我很多。只是经历了答案,找到了完美的答案。谢谢。 – Astha 2011-09-29 17:14:20

0

也许把信息扔进一张桌子,并检查它?如果它是根据帖子数量计算的,那么字段为badge_namepost_count并检查那种方式?