-1

我想要一个 $startdate 从用户输入日期向后计算 3 天,这 3 天不是假期。

因此,如果用户输入的日期是 10 月 22 日,则 $startdate 将是 10 月 17 日而不是 10 月 19 日。因为 10 月 19 日和 20 日是假期

$i = 1;
do{
    //some code to produce the $startdate
    //...
    //end

    //check if $startdate is holiday or not
    $this->db->where('hl_date',$startdate);
    $query = $this->db->get('php_ms_holiday');

    //if $startdate = holiday, it doesn't count
    if($query->result_array()){
    }
    else{
            $i++;
    }
}while($i <= 3);

if($query->result_array())但是,使用该代码,当在语句中捕获 $startdate 时,我可以在浏览器上不停地加载。只有当我在下面的if($query->result_array())语句中添加类似以下代码时,浏览器才能返回结果:

$i = $i + n; //n is a number starting from 1, or
$i++;

但不是:

$i = $i; //or
$i = $i + 0;

这是为什么?

4

3 回答 3

0

如果这总是正确的:

if($query->result_array()){
}

那么这将永远是真的:

}while($i <= 3); //$i is never adjusted, so it will always be less than 3

由于您说要显示最近 3 天的数据,但如果中间有假期,您不想计算它们。为了解决这个问题,我认为你不应该使用文字“3”,因为它可以调整,它也应该是一个变量。

一个可能的解决方案是有一个变量,例如:

$days_back = 3;

然后尝试计算过去 3 天内(或类似的)假期的数量:

//You can make the holidays to be zero by default

$holidays = someWayToCountHolidays($startDate, $endDate); //You'll obviously have to code this

然后你可以使变量你的while循环再次工作

$num_days = $days_back + $holidays; //$holidays can be zero by default

然后像下面这样为你做while循环:

   if($query->result_array()){
      //Then do what ever you want to do here
   } else {
      //Then do what ever you want to do here
   }

   $i++; //Adjust $i

}while($i <= $num_days);
于 2013-10-16T06:50:15.190 回答
0

if($query->result_array()) will always evaluate to true, if your query is correctly formulated. So also if the number of rows returned is 0. This means that you never ever go to the else. You if-statement should check the number of results returned instead; Furthermore, I personally would prefer a normal for-loop:

for($i = 0; $i < 3)
  {
  //some code to produce the $startdate
  //...
  //end

  //check if $startdate is holiday or not
  $this->db->where('hl_date',$startdate);
  $query = $this->db->get('php_ms_holiday');

  //if $startdate = holiday, it doesn't count
  if [num_rows equals 0] // test num_rows here; Not sure what your class offers here
    {
    $i++; // increment counter
    // do whatever else you want to do
    }
  }
于 2013-10-16T06:42:51.113 回答
0

您只需进行数据库查询,结果解释为 TRUE。也许我错了,但似乎您在每次循环迭代中都进行了相同的数据库查询。因此,请检查您的查询,例如为假期检查添加特定日期。

于 2013-10-16T06:45:18.563 回答