简体   繁体   中英

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.

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 '.

in my table prereservation each id has different * qty *, so i want sum of qty of each id 's for sample like this:

    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? also please check the first 3 queries because i know i have an error for each query. to get the array_merge and array_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)

Use one SQL statement for your selection,

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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