2010-06-23 24 views
2

我正在努力研究如何最好地做到這一點,我認爲它不應該太難,但我是這樣做的!在我的網站中,每當成員提供一些數據時,我都會將其存儲在該成員的Entry中。這個想法是,每個月連續提交數據的成員將在某個階段獲得獎勵。但有一些參數:在上次訪問後的21天內返回該網站的會員不會將其計爲新條目,而只是與上一次相同的入場期的另一次提交。同樣,如果該成員在最後一次入境日期後超過49天返回該網站,則入境號碼將不連續,但會增加2,表明入場券之間有中斷。這是我想出來的,以便在正確的時間範圍內填寫數據的成員之間進行區分 - 希望這一切都合情合理!PHP/OOP幫助 - 邏輯和設計問題

對於我的代碼/設計問題,任何人都可以幫助我改進我的代碼,以及如何在時間範圍檢查中最好地添加?這是我的模型,我試圖去管理條目,以便它返回正確的條目(即新條目 - 由最後一條或兩條增加,或者從當前期間已經存在的條目遞增) 。

任何指針將非常感激!

//example call from a controller after successful form submission - $this->entry is then passed around for use within that session 
$this->entry = $this->pet->update_entry('pet/profile/2'); 


public function update_entry($stage = NULL) 
{ 
    //get last number entered for this pet 
    $last_entry = $this->last_entry(); 

    //if part one, pet profile is calling the update (passing the next stage as a param) 
    if ($stage === 'pet/profile/2') 
    { 
     //only at this stage do we ever create a new entry 
     $entry = ORM::factory('data_entry'); 

     //if no previous sessions, start from 1 
     if ($last_entry === FALSE) 
      $num = 1; 
     //here we need to check the time period elapsed since the last submission, still to be ironed out 
     //for now just increment each time, but this may change 
     else 
      $num = $last_entry->number + 1; 

     //save the rest of the data for a new entry 
     $entry->number = $num; 
     $entry->initiated = time(); 
     $entry->updated = time(); 
     $entry->last_category_reached = $stage; 
     $entry->pet_id = $this->id; 
     $entry->save(); 
    } 
    elseif ($stage !== NULL) 
    { 
     //echo $stage; correctly prints out stage 
     //this must be a continuation of a form, not the beginning of a new one 
     //timeframe checks to be added here 
     //just update the stage reached and save 
     $last_entry->last_category_reached = $stage; 
     $last_entry->updated = time(); 
     $last_entry->save(); 
     //assign to $entry for return 
     $entry = $last_entry; 
    } 

    return $entry; 
} 

/** 
* 
* Returns the the last data entry session 
*/ 
public function last_entry() 
{ 
     return $this 
        ->limit(1) 
        ->data_entries 
        ->current(); 

}** 

回答

2

你爲什麼不寫一個函數來計算時間差使用時間戳。

然後,您將簡單地比較差異並相應地執行正確的功能。

所以......

<?php 
     class time_checker{ 
     function difference($last_entry){ 

     $now = time(); 
     $difference = $now - $last_entry; 

     return difference; 

     } 

    } 

    //Let's use the class just before the entry is stored 

    $day = 24 * 60 * 60; //Number of seconds in a day because we are using timestamps 
    $last_entry = get_last_entry($user_id);// Have a function that gets the last entry 
    $time_check = new time_checker; 
    $time_since_last_entry = $time_check->difference($last_entry); 

    $period_1 = 21 * $day; //21 day period 
    $period_2 = 49 * $day; //49 day period 
    if($time_since_last_entry < $period_1){ 

    //This is what we'll do if there have been less than 21 days since last entry 


    } 

    if($time_since_last_entry > $period_2){ 

    //This is what we'll do if there have been more than 49 days since last entry 


    } 


    ?>