在循环时优化PHP

时间:2015-02-24 23:25:28

标签: php loops mysqli while-loop

我在做什么:

检索可用的公寓列表并按布局分组 - 工作室,1卧室,2卧室等。每个布局列出最低和最高的平方英尺,最低租金,然后列出所有单独的单元,每个单元的详细信息。我附上一张图片来展示它的样子。 floorplans

问题

目前,我正在使用多个查询来获取床的数量,并为每个查询运行while循环。我希望这个优化,只使用1个查询,但不知道从哪里开始。

代码

我只在这里展示Studio和1 Bedroom的循环,但我对2和3卧室查询做同样的事情。我想将这些全部合并为一个。任何帮助表示赞赏。

$studioQuery = $mysqli->query('SELECT * FROM properties WHERE Beds = "0" AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY FloorplanName');
$oneBedQuery = $mysqli->query('SELECT * FROM properties WHERE Beds = "1" AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY FloorplanName');
$twoBedQuery = $mysqli->query('SELECT * FROM properties WHERE Beds = "2" AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY FloorplanName');
$threeBedQuery = $mysqli->query('SELECT * FROM properties WHERE Beds = "3" AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY FloorplanName');

// Studio
if($studioQuery->num_rows > 0) {
    echo '<div class="layout">';
    $studioCount = 0;
    $highSqFt = 0;
    $markupStudio = '';
    while ($row = mysqli_fetch_assoc($studioQuery)) {

        $aptID = $row['ApartmentId'];

        // Get the lowest square footage
        if($studioCount == 0){
            $lowSqFt = $row['SQFT'];
            $lowRent = $row['MinimumRent'];
        }
        if($lowSqFt > $row['SQFT']){
            $lowSqFt = $row['SQFT'];
        }
        // Get the highest square footage
        if($highSqFt < $row['SQFT']){
            $highSqFt = $row['SQFT'];
        }
        // Get the lowest Rent
        if($lowRent > $row['MinimumRent']){
            $lowRent = $row['MinimumRent'];
        }

        $markupStudio .= '<div class="row unit">';
        $markupStudio .= '<div class="col-sm-15">'. $row['Beds'] .' Bed / '. $row['Baths'] .' Bath</div>';
        $markupStudio .= '<div class="col-sm-15">'. $row['ApartmentName'] .'</div>';
        $markupStudio .= '<div class="col-sm-15">$'. $row['MinimumRent'] .'</div>';
        $markupStudio .= '<div class="col-sm-15">'. $row['AvailableDate'] .'</div>';
        $markupStudio .= '<div class="col-sm-15 apply-col"><a href="'. $row['ApplyOnlineURL'] .'" target="_blank">Apply ></a></div>';
        $markupStudio .= '</div>';

        $studioCount++;
    }
    ?>

    <div class="row floorplan-header">
        <div class="col-sm-3"><div><h3>Studio</h3></div></div>
        <div class="col-sm-3 sq-ft-col"><div><?php echo $lowSqFt .' - '. $highSqFt .' SQ. FT.'; ?></div></div>
        <div class="col-sm-3 from-price-col"><div>FROM $<?php echo $lowRent; ?></div></div>
        <div class="col-sm-3 available-col"><div>Check Availability <span class="icon-caret-right"></span></div></div>
    </div>

    <div class="row unit-list">
        <div class="col-lg-12">
            <div class="row unit-header">
                <div class="col-sm-15">Type</div>
                <div class="col-sm-15">Unit#</div>
                <div class="col-sm-15">Rent</div>
                <div class="col-sm-15">Available</div>
            </div>
            <?php echo $markupStudio; ?>
        </div>
    </div>

    <?php echo '</div>';
}

// 1 Bedroom
if($oneBedQuery->num_rows > 0) {
    echo '<div class="layout">';
    $oneBedCount = 0;
    $highSqFt = 0;
    $markupOne = '';
    while ($row = mysqli_fetch_assoc($oneBedQuery)) {

        $aptID = $row['ApartmentId'];

        // Get the lowest square footage
        if($oneBedCount == 0){
            $lowSqFt = $row['SQFT'];
            $lowRent = $row['MinimumRent'];
        }
        if($lowSqFt > $row['SQFT']){
            $lowSqFt = $row['SQFT'];
        }
        // Get the highest square footage
        if($highSqFt < $row['SQFT']){
            $highSqFt = $row['SQFT'];
        }
        // Get the lowest Rent
        if($lowRent > $row['MinimumRent']){
            $lowRent = $row['MinimumRent'];
        }

        $markupOne .= '<div class="row unit">';
        $markupOne .= '<div class="col-sm-15">'. $row['Beds'] .' Bed / '. $row['Baths'] .' Bath</div>';
        $markupOne .= '<div class="col-sm-15">'. $row['ApartmentName'] .'</div>';
        $markupOne .= '<div class="col-sm-15">$'. $row['MinimumRent'] .'</div>';
        $markupOne .= '<div class="col-sm-15">'. $row['AvailableDate'] .'</div>';
        $markupOne .= '<div class="col-sm-15 apply-col"><a href="'. $row['ApplyOnlineURL'] .'" target="_blank">Apply ></a></div>';
        $markupOne .= '</div>';

        $oneBedCount++;
    }
    ?>

    <div class="row floorplan-header">
        <div class="col-sm-3"><div><h3>1 Bedroom</h3></div></div>
        <div class="col-sm-3 sq-ft-col"><div><?php echo $lowSqFt .' - '. $highSqFt .' SQ. FT.'; ?></div></div>
        <div class="col-sm-3 from-price-col"><div>FROM $<?php echo $lowRent; ?></div></div>
        <div class="col-sm-3 available-col"><div>Check Availability <span class="icon-caret-right"></span></div></div>
    </div>

    <div class="row unit-list">
        <div class="col-lg-12">
            <div class="row unit-header">
                <div class="col-sm-15">Type</div>
                <div class="col-sm-15">Unit#</div>
                <div class="col-sm-15">Rent</div>
                <div class="col-sm-15">Available</div>
            </div>
            <?php echo $markupOne; ?>
        </div>
    </div>

    <?php echo '</div>';
}

/ *编辑* /

所以现在我有一个查询,但我的逻辑在while循环中关闭。它现在打印出正确数量的Floorplan标题,但仅显示1卧室下的第一个可用单元,然后列出2卧室标题下的所有可用单卧室单元,仅列出第一个可用的2卧室单元。以下是更新后的代码。

$bedQuery = $mysqli->query('SELECT * FROM properties WHERE PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY Beds, FloorplanName');

// Floorplans
if($bedQuery->num_rows > 0) {

    $bedCount = 0;
    $highSqFt = 0;
    $markup = '';
    $previous = 0;

    while ($row = mysqli_fetch_assoc($bedQuery)) {

        $bedCount = $row['Beds'];
        $aptID = $row['ApartmentId'];

        // Get the lowest square footage
        if($previous == 0){
            $lowSqFt = $row['SQFT'];
            $lowRent = $row['MinimumRent'];
        }
        if($lowSqFt > $row['SQFT']){
            $lowSqFt = $row['SQFT'];
        }
        // Get the highest square footage
        if($highSqFt < $row['SQFT']){
            $highSqFt = $row['SQFT'];
        }
        // Get the lowest Rent
        if($lowRent > $row['MinimumRent']){
            $lowRent = $row['MinimumRent'];
        }

        $markup .= '<div class="row unit">';
        $markup .= '<div class="col-sm-15">'. $row['Beds'] .' Bed / '. $row['Baths'] .' Bath</div>';
        $markup .= '<div class="col-sm-15">'. $row['ApartmentName'] .'</div>';
        $markup .= '<div class="col-sm-15">$'. $row['MinimumRent'] .'</div>';
        $markup .= '<div class="col-sm-15">'. $row['AvailableDate'] .'</div>';
        $markup .= '<div class="col-sm-15 apply-col"><a href="'. $row['ApplyOnlineURL'] .'" target="_blank">Apply ></a></div>';
        $markup .= '</div>';

        if($bedCount != $previous) {
            //echo 'Beds: '.$bedCount.'</br>';
            //echo 'Previous: '.$previous;
            echo '<div class="layout">';
        ?>

        <div class="row floorplan-header">
            <div class="col-sm-3"><div><h3><?php echo $row['Beds']; ?> Bedroom</h3></div></div>
            <div class="col-sm-3 sq-ft-col"><div><?php echo $lowSqFt .' - '. $highSqFt .' SQ. FT.'; ?></div></div>
            <div class="col-sm-3 from-price-col"><div>FROM $<?php echo $lowRent; ?></div></div>
            <div class="col-sm-3 available-col"><div>Check Availability <span class="icon-caret-right"></span></div></div>
        </div>

        <div class="row unit-list">
            <div class="col-lg-12">
                <div class="row unit-header">
                    <div class="col-sm-15">Type</div>
                    <div class="col-sm-15">Unit#</div>
                    <div class="col-sm-15">Rent</div>
                    <div class="col-sm-15">Available</div>
                </div>
                <?php echo $markup; ?>
            </div>
        </div>

        <?php echo '</div>';
        }

        $previous = $bedCount;

    }

}

2 个答案:

答案 0 :(得分:3)

使用in()并将Beds添加到order by会产生一个查询:

$studioQuery = $mysqli->query('SELECT * FROM properties WHERE Beds IN('0','1','2','3') AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY Beds, FloorplanName');

现在,在同一个查询中,您将获得所需的所有行,每个床号都在下一个之前,并且在每张床的数量中,您将按照FloorplanName订购。

现在,只需循环浏览它们,跟踪最后一个床值并与当前床值进行比较。当它们不再相等时,打印该部分的新标题。

答案 1 :(得分:1)

我建议用一个查询来选择所有数据,然后将其保存到以床位数索引的数组中。

然后迭代各个顶级元素代替当前的while循环

$query = $mysqli->query('SELECT * FROM properties WHERE Beds < 4 AND PropertyId = '.$propID.' AND AvailableDate <> "" ORDER BY FloorplanName');

$data=[];

while ($row = mysqli_fetch_assoc($query))
    $data[$row['beds']][]=$row;

foreach($data[0] as $studioRow)
    //studio code


foreach($data[1] as $oneBedRow)
    //onebed code

这可以最大限度地减少您的重构,并有助于将您的演示文稿与逻辑分开一点