我正在尝试为汽车租赁公司制作一个自定义汽车时刻表日历,以便在附件文件中显示白色的所有免费日期和红色的繁忙日子。直到现在我成功地用正确的日期和样式颜色绘制日历,但我也希望在td单元格中显示每个预订的客户名称。如果可能的话,合并预订单元
这是我的工作代码,但无论我尝试什么,我都无法在日期数组中传递客户名称,因此我可以以某种方式识别每个客户的预订日。
$datstart2 = "2017-01-01 00:00:00" ;
$datend2 = "2017-01-31 00:00:00" ;
foreach($cars as $cid){
$query2 = 'select rr.resnumber, DATE_FORMAT( `datestart` , "%Y-%m-%d" ), DATE_FORMAT( `dateend` , "%Y-%m-%d" ), rr.cl_name, rr.totdays from res_cars rc,res_reservations rr where rr.carid="'.$cid.'" and datestart > now() and (datestart BETWEEN "'.$datstart2.'" AND "'.$datend2.'") group by resnumber order by datestart ASC';
$result2=mysql_query($query2) or die($query2);
$num_rows2=mysql_num_rows($result2);
for($j=0;$j<$num_rows2;$j++){
$row2=mysql_fetch_row($result2);
$current = strtotime($row2[1]);
$last = strtotime($row2[2]);
//here I generate array that contains all busy days between datestart & dateend for each reservation
while( $current <= $last ){
$dates1[] = date('Y-m-d', $current);
$current = strtotime($step, $current);
}
}
//here I generate a new array with all days without reservation for January for the car
$dates2 array consists all days in the month
$emptydays=array_diff($dates2,$dates1);
foreach($dates as $date){
//here I draw table cell with different styles if the dy is reserved or not
if(in_array($date,$emptydays)){
echo '<td class="emptyday">.</td>';
} else {
echo '<td class="busyday">X</td>';
}
}
}
问题是我希望来自busyday类型的表格单元格看起来像:
echo '<td class="busyday">Customer names here</td>';
但无论我尝试过什么都没有成功。 请各位帮帮我,并告诉我打印每个预订的客户名称(cl_name)的正确方法,并使其看起来像:
echo '<td class="busyday">Customer names here</td>';
答案 0 :(得分:0)
一种可能的解决方案是将$date
数组更改为而不是仅存储日期,您可以为每个日期/ customer_name存储数组:
$dates = array
(
array("date"=>"2016-12-10", "customer_name"=>"Customer 1"),
array("date"=>"2016-12-15", "customer_name"=>"Customer 2"),
array("date"=>"2016-12-15", "customer_name"=>"Customer 3"),
array("date"=>"2016-12-20", "customer_name"=>"Customer 4")
);
然后你可以访问它:
echo $dates[0]["date"].": ".$dates[0]["customer_name"];
例如,在foreach
循环中:
foreach($dates as $date){
if(in_array($date["date"],$emptydays)){
echo '<td class="emptyday">'.$date["customer_name"].'</td>';
} else {
echo '<td class="busyday">'.$date["customer_name"].'</td>';
}
}
还有一个例子,在你的while循环中,你可以这样做:
array_push($dates, array("date"=>date('Y-m-d', $current),
"customer_name"=>$row2["cl_name"]));
$dates1[] = $dates; // or array_push($dates1, $date);
*您在问题中提到了“cl_name”,因此我将其作为列名称,如果没有,请将其替换为正确的名称。