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

标签 php mysql date

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

一切正常,但我想让它可编辑,意味着对于可用日期,应该有一个链接“预订”,对于预订日期,应该有一个链接“未预订”。

我的脚本实际上是从表中获取 ID 和日期并将其存储在数组中(顺便说一句,该表中只有这两列)。然后使用 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>

最佳答案

您需要将 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
}

关于php - 日历脚本 - 在日期列中映射 ID 和日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21805419/

相关文章:

php - 在 MySQL 中获取最后插入的 IDS

mysql - 一份声明中包含两个限制条款?

shell - 如何在shell中创建日期生成器?

java - Joda时间(Java): Hijri to Gregorian conversion inaccurate?

javascript - Extjs 日期列格式

php mysql fetch 在每行的开头添加我的行号

php - 使用php删除mysql中id最高的行

javascript - 单击链接时更改 session 变量

php - 使用 Guzzle 通过 Curl 请求传递客户端证书

mysql - 将参数传递给 mySQL 数据库的 IdbCommand