【问题标题】:PHP/OOP Help - logic and design issuePHP/OOP 帮助 - 逻辑和设计问题
【发布时间】:2011-03-07 05:34:15
【问题描述】:

我正在兜圈子,试图找出如何最好地做到这一点,我认为这应该不会太难,但我正在这样做!在我的站点中,每次成员提供一些数据时,我都会将其存储在该成员的条目中。这个想法是,每月连续提交数据的成员将在某个阶段获得奖励。但是有一些参数:在上次访问后 21 天内返回站点的成员不会将其视为新条目,而只是在与前一个条目相同的条目期间再次提交。同样,如果会员在最后一个条目日期后超过 49 天返回网站,则条目​​编号将不是连续的,而是将增加 2,显示条目之间的中断。这就是我想出的方法,以便区分在正确时间范围内填写数据的成员 - 希望这一切都有意义!

对于我的代码/设计问题,谁能帮我在这里改进我​​的代码以及如何最好地添加时间框架检查?这是我的模型,我正在尝试管理条目,以便它返回正确的条目(即一个新条目 - 将最后一个增加一或两个,或者当前期间已经存在的条目) .

任何指针将不胜感激!

//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();

}**

【问题讨论】:

    标签: php model-view-controller oop


    【解决方案1】:

    为什么不编写一个使用时间戳计算时差的函数。

    然后,您将简单地比较差异并相应地执行正确的功能。

    所以...

     <?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
    
    
        }
    
    
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-14
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多