【问题标题】:While loop for two queries两个查询的while循环
【发布时间】:2015-12-08 08:55:26
【问题描述】:

我有这两个查询,一个用于显示最低费率,另一个用于显示所有费率。我正在使用 while 循环来获取数据,第一个 while 循环用于具有 min 和 group by id 的属性,另一个 while 循环用于包含所有费率的房间包。 我想要这种格式:

 <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);

【问题讨论】:

  • 欢迎来到 SO。请发布您的代码,以便我们查看。

标签: php sql xml


【解决方案1】:

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

  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);
                        }
                    }
                }
            }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2013-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 2013-01-15
    • 2013-03-19
    相关资源
    最近更新 更多