2010-09-24 26 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並檢查那種方式?