0

我在循环数组并将它们混合为一组新数据时遇到问题。第一组数组是房间 ID。另一组是日期范围。我想每天在数组中包含房间 ID 的范围内循环。

这是我的资源:

$_rid=array("5","6");
$date_range=getDays('2012-11-30','2012-12-05');
$_sid=md5(time());

获取两天之间的日期函数:

function getDays($strDateFrom,$strDateTo) {
  // takes two dates formatted as YYYY-MM-DD and creates an
  // inclusive array of the dates between the from and to dates.

  // could test validity of dates here but I am already doing
  // that in the main script

  $aryRange=array();

  $iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
  $iDateTo=mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

  if ($iDateTo>=$iDateFrom) {
    array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

    while ($iDateFrom<$iDateTo) {
      $iDateFrom+=86400; // add 24 hours
      array_push($aryRange,date('Y-m-d',$iDateFrom));
    }
  }
  return $aryRange;
}

所以我写道:

foreach($_POST[vid] as $_vidz){//5-6
    foreach($date_range as $val0){  
    //get cost from villas_rate each date
    $sql_rCost="select vr_cost from villas_rate where vr_id='$_vidz'";
    //echo $sql_rCost."<hr />";
    $result_rCost=mysql_db_query($dbname,$sql_rCost);
    while($rec_rCost=mysql_fetch_array($result_rCost)){
        $_rCostDBcost=explode("-",str_replace(",","",$rec_rCost['vr_cost']));   
            $_rate=$_rCostDBcost[$_rtype-1];//rate starts with 0
            $_date=$val0;
            $sql_cBk="insert into booking_customer values('','$_sid','$_vidz','$_rate','$_agc','$_date')";
            echo $sql_cBk."<br />";             
        }
    }
}

结果 应该是好的结果。但它只为数组 $_rid=5 中的一个值循环。

insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-11-30')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-01')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-02')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-03')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-04')
insert into booking_customer values('','e1eb3f2e2c0fe99780c0354fa699a827','5','2012-12-05')
4

1 回答 1

0

May be you used input's names like <input name="vid" .. <input name="vid" .. in your HTML form and in this case you have trouble here:

foreach($_POST[vid] as $_vidz)

You should using names with [] like <input name="vid[]" .. <input name="vid[]" ..

Or in villas_rate didn't exists records with vr_id = 6

于 2012-11-26T14:26:53.840 回答