日历脚本 - 在日期列中映射ID和日期

时间:2014-02-16 00:00:09

标签: php mysql date

我正在为宾馆创建日历。它只是告诉预订或可用的日期。 绿色为可用,红色为预订。

一切都很好但我想让它可编辑意味着可用日期应该有一个链接" Book It"对于预订日期,应该有一个链接"未预订"。

我的脚本实际上从表中获取ID和日期并存储在一个数组中(该表中只有这两列BTW)。然后使用 in_arrary 函数,它只会为表中找到的所有日期生成红色,并将所有其他日期保留为绿色。

我很难映射或填充ID和日期值,任何人都可以帮助我。

简而言之,我希望在日期列上打印ID和日期值,以便我可以建立链接。对于预订日期, ID 应传递到下一页,以便我可以将其删除并再次使用。对于可用日期,日期应该在下一页上传递,因此我在表格中添加该日期并将其设为预订。

这是我的完整脚本:

<?php

error_reporting(0);
include("config.php");

/// get current month and year and store them in $cMonth and $cYear variables
(intval($_REQUEST["month"])>0) ? $cMonth = $_REQUEST["month"] : $cMonth = date("n");
(intval($_REQUEST["year"])>0) ? $cYear = $_REQUEST["year"] : $cYear = date("Y");

if ($cMonth<10) $cMonth = '0'.$cMonth;

// generate an array with all unavailable dates
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE `date` LIKE '".$cYear."-".$cMonth."-%'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
$unavailable[] = $row["date"];
}


// calculate next and prev month and year used for next / prev month navigation links and store them in respective variables
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = intval($cMonth)-1;
$next_month = intval($cMonth)+1;

// if current month is Decembe or January month navigation links have to be updated to point to next / prev years
if ($cMonth == 12 ) {
$next_month = 1;
$next_year = $cYear + 1;
} elseif ($cMonth == 1 ) {
$prev_month = 12;
$prev_year = $cYear - 1;
}
?>
<table style="width:100%; text-align: center;">
<tr>
    <td class="mNav"><a href="javascript:LoadMonth('<?php echo $prev_month; ?>', '<?php echo $prev_year; ?>')">&lt;&lt;&nbsp;Prev</a></td>
    <td colspan="5" class="cMonth"><?php echo date("F, Y",strtotime($cYear."-".$cMonth."-01")); ?></td>
    <td class="mNav"><a href="javascript:LoadMonth('<?php echo $next_month; ?>', '<?php echo $next_year; ?>')">Next&nbsp;&gt;&gt;</a></td>
</tr>
<tr>
    <td style="width:16%;" class="wDays">M</td>
    <td style="width:14%;" class="wDays">T</td>
    <td style="width:14%;" class="wDays">W</td>
    <td style="width:14%;" class="wDays">T</td>
    <td style="width:14%;" class="wDays">F</td>
    <td style="width:14%;" class="wDays">S</td>
    <td style="width:14%;" class="wDays">S</td>
</tr>
<?php 
$first_day_timestamp = mktime(0,0,0,$cMonth,1,$cYear); // time stamp for first day of the month used to calculate 
$maxday = date("t",$first_day_timestamp); // number of days in current month
$thismonth = getdate($first_day_timestamp); // find out which day of the week the first date of the month is
$startday = $thismonth['wday'] ; // 0 is for Sunday and as we want week to start on Mon we subtract 1
if (!$thismonth['wday']) $startday = 7;


for ($i=1; $i<($maxday+$startday); $i++) {


if (($i % 7) == 1 ) echo "<tr>";

if ($i < $startday) { echo "<td>&nbsp;</td>"; continue; };

$current_day = $i - $startday + 1;

(in_array($cYear."-".$cMonth."-".$current_day,$unavailable)) ? $css='booked' : $css='available'; // set css class name based on date availability


if ($css == 'booked') { 
echo "<td class='".$css."'>". $current_day . "<br/>" . "Booked" . "</td>";
}
elseif ($css == 'available') {
echo "<td class='".$css."'>". $current_day . "<br/>" . "Available" . "</td>";
}

if (($i % 7) == 0 ) echo "</tr>";

}
?> 
</table>

1 个答案:

答案 0 :(得分:0)

您需要将id添加到$unavailable数组,以便稍后将其添加到链接中。最简单的方法是将它用作数组键 -

while ($row = mysql_fetch_assoc($sql_result)) {
   $unavailable[$row["id"]] = $row["date"];
}

然后,当您构建每一天的单元格时,使用array_search()获取预订日期的数组键,并使用可用日期的日期 -

if ($css == 'booked') { 
    echo "<td class='".$css."'>". $current_day . "<br/>" . "Booked" . "<br/>" . "<a href='scheduler_page.php?id=".array_search($cYear."-".$cMonth."-".$current_day,$unavailable)."'>Unbook</a>" . "</td>";
}
elseif ($css == 'available') {
    echo "<td class='".$css."'>". $current_day . "<br/>" . "Available" . "<br/>" . "<a href='scheduler_page.php?date=".$cYear."-".$cMonth."-".$current_day."'>Book it</a>" . "</td>";
}

然后在您的下一页上,我的示例使用了scheduler_page.php,您可以$_GET确定要取消预定或预订 -

if(isset($_GET['id'])){
   // unbook the date
}
elseif(isset($_GET['date'])){
   // schedule the date
}