首页 > 解决方案 > 如何为while循环的第一次迭代设置PHP变量?

问题描述

我正在尝试将变量 $OnWishList 设置为显示一个勾号或加号,具体取决于该属性是否在愿望清单中。尽管它这样做了,但它会跳过 wile 循环中的第一个属性,并在第二个属性上显示第一个属性的正确符号。如何让符号显示在正确的属性上。如果这是一个简单的问题,我深表歉意,因为我是 PHP 和 SQL 的新手,并且已经为这个问题苦苦挣扎了一段时间。

$city = (isset($_GET['city'])) ? $_GET['city'] : 0;
$suburb = (isset($_GET['suburb'])) ? $_GET['suburb'] : 0;
$minBed = (isset($_GET['minBed'])) ? $_GET['minBed'] : 0;
$maxBed = (isset($_GET['maxBed'])) ? $_GET['maxBed'] : 0;
$minBath = (isset($_GET['minBath'])) ? $_GET['minBath'] : 0;
$maxBath = (isset($_GET['maxBath'])) ? $_GET['maxBath'] : 0;
$minPrice = (isset($_GET['minPrice'])) ? $_GET['minPrice'] : 0;
$maxPrice = (isset($_GET['maxPrice'])) ? $_GET['maxPrice'] : 0;


$pagingVariable = '';
$cityName ='';
//}

        $rowsPerPage = 8; // edit the number of rows per page



        $query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property. price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image, tbl_city.cityName FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID INNER JOIN tbl_city ON tbl_property.city_ID=tbl_city.city_ID";


//$query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property. price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID";
$pagingLink = getPagingLink($query, $rowsPerPage);
$result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
if($city == 0){
    $query .= " WHERE ";

}

        if($city != 0){

        $query = "SELECT tbl_property.property_ID, tbl_property.name, tbl_property.location, tbl_property.description, tbl_property.price, tbl_property.landsize, tbl_property.property_image, tbl_property.bedrooms, tbl_property.bathrooms, tbl_property.garage, tbl_agents.agent_name, tbl_agents.agent_image, tbl_city.cityName FROM tbl_property INNER JOIN tbl_agents ON tbl_property.agent_ID=tbl_agents.agent_ID INNER JOIN tbl_city ON tbl_property.city_ID=tbl_city.city_ID WHERE tbl_property.city_ID ='$city'";
            $pagingVariable .= "&city=".$_GET['city'];
            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
            $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
            }



            if($suburb != 0){

                $query .= "AND suburb_ID ='$suburb'";
                $pagingVariable .= "&suburb=".$_GET['suburb'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

        if($minBed != 0 && $maxBed == 0 && $city == 0){

            $query .= " bedrooms >='$minBed'";

                $pagingVariable .= "&minBed=".$_GET['minBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($minBed != 0 && $city != 0){

                $query .= "AND bedrooms >='$minBed'";
                $pagingVariable .= "&minBed=".$_GET['minBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($maxBed != 0 && $minBed == 0 && $city == 0){

            $query .= " bedrooms <='$maxBed'";

                $pagingVariable .= "&maxBed=".$_GET['maxBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($maxBed != 0 && $minBed == 0 && $city != 0){

                $query .= "AND bedrooms <='$maxBed'";
                $pagingVariable .= "&maxBed=".$_GET['maxBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($minBed != 0 && $maxBed != 0 && $city != 0){
                $query .= "AND (bedrooms BETWEEN '$minBed' AND '$maxBed')";
                $pagingVariable .= "&minBed=".$_GET['minBed']."&maxBed=".$_GET['maxBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}

            if($minBed != 0 && $maxBed != 0 && $city == 0){
                $query .= " (bedrooms BETWEEN '$minBed' AND '$maxBed')";
                $pagingVariable .= "&minBed=".$_GET['minBed']."&maxBed=".$_GET['maxBed'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}



if (  ((($minBed != 0 || $maxBed != 0) && $city != 0)) || ((($minBed != 0 || $maxBed != 0) && $city == 0)) || ((($minBed == 0 && $maxBed == 0) && $city != 0))){
 $query .= " AND";
}   


if($minBath != 0 && $maxBath == 0 && $city == 0){

            $query .= " bathrooms >='$minBath'";

                $pagingVariable .= "&minBath=".$_GET['minBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                } 

            if($minBath != 0 && $maxBath == 0 && $city != 0){

                $query .= " bathrooms >='$minBath'";
                $pagingVariable .= "&minBath=".$_GET['minBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($minBath != 0 && $maxBath != 0 && $city == 0){
                $query .= " (bathrooms BETWEEN '$minBath' AND '$maxBath')";
                $pagingVariable .= "&minBath=".$_GET['minBath']."&maxBath=".$_GET['maxBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}

            if($minBath != 0 && $maxBath != 0 && $city != 0){
                $query .= " (bathrooms BETWEEN '$minBath' AND '$maxBath')";
                $pagingVariable .= "&minBath=".$_GET['minBath']."&maxBath=".$_GET['maxBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}

            if($maxBath != 0 && $minBath == 0 && $city == 0){

            $query .= " bathrooms <='$maxBath'";

                $pagingVariable .= "&maxBath=".$_GET['maxBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($maxBath != 0 && $minBath == 0 && $city != 0){

                $query .= " bathrooms <='$maxBath'";
                $pagingVariable .= "&maxBath=".$_GET['maxBath'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }
//
//
if ($minBath != 0 || $maxBath != 0){
 $query .= " AND";

}

if($minPrice != 0 && $maxPrice == 0 && $city == 0){

            $query .= " Price >='$minPrice'";

                $pagingVariable .= "&minPrice=".$_GET['minPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                } 

            if($minPrice != 0 && $maxPrice == 0 && $city != 0){

                $query .= " Price >='$minPrice'";
                $pagingVariable .= "&minPrice=".$_GET['minPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($minPrice != 0 && $maxPrice != 0 && $city == 0){
                $query .= " (Price BETWEEN '$minPrice' AND '$maxPrice')";
                $pagingVariable .= "&minPrice=".$_GET['minPrice']."&maxPrice=".$_GET['maxPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}

            if($minPrice != 0 && $maxPrice != 0 && $city != 0){
                $query .= " (Price BETWEEN '$minPrice' AND '$maxPrice')";
                $pagingVariable .= "&minPrice=".$_GET['minPrice']."&maxPrice=".$_GET['maxPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
}

            if($maxPrice != 0 && $minPrice == 0 && $city == 0){

            $query .= " Price <='$maxPrice'";

                $pagingVariable .= "&maxPrice=".$_GET['maxPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

            if($maxPrice != 0 && $minPrice == 0 && $city != 0){

                $query .= " Price <='$maxPrice'";
                $pagingVariable .= "&maxPrice=".$_GET['maxPrice'];

            $pagingLink = getPagingLink($query, $rowsPerPage, $pagingVariable);
                $result = mysqli_query($link, getPagingQuery($query, $rowsPerPage));
                }

   if (mysqli_num_rows($result) < 1) {
     $noResults = "Sorry, no results were found!";
   } else {


    while($row = mysqli_fetch_array($result)) {    
      extract($row);
    ?>
            <div class="col-md-6">
            <div class="propertyCardOuter card">
            <a href="addWishList.php?propertyID=<?php echo $row['property_ID']; ?>"><div title="add to watchlist" class="displayWishAdd"><div class="plusSymbol"><?php echo $OnWishList;?></div></div></a>
                <a class="propertyCardLink" href="viewProperty.php?propertyID=<?php echo $row['property_ID']; ?>"><div class="card propertyCard">
                    <div class="propertyImgContainer">
                        <img alt="Photo" class="PropertyImagesHome img-fluid" src="<?php echo 'property-images/'.$row['property_image']; ?>" title="<?php echo $row['name']; ?>" />
                        </div>
                    <div class="row">

                        <div class="col-md-9 propertyInfoBox">
                            <div class="propertyName">
                                <?php echo $row['name']; ?></div>
                            <div style="font-size: 14px;">
                                <?php echo $row['location']; ?>
                                </div>
                            <hr>
                            <div class="houseDetails">
                                <?php echo $row['bedrooms']; ?> <i class="fa fa-bed icons"></i><?php echo $row['bathrooms']; ?>
                                <i class="fa fa-bath icons"></i><?php echo $row['garage']; ?>
                                <i class="fa fa-car icons"></i><?php echo $row['landsize'] . 'sqm'; ?>
                                <img src="images/landSizeIcon.png" width="19px;" alt="landsize"/>
                                <span class="propertyPrice"><?php echo '$' . number_format($row['price']); ?></span>
                            </div>

                        </div>
                        <div class=" col-sm-12 col-md-3">
                        <div class="agentContainer">
                            <div class="agentPhotoContainer">
                                <img alt="Photo" class="agentImage" src="<?php echo 'property-images/'.$row['agent_image']; ?>" title="photo" />
                            </div>
                            <div style="text-align:center; margin-top: 10px;"><?php echo $row['agent_name']; ?></div></div>
                        </div>
                    </div>
                </div></a></div>
                <h3 style="text-align:right;">
            </h3>


            </div>
            <?php
        $propertyID2 = $row['property_ID'];
        $query2 = "SELECT * FROM tbl_wishlist WHERE member_ID='$memberID' && property_wishList_ID='$propertyID2' ";
        $result2 = mysqli_query($link, $query2); // execute the SQL 
        if ($row = mysqli_fetch_array($result2)) {
           $OnWishList = "<span id='tickSpan'>&#10003;</span>";
           }
    else {
    $OnWishList = "<span id='plusSpan'>+</span>";
        }
        }
    } // end of while loop
    }
    ?>

我希望 $OnWishList 变量显示从 while 循环中的第一个属性开始的刻度和加号。但是,符号显示时会跳过循环中的第一个属性,并在第二个属性上显示其符号,依此类推。

标签: phphtmlmysql

解决方案


尽管存在一些争议,但通常认为它会产生更好的性能,以便在任何循环之前制作准备好的语句并在循环中执行多次并传入不同的变量。这就是使用 aprepared statement并将变量分配给sql中的占位符。通过在第一次迭代进入循环时将此代码移动到循环顶部附近,它将在您尝试使用变量之前被调用$OnWishList

<?php

    $sql='select * from `tbl_wishlist` where `member_id`=? and `property_wishlist_id`=?';
    $stmt=$link->prepare( $sql );
    $stmt->bind_param( 'ii', $memberID, $wishlistid );

    while( $row = mysqli_fetch_array( $result ) ) {

        extract( $row );




        $wishlistid=$row['property_ID'];

        $stmt->execute();
        $stmt->store_result();
        $count=$stmt->num_rows;

        $OnWishList=$count > 0 ? "<span id='tickSpan'>&#10003;</span>" : "<span id='plusSpan'>+</span>";

?>



    <div class="col-md-6">
        <div class="propertyCardOuter card">
            <a href="addWishList.php?propertyID=<?php echo $row['property_ID']; ?>">
                <div title="add to watchlist" class="displayWishAdd">
                    <div class="plusSymbol"><?php echo $OnWishList;?></div>
                </div>
            </a>
            <a class="propertyCardLink" href="viewProperty.php?propertyID=<?php echo $row['property_ID']; ?>">
                <div class="card propertyCard">
                    <div class="propertyImgContainer">
                        <img alt="Photo" class="PropertyImagesHome img-fluid" src="<?php echo 'property-images/'.$row['property_image']; ?>" title="<?php echo $row['name']; ?>" />
                    </div>
                    <div class="row">

                        <div class="col-md-9 propertyInfoBox">
                            <div class="propertyName">
                                <?php echo $row['name']; ?>
                            </div>
                            <div style="font-size: 14px;">
                                <?php echo $row['location']; ?>
                            </div>
                            <hr>
                            <div class="houseDetails">
                                <?php echo $row['bedrooms']; ?><i class="fa fa-bed icons"></i>
                                <?php echo $row['bathrooms']; ?><i class="fa fa-bath icons"></i>
                                <?php echo $row['garage']; ?><i class="fa fa-car icons"></i>
                                <?php echo $row['landsize'] . 'sqm'; ?><img src="images/landSizeIcon.png" width="19px;" alt="landsize"/>
                                <span class="propertyPrice"><?php echo '$' . number_format($row['price']); ?></span>
                            </div>
                        </div>
                        <div class=" col-sm-12 col-md-3">
                            <div class="agentContainer">
                                <div class="agentPhotoContainer">
                                    <img alt="Photo" class="agentImage" src="<?php echo 'property-images/'.$row['agent_image']; ?>" title="photo" />
                                </div>
                                <div style="text-align:center; margin-top: 10px;"><?php echo $row['agent_name']; ?></div>
                            </div>
                        </div>
                    </div>
                </div>
            </a>
        </div>
        <h3 style="text-align:right;"></h3>
    </div>

<?php

    }//end while loop

?>

推荐阅读