MYSQL / PHP帮助 - 从原始预订的任一侧的表中选择下一个可用日期

时间:2013-05-14 06:17:52

标签: php mysql date calendar

我已经制作了这张表,其中包含了截至2063年的所有日期,为了保持简单,它包含3个已经预先填充的列,例如如下......

AutoInc |    date    | status

1       | 2013-05-05 | available

2       | 2013-05-06 | available

3       | 2013-05-07 | booked

4       | 2013-05-08 | booked

5       | 2013-05-09 | booked

6       | 2013-05-10 | available

7       | 2013-05-11 | booked

8       | 2013-05-12 | available

现在,检查日期是否可用对我来说已经足够了,但区别在于这个日历将检查他们需要的块中是否有可用的日期(例如,连续4天)日期的任何一侧他们最初进入了。

切入追逐,我已经确定了下一个可用日期在哪个方向,但我需要的是它检查块,即,在桌子上的任一方向连续4天可用的地方。是否可以对此代码进行一些更改?

$previousDate = mysql_query("SELECT * FROM calendar WHERE autoInc < $requestedDate AND status='available' ORDER BY autoInc DESC");

如果您需要更多,请告诉我,


好的,到目前为止的实际故事......

实际表格

AutoIncNo |    BookingDate    | Status

日历分为两部分。

  1. 选择日期的日历

  2. 一个列表菜单,用于选择他们希望留下的夜晚。

  3. 所以我采用原始日期(他们选择的日期)并重新排列格式以适应表格...

    $CalendarDate = str_replace("/", "-", "$CalendarDate");
    
        $QueryDate = date("Y-m-d", strtotime($CalendarDate));
    

    连接数据库......

        include_once('../connect/connectdatabase.php');
    

    运行第一个查询以检查它们所需的日期是否可用。

    $ QueryDate是他们选择的日期

    $ NightsForQuery是他们想要留下的夜晚

    $CalendarQuery = mysql_query("SELECT * FROM BookingsCalendar WHERE BookingDate='$QueryDate' LIMIT 1");
    
        while($row = mysql_fetch_array($CalendarQuery)) {$AutoInc = $row["AutoIncNo"];}
    
    
        $AutoInc2 = $AutoInc + $NightsForQuery - 2;
    
    
        $SelectDates = mysql_query("SELECT * FROM BookingsCalendar WHERE AutoIncNo BETWEEN $AutoInc AND $AutoInc2");
    
        while($row = mysql_fetch_array($SelectDates)) {
    
            $AutoIncNo = $row["AutoIncNo"];
            $BookingDate = $row["BookingDate"];
            $Status = $row["Status"];
    
            if ($Status == 'booked') {
    
                $LastBookedDate = $BookingDate; 
                $LastAutoIncNo = $AutoIncNo; 
                $Handle = 1;
    
            }
    
        } // End - while($row = mysql_fetch_array($SelectDates)) {
    
    
    
    if ($Handle !== 1) {echo 'DATES AVAILABLE >> WRITE BOOKING CODE';}
    

    因此,如果句柄不等于1并且可以预订,但是,如果日期不可用(即$ Handle == 1),我需要检查任何一方(前和在他们希望留下的夜晚状态“可用”之后他们想要的日期......

    所以我打算在任何一个方向建立第一个可用日期,然后我就陷入困境。看看它我肯定你可以运行一个while循环来找到上面代码中的下一个可用块,但不确定如何。

    if ($Handle == 1) {
    
    
        $FirstDateQuery = mysql_query("SELECT * FROM BookingsCalendar WHERE AutoIncNo < $LastAutoIncNo AND Status='available' ORDER BY AutoIncNo DESC LIMIT 1");
    
        while($row = mysql_fetch_array($FirstDateQuery)) {
    
            $AutoIncNo = $row["AutoIncNo"];
            $BookingDate = $row["BookingDate"];
            $Status = $row["Status"];
    
    
            } // End - while($row = mysql_fetch_array($SelectDates)) {
    
    
        $FirstBookedDate = $BookingDate;
    
        echo $FirstBookedDate . ' - ' . $LastBookedDate;
    
    } // End - if ($Handle == 1) {
    

    如果你有更好的方式或者能够适应已经存在的东西,那就太棒了......我的大脑很伤心哈哈。



    ...续

    我把它分成了这个......

    SELECT * FROM BookingsCalendar WHERE Status='available' AND AutoIncNo < $LastAutoIncNo ORDER BY AutoIncNo DESC LIMIT 4
    

    它配对,但是,它分别选择前4行。

    例如,如果有人试图从2013.06.01预订,但由于其在接下来的4天内被“预订”,则上述脚本会运行并显示2013.05.31 - 2013.05.30 - 2013.05.29 - 2013.05。结果是28。

    但如果其中一个日期被预订,它将跳过它并给我下一个(选择接下来的4个满足条件'可用')

    因此,如果说2013.05.29被预订,它将显示2013.05.31 - 2013.05.30 - 2013.05.28 - 2013.05.27错过了预订的那天。

    现在问题是我们需要接下来的4行(未分割/连续/没有日期中断)“可用”。

    有什么想法吗?




    更新:

    问题是它仍会选择接下来的4行,但不会按日期显示它们。因此,如果有人在日期之前或之后搜索了4天,他们将获得结果,但它们将在预订日期之间展开。当他们预订时,需要整整一段时间,看看我的意思是芽?

    也许我并没有很好地解释它,对此感到抱歉,但结果必须是在一块/一起,没有任何“预定日期”。

    完整的查询:

    $FirstDateQuery = mysql_query("SELECT * FROM ((SELECT * FROM BookingsCalendar WHERE Status='available' AND AutoIncNo < $LastAutoIncNo ORDER BY AutoIncNo DESC LIMIT 4)UNION (SELECT * FROM BookingsCalendar WHERE Status='available' AND AutoIncNo > $LastAutoIncNo ORDER BY AutoIncNo ASC LIMIT 4)) as avail_4 ORDER BY avail_4.AutoIncNo DESC")or die(mysql_error());
    
        while($row = mysql_fetch_array($FirstDateQuery)) {
    
    
            $AutoIncNo = $row["AutoIncNo"];
            $BookingDate = $row["BookingDate"];
            $Status = $row["Status"];
            $avail_4 = $row["avail_4"];
    
            echo $BookingDate . ' ' . $avail_4 . ' ' . '&nbsp;&nbsp;&nbsp;';
    
            } // End - while($row = mysql_fetch_array($FirstDateQuery)) {
    

    这有效,但并没有达到目标。




    数据播放:

    这些脚本将按照我的方式设置表格......

        CREATE TABLE `table_setup` (
        `number` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
        PRIMARY KEY (`number`)
    )
        COLLATE='utf8_general_ci'
        ENGINE=MyISAM;
    
    
    
    
    
        INSERT INTO table_setup VALUES(NULL);
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    INSERT INTO table_setup SELECT NULL FROM table_setup;
    DELETE FROM table_setup WHERE NUMBER > 18264;
    
    
    
    
    
        CREATE TABLE `BookingsCalendar` (
        `AutoIncNo` SMALLINT(2) NOT NULL AUTO_INCREMENT,
        `BookingDate` DATE NOT NULL,
        `Status` VARCHAR(10) NOT NULL DEFAULT 'available'
        `InvoiceRefference` varchar(40) NOT NULL,
        `CustomerName` varchar(40) NOT NULL,
        `CustomerEmail` varchar(40) NOT NULL,
        `CustomerPhone` varchar(30) NOT NULL,
        `Address1` varchar(100) NOT NULL,
        `County` varchar(40) NOT NULL,
        `Country` varchar(30) NOT NULL,
        `PostCode` varchar(10) NOT NULL,
        PRIMARY KEY (`AutoIncNo`)
    
    )
        COLLATE='utf8_general_ci'
        ENGINE=MyISAM;
    
    
    
    
    
    INSERT INTO BookingsCalendar (BookingDate)
    SELECT DATE_ADD('2013-05-05', INTERVAL number-1 DAY)
    FROM table_setup
    WHERE NUMBER < 18264;
    
    
    
    
    
    DROP TABLE `table_setup`
    

1 个答案:

答案 0 :(得分:2)

只需从头顶打字。

SELECT * FROM calendar 
WHERE DATE(date) BETWEEN DATE_SUB($requestedDate, INTERVAL 4 DAY) AND DATE_ADD($requestedDate, INTERVAL 4 DAY) 
AND status='available' 
ORDER BY autoInc DESC

HTH

编辑:基于修订问题的新答案

SELECT * FROM (
(SELECT * FROM BookingsCalendar 
WHERE Status='available' AND AutoIncNo < $LastAutoIncNo 
ORDER BY AutoIncNo DESC LIMIT 4)
UNION 
(SELECT * FROM BookingsCalendar 
WHERE Status='available' AND AutoIncNo > $LastAutoIncNo 
ORDER BY AutoIncNo ASC LIMIT 4)
) as avail_4
ORDER BY avail_4.AutoIncNo DESC

这应该有效。我的不好,忘了在内部选择周围添加括号。