Php 与检查连续数据项的逻辑斗争

Php 与检查连续数据项的逻辑斗争,php,Php,我正在想办法处理今天早上遇到的一个棘手的小问题。我的数据库中有一个entries表,其中存储了关于用户每月条目的详细信息(信息捕获内容)——我希望在每个月过去后增加每个条目的数量(而不是ID)。其想法是使用“数字”字段来识别连续的月度条目,并忽略彼此非常接近的条目 当用户访问站点开始新条目时,我会检查最后一个条目的完成日期,看它是否超过21天前(这是一个有效的月份),然后我会增加这个新条目的“数字”。问题是,我最终可能会得到一系列的条目,它们之间的间隔都不到21天(因此所有条目都有相同的编号),

我正在想办法处理今天早上遇到的一个棘手的小问题。我的数据库中有一个entries表,其中存储了关于用户每月条目的详细信息(信息捕获内容)——我希望在每个月过去后增加每个条目的数量(而不是ID)。其想法是使用“数字”字段来识别连续的月度条目,并忽略彼此非常接近的条目

当用户访问站点开始新条目时,我会检查最后一个条目的完成日期,看它是否超过21天前(这是一个有效的月份),然后我会增加这个新条目的“数字”。问题是,我最终可能会得到一系列的条目,它们之间的间隔都不到21天(因此所有条目都有相同的编号),但总的来说跨度超过21天!我需要找到一些逻辑来处理这个问题——有人有什么想法吗

下面是一个如何存储这些数据的示例,以及我遇到的问题

+------+--------+------------+------------+----------------------------+
| id   | number | initiated  | updated    | last_category_reached      |
+------+--------+------------+------------+----------------------------+
|    4 |      1 | 1277914181 | 1277914320 | complete                   |
|  105 |      2 | 1282639343 | 1283444717 | complete                   |
|  397 |      3 | 1284999429 | 1285001298 | complete                   |
|  404 |      3 | 1287478550 | 1287478631 | complete                   |
|  636 |      3 | 1287479243 | 1287479377 | complete                   |
|  649 |      3 | 1287581361 | 1287581466 | complete                   |
|  652 |      3 | 1287585123 | 1287585365 | complete                   |
|  656 |      3 | 1290185205 | 1290424128 | complete                   |
| 1105 |      3 | 1292421193 | 1292426686 | complete                   |
| 1106 |      3 | 1292426769 | 1292426870 | complete                   |
+------+--------+------------+------------+----------------------------+
我的php逻辑如下

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

        // If part one, user profile is calling the update (passing the next stage as a param)
        if ($stage === 'user/profile/2?s=p_prof&p=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
            else
            {
                // Check if time difference between last visit and current time is less than 49 days and more than 21 days
                if (($last_entry->initiated > time() - 4233600) && ($last_entry->initiated < time() - 1814400))
                {
                    // Within allowed timeframe, ok to increment by one as a new entry
                    $num = $last_entry->number + 1;
                }
                // More than 49 days since last visit
                elseif (($last_entry->initiated < time() - 4233600)) 
                {
                    // Increment by two to break consecutive entries
                    $num = $last_entry->number + 2;
                }
                // Entry is within the last 21 days - if user never finished stages, use last entry created instead of creating a new one
                else
                {
                    // If they are back at the start having completed a full entry the last time, ok to create a new entry - otherwise use the one created the last time
                    if ($last_entry->last_category_reached !== 'complete')
                        $entry = $last_entry;

                    $num = $last_entry->number;
                }

            }

            // 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->user_id = $this->id;
            $entry->save();
        }
        // If it's been more than 49 days since last part completion of an entry, user won't be given option to finish the entry, so no need for time check here
        elseif ($stage !== NULL)
        {
            // This must be a continuation of a form, not the beginning of a new one
            // 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
     * @return 
     */
    public function last_entry()
    {
            return $this
                    ->limit(1)
                    ->data_entries
                    ->current();
    }
public function update\u条目($stage=NULL)
{
//获取为此用户输入的最后一个号码
$last_entry=$this->last_entry();
//如果是第一部分,则用户配置文件正在调用更新(将下一阶段作为参数传递)
如果($stage=='user/profile/2?s=p\u prof&p=2')
{
//只有在这个阶段,我们才能创建一个新条目
$entry=ORM::factory('data_entry');
//如果没有以前的会话,请从1开始
如果($last_entry==FALSE)
$num=1;
//这里我们需要检查自上次提交以来经过的时间段
其他的
{
//检查上次就诊与当前就诊的时差是否小于49天且大于21天
如果($last_entry->initiated>time()-4233600)和($last_entry->initiatednumber+1;
}
//自上次访问以来超过49天
elseif($last_entry->initiatednumber+2;
}
//条目在最近21天内-如果用户从未完成阶段,请使用创建的最后一个条目,而不是创建新条目
其他的
{
//如果他们在上一次完成完整条目后回到起点,请确定创建新条目,否则使用上一次创建的条目
如果($last\u entry->last\u category\u到达!=='complete')
$entry=$last_entry;
$num=$last_entry->number;
}
}
//为新条目保存其余数据
$entry->number=$num;
$entry->initiated=time();
$entry->updated=time();
$entry->last_category_reated=$stage;
$entry->user\u id=$this->id;
$entry->save();
}
//如果从最后一部分完成一个条目到现在已经超过49天了,用户将不能选择完成条目,所以不需要在这里进行时间检查
elseif($stage!==NULL)
{
//这必须是一种形式的延续,而不是新形式的开始
//只需更新到达的阶段并保存
$last\u entry->last\u category\u reated=$stage;
$last_entry->updated=time();
$last_entry->save();
//分配给$entry以返回
$entry=$last_entry;
}
返回$entry;
}
/**
*返回上次数据输入会话的
*@返回
*/
公共函数最后一项()
{
退还$this
->限额(1)
->数据输入
->当前();
}

我将在伪代码中执行的操作:

如果有以前的编号,则采用带有max(编号)和min(id)的条目。 计算此条目的时间与当前时间之间的延迟。 如果少于21天,我就不换号码,如果超过21天,我就换号码


如果应用此方法,则不会得到持续时间超过21天的时段。

将时间戳增加为4233600不会考虑白天的变化,请尝试类似于
time()-(time()-strotime('-49天'))的方法。
这有点太抽象了。正确的逻辑取决于我们没有的关于此应用程序的真正业务需求的信息。原始目标与您提出的解决方案之间存在潜在的巨大脱节,我们无法衡量这一脱节,以了解基于您当前方案的建议是否符合要求,或者是否进一步偏离要求。如果你强迫我放弃一个想法,那就是唯一地标识条目组,而不是重复使用递增的数字。确定最新的条目是否应该是最后一组的一部分。谢谢Dan,你是对的-很难简明扼要地解释!我想@LaGrandMere已经根据我目前的设置找到了我想要的。谢谢,这就是我想要的——今天早上大脑没有功能!在使用max(number)和min(id)进行内部连接时浪费了一些时间,然后意识到我只需要使用orderbynumberdesc,id asc limit 1进行查询