数组和循环几天

时间:2013-08-16 23:07:22

标签: php arrays codeigniter loops

我在创建此功能时遇到问题。我的代码很乱,而且我被卡住了,所以我宁愿不发布它。我宁愿要求一个新的解决方案。

我有一个数组(mysql行),以今天的日期作为条件获取。我想基于前一个数组的数据创建一个新数组,并在今天的日期之前将其插入到数据库中。限制为15.因此,如果此日期之前已有10行,则只插入5,并在下一个日期继续,只要第一个数组中有行。

我正在使用php和代码点火器。

2 个答案:

答案 0 :(得分:0)

我不知道你是如何获取数据的,或者你是如何生成新数据的,但是像这样的东西;

//loop through days that you want to check/update
    //fetch existing data for this day into $data
    if( count( $data ) >= 15 ) continue; //skip days that already have 15+
    for( $x = 0; $x < 15 - count( $data ); $x++ )
    {
        //insert one new row here
    }
//end days loop

答案 1 :(得分:0)

这是我如何做到的,它做了我想做的事情。不确定它是否是实现此功能的最佳方式。在这里,我使用临时阵列进行测试。每个元素都是数据库的新行。使用3作为最大值(而不是15)仅用于测试。

    $subscriptions = Subscription::all(array('conditions' => 'token != "" ', 'order' => 'id asc'));
    $startTime = strtotime('2013-08-15'); 

    $temparray = array();

    $projects = Project::all(array('conditions' => 'end = "'.date("Y-m-d", $startTime).'" '));
    if ($projects){$counter = count($projects);}else{$counter = 0;}

    while (list($key, $value) = each($subscriptions))
    {
        if ($counter == 3)
        {
            do
            {
                $startTime = strtotime('+1 day', $startTime);
                $projects = Project::all(array('conditions' => 'end = "'.date("Y-m-d", $startTime).'" '));
                if (count($projects) < 3)
                    {
                        $counter = count($projects);
                        break;
                    }
            } while ($counter <= 3);

                $temparray[] = $value->date . " " . date("Y-m-d", $startTime);
                continue;   
        }

    $temparray[] = $value->date . " " . date("Y-m-d", $startTime);
    $counter++;
    }