1

I am trying to set the variable $OnWishList to display a tick or a plus symbol depending on if the property is in the wish list. Though it does this it skips the first property in the wile loop and shows the correct symbol for the first property on the second property. How can I get the symbols to show on the correct properties. I apologize if this is a simple question as I am new to PHP and SQL and have struggled with this problem for a while.

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

I would like the $OnWishList variable to display the tick and plus symbols starting with the first property in the while loop. However the symbols are displayed skipping the first property in the loop and showing its symbol on the second property and so on.

8
  • Couldn't you just move the code which fetches and sets the $OnWishList value to the start of the loop? Commented Oct 12, 2019 at 6:38
  • It's possible ( I should imagine ) to remove the query within the loop altogether and fetch the required information in the main query -- impossible to say for sure without seeing that query. It would make the code much cleaner and less impactful on the db Commented Oct 12, 2019 at 6:51
  • sorry I didnt include the large concatenated query above the while loop. I have tried moving the code to the top but that results in each of the $row values to have "Undefined Index". Commented Oct 12, 2019 at 7:21
  • You can still add the original query if you want, it might help Commented Oct 12, 2019 at 7:58
  • I added the query which is very messy and unstructured. However I don't have much time to finish this so rather than advice on fixing that I would just like the original question answered Commented Oct 12, 2019 at 8:08

3 Answers 3

1

It is generally considered, though there is some debate, that it yields better performance to craft a prepared statement before any loops and execute multiple times within a loop with different variables being passed in. That was the intention here in using a prepared statement and assigning variables to placeholders in the sql. By moving this code to near the top of the loop on the first iteration into the loop it will be called before you attempt to use the 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

?>
Sign up to request clarification or add additional context in comments.

6 Comments

I changed my code to this but I only seem to get + symbols for all properties and no ticks for the properties on the wishlist.
removed the count(*) in the sql - replaced with simply * as it was initially. thanks @Dharman for pointing out my mistook
thanks for the help but still getting all + symbols for every property
I'll have a play with a test case later when I get home
i started doing a mockup but the db tables I chose were not suitable - then other things cropped up and I left it... I'll have a quick look now
|
0

There is huge error in your second if clause:

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
    }

So instead checking if the row is equal to something, you are actually checking, if $row can be assigned mysqli_fetch_array($result2) Therefore, the else clause will be executed only if you cannot assign new value to the $row variable.

2 Comments

Im not sure what you mean, but that code is working. It is actually causing the desired result. The problem I am having is that the $OnWishList is only being changed after the first iteration of the while loop. So the first property misses out on having $OnWishList set to+ or tick symbol. Its just empty.
Maybe because the $row changed value after first loop?
0

Check first iteration in while loop like

<?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++; } ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.