while循环进行两次查询

时间:2015-12-08 08:55:26

标签: php sql xml

我有两个查询用于显示最低费率,而另一个用于显示所有费率。我使用while循环来获取数据,第一个while循环用于具有min和group by id的Property,另一个while循环用于包含所有速率的room bundle。 我希望有这种格式:

 <Result>
        <Property>7</Property>
        <Checkin>2015-12-27</Checkin>
        <Nights>1</Nights>
        <Baserate currency="PHP" all_inclusive="true">4000</Baserate>
        <AllowablePointsOfSale>
        <PointOfSale id="7"/>
        </AllowablePointsOfSale>

         <RoomBundle>
            <RoomID>26</RoomID>
            <Baserate currency="PHP" all_inclusive="true">4385</Baserate>
            <AllowablePointsOfSale>
            <PointOfSale id="7"/>
            </AllowablePointsOfSale>
          </RoomBundle>

          <RoomBundle>
           <RoomID>167</RoomID>
           <Baserate currency="PHP" all_inclusive="true">4000</Baserate>
           <AllowablePointsOfSale>
           <PointOfSale id="7"/>
           </AllowablePointsOfSale>
          </RoomBundle>
    </Result>
    <Result>
            <Property>9</Property>
            <Checkin>2015-12-27</Checkin>
            <Nights>1</Nights>
            <Baserate currency="PHP" all_inclusive="true">5000</Baserate>
            <AllowablePointsOfSale>
            <PointOfSale id="7"/>
            </AllowablePointsOfSale>

             <RoomBundle>
                <RoomID>2</RoomID>
                <Baserate currency="PHP" all_inclusive="true">6385</Baserate>
                <AllowablePointsOfSale>
                <PointOfSale id="7"/>
                </AllowablePointsOfSale>
              </RoomBundle>
        </Result>

我现在获得的格式是所有房间套餐只有一个属性。是否可以在一行中查询?获取格式的最佳方法是什么?谢谢

while循环

    while($row1 = mysql_fetch_array($sql_result)) { 
while($row = mysql_fetch_array($sql_result1)) { 
    $rate = $row['Minrate'];
    for ($a = 1; $a <= $Nights; ++$a) { 
        $full_rate = $rate * $a;
        $Result = $Transaction->addChild('Result');
        $Property = $Result->addChild('Property',$row['hotel_id']);
        $Checkin = $Result->addChild('Checkin', $Checkin);
        $full_rate = $rate * $a;
        $Nights1 = $Result->addChild('Nights', $a);
        $Baserate = $Result->addChild('Baserate', $full_rate);
                $Baserate -> addAttribute('currency', 'PHP');
                $Baserate -> addAttribute('all_inclusive', 'true');
        $AllowablePointsOfSale = $Result ->addChild('AllowablePointsOfSale');
            $PointOfSale = $AllowablePointsOfSale-> addChild('PointOfSale');
    $PointOfSale -> addAttribute ('id',$row['hotel_id']); }
        }
    }




            $hotelid = $row1['hotel_id'];
            $roomtypeid = $row1['roomtypeid'];
            $roomtype = $row1['roomtype'];
            $rates = $row1['rates'];
            $weekrate = $row1['$week_rate'];
            $weekmin = $row1['$week_min'];
        //for room bundles
        //$full_rate = $rates * $a;
        $RoomBundle = $Result -> addChild('RoomBundle');
            $RoomID = $RoomBundle -> addChild('RoomID', $roomtypeid);
        //$PackageID = $RoomBundle -> addChild('PackageID', 'package_id');
        $Baserate = $RoomBundle->addChild('Baserate', $rates);
                $Baserate -> addAttribute('currency', 'PHP');
                $Baserate -> addAttribute('all_inclusive', 'true');
        $AllowablePointsOfSale = $RoomBundle ->addChild('AllowablePointsOfSale');
            $PointOfSale = $AllowablePointsOfSale-> addChild('PointOfSale');
        $PointOfSale -> addAttribute ('id',$hotelid);       
}

SQL

    $sql1 = "Select roomtype.hotel_id, roomtype.roomtypeid, roomtype.week_rate, roomtype.week_min, roomtype.max_pax, roomtype.roomtype, roomtype.no_pax, 
MIN(roomtype.rates) AS Minrate, hotel.currencylab, hotel.id, room.hotelid, roomtype.rates
From roomtype, hotel, room WHERE  roomtype.hotel_id IN ($hotels) AND roomtype.deleted='0' AND  roomtype.no_pax='2' AND roomtype.hotel_id = hotel.id AND roomtype.hotel_id = room.hotelid
Group by hotel.id Order by roomtype.roomtypeid, roomtype.rates";

$sql = "Select roomtype.* ,hotel.currencylab,hotel.id From roomtype,hotel WHERE  roomtype.hotel_id IN ($hotels) AND  roomtype.deleted='0' AND roomtype.hotel_id = hotel.id";

$sql_result = mysql_query($sql, $conn);
$sql_result1 = mysql_query($sql1, $conn);

1 个答案:

答案 0 :(得分:0)

这就是我所做的。 创建了一个数组。

  while($hotel_min = mysql_fetch_array($sql_result1)) {
            $rowset1[]=$hotel_min;
}
while($room = mysql_fetch_array($sql_result)) {
            $rowset2[]=$room;
}

添加了foreach和if条件。

foreach($rowset1 as $row) {
    for ($a = 1; $a <= $Nights; ++$a) {
    $full_rate = $row['Minrate'] * $a;
        $Result = $Transaction->addChild('Result');
        $Property = $Result->addChild('Property', $row['hotel_id']);
        $Checkin = $Result->addChild('Checkin', $Checkin);
        $Nights1 = $Result->addChild('Nights', $a);
        $Baserate = $Result->addChild('Baserate', $full_rate);
                $Baserate -> addAttribute('currency', $row['currencylab']);
                $Baserate -> addAttribute('all_inclusive', 'true');
        $AllowablePointsOfSale = $Result ->addChild('AllowablePointsOfSale');
    $PointOfSale = $AllowablePointsOfSale-> addChild('PointOfSale');
    $PointOfSale -> addAttribute ('id',$row['hotel_id']);
  foreach($rowset2 as $row1) {
      if ($row['hotel_id'] == $row1['hotel_id']){
          $hotelid = $row1['hotel_id'];
            $roomtypeid = $row1['roomtypeid'];
            $roomtype = $row1['roomtype'];
            $rates = $row1['rates'];
            $weekrate = $row1['$week_rate'];
            $weekmin = $row1['$week_min'];
            $fullrate = $rates * $a;
        //for room bundles
        //$full_rate = $rates * $a;
        $RoomBundle = $Result -> addChild('RoomBundle');
            $RoomID = $RoomBundle -> addChild('RoomID', $roomtypeid);
        //$PackageID = $RoomBundle -> addChild('PackageID', 'package_id');
        $Baserate = $RoomBundle->addChild('Baserate', $fullrate);
                $Baserate -> addAttribute('currency', $row1['currencylab']);
                $Baserate -> addAttribute('all_inclusive', 'true');
        $AllowablePointsOfSale = $RoomBundle ->addChild('AllowablePointsOfSale');
            $PointOfSale = $AllowablePointsOfSale-> addChild('PointOfSale');
            $PointOfSale -> addAttribute ('id',$hotelid);
                        }
                    }
                }
            }