Estoy tratando de configurar la variable $ OnWishList para mostrar una marca o un símbolo más, dependiendo de si la propiedad está en la lista de deseos. Aunque hace esto, omite la primera propiedad en el bucle wile y muestra el símbolo correcto para la primera propiedad en la segunda propiedad. ¿Cómo puedo hacer que los símbolos se muestren en las propiedades correctas? Pido disculpas si esta es una pregunta simple, ya que soy nuevo en PHP y SQL y he tenido problemas con este problema por un tiempo.

$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
    }
    ?>

Me gustaría que la variable $ OnWishList muestre los símbolos tick y plus que comienzan con la primera propiedad en el ciclo while. Sin embargo, los símbolos se muestran omitiendo la primera propiedad en el bucle y mostrando su símbolo en la segunda propiedad y así sucesivamente.

1
1hunkyducky 12 oct. 2019 a las 09:32

3 respuestas

La mejor respuesta

Generalmente se considera, aunque existe cierto debate, que ofrece un mejor rendimiento para elaborar una declaración preparada antes de cualquier bucle y ejecutarla varias veces dentro de un bucle con diferentes variables que se pasan. Esa era la intención aquí al usar un {{X0} } y asignando variables a marcadores de posición en el sql. Al mover este código a cerca de la parte superior del bucle en la primera iteración, se llamará antes de intentar usar la variable $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

?>
1
RamRaider 16 oct. 2019 a las 13:24

Hay un gran error en su segunda cláusula if:

if ($row = mysqli_fetch_array($result2)) {
//       ^ only one equal sign !!
       $OnWishList = "<span id='tickSpan'>&#10003;</span>";
       }
else {
$OnWishList = "<span id='plusSpan'>+</span>";
// this will NEVER be executed
    }

Entonces, en lugar de verificar si la fila es igual a algo, en realidad está verificando si $row se puede asignar mysqli_fetch_array($result2) Por lo tanto, la cláusula else se ejecutará solo si no puede asignar un nuevo valor a la variable $row.

0
Pavel Janicek 12 oct. 2019 a las 07:30

Compruebe la primera iteración en el ciclo while como

<?php 
$i=1;
while($row = mysqli_fetch_array($result)) {    
?>
 <?php 
if($i==1){
echo "Yes or your code";
}
else
{
   echo "no or your code";
}
?>

<?php $i++; } ?>
0
Dharman 12 oct. 2019 a las 08:34