简体   繁体   English

预订日期的酒店预订查询

[英]hotel reservation query for booking date

how to get result of a queries into array and the results should compare each one and merge into one array. 如何将查询结果放入数组中,结果应进行比较,然后合并为一个数组。

i have 3 queries, i don't know what's is wrong in my coding, my best is not enough so guys please help me especially the experts for sql and php. 我有3个查询,我不知道我的编码有什么问题,我最好的还不够,所以伙计们请帮助我,尤其是sql和php的专家。

here's my code: 这是我的代码:

        <?php
        $a = $p['id'];
        $query1 = mysql_query("SELECT id FROM prereservation where '$arrival' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        $rows1 = mysql_fetch_array($query1);  

        $query2 = mysql_query("SELECT id FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        $rows2 = mysql_fetch_array($query2);

        $query3 = mysql_query("SELECT id FROM prereservation where arrival > '$arrival' and departure < '$departure' and room_id ='$a' and status = 'active'");
        $rows3 = mysql_fetch_array($query3);

        $sample = array_unique(array_merge($rows1, $rows2, $rows3));             
        ?> 

and the display is like this: 和显示是这样的:

        <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
        <option value="0"></option>
        <? $counter = 1; ?>
        <? while ($counter <= ($p['qty']) - $????????) { ?>
        <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
        <? $counter++;
        }?>
        </select>

all i want for example output is like this: 我想要的所有输出例如是这样的:

$rows1 = array(id= 48, 55, 51, 53)
$rows2 = array(id= 48, 49, 51, 52)
$rows3 = array(id= 48, 49, 50, 51)
$sample = array_unique(array_merge($rows1, $rows2, $rows3))

so the output of sample is like this: 所以样本的输出是这样的:

$sample = array(id= 48, 49, 50, 51, 52, 53)

then in table prereservation the columns are: ' id ', ' arrival ', ' departure ', ' room_id ', ' qty ' and ' status '. 然后在表保留中,列为:“ id ”,“ 到达 ”,“ 离开 ”,“ room_id ”,“ 数量 ”和“ 状态 ”。

in my table prereservation each id has different * qty *, so i want sum of qty of each id 's for sample like this: 在我的表保留中,每个ID都有不同的 * 数量 *,所以我想要每个ID数量 ,例如:

    id    |   qty
    48    |    2
    49    |    1
    50    |    1
    51    |    3   
    52    |    1
    53    |    3  


$total = sum(qty); which is 11

can you guys help me how to create a query to get the sum of those qty by each id 's? 你们可以帮助我如何创建查询以获取每个id数量 总和吗? also please check the first 3 queries because i know i have an error for each query. 还请检查前3个查询,因为我知道每个查询都有错误 to get the array_merge and array_unique . 获得array_mergearray_unique thank you very much guys. 非常感谢你们。

            <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
            <option value="0"></option>
            <? $counter = 1; ?>
            <? while ($counter <= ($p['qty']) - $total) { ?>
            <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
            <? $counter++;
            }?>
            </select>

here my database: 这是我的数据库:

table rooms
id |  type   |  qty
---|---------|-----
11 | single  |  50
12 | double  |  50
13 | deluxe  |  20

table preservation
id |   arrival   |  departure  | room_id | qty | status
---|-------------|-------------|---------|-----|-------
48 | 05/11/2012  | 11/11/2012  |    11   |  2  | active 
49 | 06/11/2012  | 11/11/2012  |    11   |  1  | active 
50 | 06/11/2012  | 08/11/2012  |    11   |  1  | active 
51 | 05/11/2012  | 07/11/2012  |    11   |  3  | active 
52 | 06/11/2012  | 09/11/2012  |    11   |  1  | active 
53 | 07/11/2012  | 07/11/2012  |    11   |  3  | active 

so in my display i have an select tag from table rooms(id=11) and the option is qty and deducted by total sum of qty from table preservation(room_id=11) 因此在我的显示中,我有一个来自table rooms(id = 11)的select标记,该选项的数量qty,减去了来自表保存(room_id = 11)数量的 总和

Use one SQL statement for your selection, 使用一条SQL语句进行选择,

"SELECT id, SUM(qty) FROM prereservation GROUP BY qty WHERE ( ( '$arrival' BETWEEN arrival and departure and room_id ='$a' ) OR ( '$departure' BETWEEN arrival and departure and room_id ='$a' ) OR ( arrival > '$arrival' and departure < '$departure' and room_id ='$a' ) ) AND status = 'active'";

Edit: 编辑:

ok what about something like this? 好,这样的事情呢?

SELECT *, (SELECT SUM(preservation.qty) as sum FROM preservation WHERE rooms.id = preservation.room_id GROUP BY preservation.room_id ) as qty_reserved, rooms.qty - (SELECT SUM(preservation.qty) as sum FROM preservation WHERE rooms.id = preservation.room_id GROUP BY preservation.room_id ) as qty_available  FROM rooms;

It's much more efficient going to the database once, getting all the information you need and reusing that throughout your application. 一次访问数据库,获取所需的所有信息并在整个应用程序中重复使用这些信息,效率将大大提高。 You shouldn't need any logic in your php if just displaying these variables. 如果只显示这些变量,则您不需要在php中使用任何逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM