简体   繁体   中英

Query for hotel reservation

I have three queries in a hotel reservation system where reserves are automatic deducted from the total number of rooms. All I want those three queries goes all in only one query. My query looks like this:

$p = is in a foreach loop where $p['id'] is the id of each room class and $p['qty'] is the maximum rooms of each room class.

$a = $p['id'];
$query = mysql_query("SELECT sum(qty) FROM prereservation where '$arrival' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin=$rows['sum(qty)'];
          }
$query = mysql_query("SELECT sum(qty) FROM prereservation where departure BETWEEN '$arrival' and '$departure' and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin2=$rows['sum(qty)'];
          }
$query = mysql_query("SELECT sum(qty) FROM prereservation where '$departure' BETWEEN arrival and departure and room_id ='$a' and status = 'active'");
        while($rows = mysql_fetch_array($query))
          {
          $inogbuwin3=$rows['sum(qty)'];
          }

<select>
 <option value="0"></option>
 <? $counter = 1; ?>
 <? while ($counter <= ($p['qty'])-($inogbuwin + $inogbuwin2 + $inogbuwin3)){ ?>
 <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
 <? $counter++;
 }?>
</select>

Every time I put a date between the range of those three queries, the total deducted is also tripled which is the reason I want those three queries to be all in one.

for example i have a record in database of arrival = 27/10/2012 and departure = 29/10/2012

and the input dates that are not available in range is

 $arrival = 27/10/2012 and $departure = 29/10/2012 
 $arrival = 26/10/2012 and $departure = 27/10/2012 
 $arrival = 29/10/2012 and $departure = 30/10/2012 
 $arrival = 26/10/2012 and $departure = 30/10/2012 
 $arrival = 29/10/2012 and $departure = 29/10/2012 

so each date that in rance of those three queries are also the deductions, so i want those queries to be in one. thanks guys

after all i fixed the errors but only one preblem left. i fixed the reservation dates in only a single month, the problem is when the inputs of arrival and departure are not in the same month it does'nt work either. here's my code again below.

<?
$a = $p['id'];
    $query = mysql_query("SELECT
    SUM(IF('$arrival' BETWEEN arrival and departure, qty, 0)) AS bu1,
    SUM(IF('$departure' BETWEEN arrival and departure, qty, 0)) AS bu2,
    SUM(IF(arrival > '$arrival' and departure < '$departure', qty, 0)) AS bu3
    FROM prereservation WHERE room_id ='$a' and status = 'active'");
    $row = mysql_fetch_array($query);

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

please help me guys how to solve this reservation query, or maybe there's an other way to solve this in php code. thanks guys.

Finally i'm finished and found the solution, here it is:

            $a = $p['id'];
        $query1 = mysql_query("SELECT DISTINCT id, SUM(qty)
        FROM prereservation 
        WHERE 
        (
            ( '$arival1' BETWEEN arrival AND departure ) OR 
            ( '$departure1' BETWEEN arrival AND departure ) OR 
            ( arrival > '$arival1' AND departure < '$departure1' )
        )
            AND room_id ='$a' 
            AND STATUS = 'active'");  
        while($rows1 = mysql_fetch_assoc($query1)){
        $set1 = $rows1['SUM(qty)'];
        }   
        ?> 
        <select id="select" name="qty[]" style=" width:50px;" onchange="checkall()">
        <option value="0"></option>
        <? $counter = 1; ?>
        <? while ($counter <= ($p['qty']) - $set1){ ?>
        <option value="<?php echo $counter ?>"><?php echo $counter ?></option>
        <? $counter++;
        }?>
        </select>

thank you guys for sharing your ideas, this solution is the combinations of your answers.. thanks again!!

You could do it like this:

SELECT
    SUM(IF('$arrival'    BETWEEN arrival   and departure,    qty, 0)) AS bu1,
    SUM(IF(departure    BETWEEN '$arrival' and '$departure', qty, 0)) AS bu2,
    SUM(IF('$departure' BETWEEN arrival   and departure,    qty, 0)) AS bu3
FROM prereservation WHERE room_id ='$a' and status = 'active'"

and then in PHP:

$query = mysql_query(...);
$row = mysql_fetch_array($query);
$inogbuwin =$row['bu1'];
$inogbuwin2=$row['bu2'];
$inogbuwin3=$row['bu3'];
mysql_free($query);

include the customary warning that mysql_ functions are discouraged and you would do well by migrating to PDO

The old API should not be used, and one day it will be deprecated and eventually removed from PHP . It is a popular extension so this will be a slow process, but you are strongly encouraged to write all new code with either mysqli or PDO_MySQL .

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