2012-11-26 42 views
0

我在循環數組時遇到了問題,並將它們混合爲一組新數據。第一組數組是房間ID。另一組是日期的範圍。我想每天循環播放一個帶有房間ID的範圍。PHP - 如何循環2組數組並將它們混合在一起?

這裏是我的資源:

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

獲取之間的兩個DAYS函數日期:

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') 
+1

尼斯[sql注入漏洞](http://bobby-tables.com)。希望你喜歡有你的服務器pwn3d。你可能想閱讀[DateTime](http://php.net/datetime)對象,這會節省你很多無意義的日期/字符串操作。 –

+0

@MarcB,如果你看到任何問題或有改善的機會。請不要猶豫,向我展示一些示例代碼。 Thnx – Wilf

回答

0

可能是你使用的輸入的名稱,如HTML表單,並在這種情況下<input name="vid" .. <input name="vid" ..你有麻煩在這裏:

foreach($_POST[vid] as $_vidz) 

你應該使用的名稱與[]<input name="vid[]" .. <input name="vid[]" ..

還是在villas_rate不存在記錄vr_id = 6

+0

Ivan,感謝您的建議。我錯誤地從以前的表單發送vid值。 – Wilf

+0

不客氣。 –

相關問題