PHP的数天内均匀分布结果

时间:2012-10-16 18:42:26

标签: php arrays datetime

我试图在任何天数内以非常具体的方式分散3个查询的结果。每个阵列的每个结果代表一个电话,我希望每天总共有18个电话。

我每天总共需要18个结果,按以下方式划分:

8 $active_costco results
4 $inactive_costso results
3 $bashas results
3 $afs results

$active_costco returns 321 total results
$inactive_costco returns 119 total results 
$bashas returns 64 total results
$afs returns 47 results

我需要每天18个结果的总数,所以如果没有$ afs或$ inactive_costco,请使用$ active_costcos填入18。

这是我目前使用的php(它每天仅将active_costcos划分为8个)

        $active_costco = sql::results($query);

$inactive_costco = sql::results();

$bashas = sql::results("");

$afs = sql::results("");
$date = date('Y-m-d');
$str = 'INSERT pharmacy.dbo.hotlist (employee_id, store_id, follow_up_date, created_at, status, urgency)VALUES';
for ($i = 0; $i < count($active_costco); $i++)
{
    if ($i%8 == 0)
    {
        $date = date('Y-m-d', strtotime($date . '+1 Weekday'));
    }
    $str .= "('0CS',". $active_costco[$i]['id'] . ", '$date', '". date('Y-m-d H:m.s') . "', 1, 3)";
    $str .= ($i == count($active_costco)-1)? '': ',';
    $str .= '<br />';
}
echo $str;

任何帮助将不胜感激。 谢谢, 麦克

1 个答案:

答案 0 :(得分:1)

花了一点时间,这是我提出的解决方案:

$str = 'INSERT pharmacy.dbo.hotlist (employee_id, store_id, follow_up_date, created_at, status, urgency)VALUES';
do 
{
    $date = date('Y-m-d', strtotime($date . " +1 Weekday"));
    $today = array();
    for ($i = 0; $i < 3; $i ++)
    {
        $basha = array_pop($bashas);
        $associated = array_pop($afs);
        if (!empty($basha))
            $today[] = $basha;
        if (!empty($associated))
            $today[] = $associated;
    }
    for ($i = 0; $i < 4; $i++)
    {
        $inactive = array_pop($inactive_costco);
        if (!empty($inactive))
            $today[] = $inactive;   
    }

    $count = 18 - count($today);
    for ($i = 0; $i < $count; $i++)
    {
        $active = array_pop($active_costco);
        if (!empty($active))
            $today[] = $active;
    }

    $calls_left = count($active_costco) + count($inactive_costco) + count($bashas) + count($afs);
    foreach ($today as $v)
    {
        echo "Store ID = " . $v['id'] . " Date = " . $date . "<br />";
    }
}while ($calls_left > 0);

它遍历并弹出每个数组的元素(指定的数字)。如果它是数组是空的(没有什么可以弹出)它不会添加任何东西。然后它计算今天数组中的调用数,从18减去它,并从$ active_costsco中获取剩余的调用。

感谢所有投入的人。

相关问题