Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据预订系统中的可用房间打印HTML表_Php_Mysql_Sql_Mysqli - Fatal编程技术网

Php 根据预订系统中的可用房间打印HTML表

Php 根据预订系统中的可用房间打印HTML表,php,mysql,sql,mysqli,Php,Mysql,Sql,Mysqli,大家好, 我试图打印一个HTML表格;在本例中,给定两个日期,从7月1日到7月31日,它将在标题上打印日期。然后,每一排都是一个房间。 然后,当天预订房间时,单元格的背景颜色将为红色,如果没有,则将保持白色 我在一次检查房间是否应该打印成红色的IF中迷失了方向。 到目前为止,我得到了以下输出: 我正在努力实现这一目标: 基于数据库查询输出: SELECT idsRoom, checkin, checkout FROM bookings WHERE checkout >= '2014-07-

大家好,

我试图打印一个HTML表格;在本例中,给定两个日期,从7月1日到7月31日,它将在标题上打印日期。然后,每一排都是一个房间。 然后,当天预订房间时,单元格的背景颜色将为红色,如果没有,则将保持白色

我在一次检查房间是否应该打印成红色的IF中迷失了方向。 到目前为止,我得到了以下输出:

我正在努力实现这一目标:

基于数据库查询输出:

SELECT idsRoom, checkin, checkout
FROM bookings
WHERE checkout >= '2014-07-01' AND checkin <= '2014-07-31'

+---------+------------+------------+
| idsRoom | checkin    | checkout   |
+---------+------------+------------+
| 2       | 2014-06-27 | 2014-07-02 |
| 4       | 2014-07-08 | 2014-07-09 |
| 6,7,8   | 2014-07-18 | 2014-07-22 |
| 14      | 2014-07-31 | 2014-08-02 |
+---------+------------+------------+
4 rows in set (0.00 sec)
是的,每个预订都有一个签到和一个结帐,而且它必须至少包含一个或多个房间,这就是为什么ids 6、7、8。我使用的是PHP5.5.13、MySQLi编写的语句和Maria DB 10.0.12。 这是PHP代码段,将打印输出表:

<?php
/* This will be the received POST date in the future */
$desdeP = "2014-07-01";
$hastaP = "2014-07-31";

/* Database Connection and DateTime objects */
$conn = new MySQLi("localhost","user","password","database"); //Seriusly? Nope ;)
$desde = DateTime::createFromFormat("Y-m-d",$desdeP);
$hasta = DateTime::createFromFormat("Y-m-d",$hastaP);
?>
<!-- Table with date headers -->
<table border="1" cellpadding="0" cellspacing="0" style="min-width:100%;min-height:100%;">
<tr>
    <td>&nbsp;</td>
    <?php
    while($desde<=$hasta){
        echo '<td>'.$desde->format("d-m-Y").'</td>';
        $desde->modify("+1Day");
    }
    ?>
</tr>
<?php
/* Query all the rooms */
$stmt = $conn->prepare("SELECT id, name FROM rooms ORDER BY id;");
$stmt->execute();
$rooms = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

/* Query all the bookings in the given dates */
$stmt = $conn->prepare("SELECT idsRoom, checkin, checkout
FROM bookings
WHERE checkout >= ? AND checkin <= ?;");
$stmt->bind_param("ss",
    $desdeP,
    $hastaP);
$stmt->execute();
$bookings = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

foreach($rooms as $k=>$v){ // Every row is a room
    echo '<tr>';
    echo '<td>'.$v['name'].'</td>';
    $desde = DateTime::createFromFormat("Y-m-d",$desdeP);
    $hasta = DateTime::createFromFormat("Y-m-d",$hastaP);
    while($desde<=$hasta){ // For every row, check in the bookings list if available or not
        echo '<td';
        foreach ($bookings as $k2=>$v2){
            $checkin = DateTime::createFromFormat("Y-m-d",$v2['checkin']);
            $checkout = DateTime::createFromFormat("Y-m-d",$v2['checkout']);
            /* HERE remains my question: What mega IF do I need to paint background in red if the room is not available? */
            if( (strpos(",".$v2['idsRoom'].",",",".$v['id'].",")!==false) && 
                ($checkin < $hasta && $checkout > $desde) && 
                ($checkin >= $desde && $checkout <= $hasta)
            ){
                echo ' style="background-color:red;"';
            }
        }
        echo '>&nbsp;</td>';
        $desde->modify("+1Day");
    }
    echo '</tr>';
}
$conn->close();
?>
</table>
提前感谢大家,希望能找到一些帮助~

问候

因为$desde是while循环中的一个运行日期,所以您应该进行检查

如果$desde在$checkin和$checkout范围内,则房间将被阻止 红色的

因此,通过使用此逻辑,这是您需要的唯一条件:

if ((strpos(",".$v2['idsRoom'].",",",".$v['id'].",")!==false) &&
    ($checkin <= $desde && $checkout > $desde)
){
    echo ' style="background-color:red;"';
}

我在我的本地主机上运行了这个程序,它按照您在示例中希望的方式工作。希望有帮助。

在快速查看问题后,我注意到id列中有一个逗号分隔的值。这肯定会给你带来麻烦,将来还会给你带来更多的麻烦。我不是一个数据库设计专家,当我开始编程时,我也在做同样的事情。我想做一些简单快捷的事情,不要在人际关系上有太多麻烦,结果总是一团糟,最终不得不从头开始,正确地做

这种设计反模式将使查询变得更加困难,而且,您必须编写大量代码来重新创建丢失的数据库的内置功能您已经开始这样做了,strpos,.$v2['idsRoom',,,,.$v['id']==错

我花了一些时间重写了这个例子。对于日期范围,我使用类。它可以在foreach循环中使用,因此它适用于日历等。此外,这里有两个关联数组$rooms和$bookings,它们与示例中的相同。将$bookings数组转换为分隔idsRoom值后,它看起来如下所示:

Array ( [0] => Array ( ... ) [1] => Array ( ... ) [2] => Array ( [idsRoom] => 6 [checkin] => 2014-07-18 [checkout] => 2014-07-22 ) [3] => Array ( [idsRoom] => 7 [checkin] => 2014-07-18 [checkout] => 2014-07-22 ) [4] => Array ( [idsRoom] => 8 [checkin] => 2014-07-18 [checkout] => 2014-07-22 ) [5] => Array (... ) ) 最后,循环打印表格:

<?php

$range = new DatePeriod( new DateTime( "2014-07-01" ), new DateInterval( 'P1D' ), new DateTime( "2014-08-01" ) );

$rooms = array_map( function( $k, $v ) { return array( 'name' => $v, 'id' => $k ); }, range( 1, 30 ), array_merge( range( 101, 110 ), range( 201, 210 ), range( 301, 310 ) ) );

$bookings_original = array( 
    array( 'idsRoom' => '2',    'checkin' => '2014-06-27', 'checkout' => '2014-07-02' ),
    array( 'idsRoom' => '4',    'checkin' => '2014-07-08', 'checkout' => '2014-07-09' ),
    array( 'idsRoom' => '6,7,8','checkin' => '2014-07-18', 'checkout' => '2014-07-22' ),
    array( 'idsRoom' => '14',   'checkin' => '2014-07-31', 'checkout' => '2014-08-02' )
);

$bookings = array();
foreach( $bookings_original as $item ) {
    foreach( explode( ',', $item['idsRoom'] ) as $room )
        $bookings[] = array_merge( $item, array( 'idsRoom' => $room ) );
}

?>
<table border="1" cellpadding="0" cellspacing="0" style="min-width:100%;min-height:100%;">
    <tr>
        <td>&nbsp;</td>
        <?php foreach( $range as $date ) : ?>
        <td><?php echo $date->format("d-m-Y"); ?></td>
        <?php endforeach; ?>
    </tr>
<?php

foreach( $rooms as $room ) {
    echo "<tr><td>$room[name]</td>" . PHP_EOL;

    foreach( $range as $date ) {
        if (
            array_filter( $bookings, function( $booking ) use ( $room, $date ) {
                return $booking[ 'idsRoom' ] == $room['id'] && $booking['checkin'] <= $date->format( "Y-m-d" ) && $booking['checkout'] > $date->format( "Y-m-d" );
            } ) 
        )
            echo '<td style="background-color:red;"></td>' . PHP_EOL;
        else
            echo '<td></td>' . PHP_EOL;
    }
    echo '</tr>';
}

?>
</table>

下面是一个可以帮助您处理数据库和表之间关系的方法。

您似乎有一列逗号分隔的值,称为idsRoom。在这种情况下,最有效的解决方案是一罐汽油和一些火柴。是的,现在我的解决方案是存储预订以及与每个预订相关的房间。这是一个好的解决方案,还是可以改进?这是一个糟糕的解决方案。请参阅规范化。好的~!谢谢草莓哦我的天啊。。。工作得很有魅力!非常感谢D现在看到这个非常简单和简洁的解决方案,我感到很惭愧。祝你今天愉快^^我明白了。。。另一种解决方法。谢谢你的DatePeriod和DateInterval示例:D我真的很感激。我将看一看你的代码和实现你的功能必须首先理解它:P谢谢!