简体   繁体   中英

Else statement isn't showing up

I'm working on a school project where people can sell their home and also buy homes. I'm trying to make a "favourite" feature, but the first step isn't working 100%. The first step is to make the heart red if the user has saved a house as favourite if the user didn't save the house as favourite, the heart should be grey.

This is the code I use in the while loop where all houses will return, so $detailsHuis['id'] comes from that while loop.

<?php
$huisid = $detailsHuis['id'];
$getFavoriet = "SELECT * FROM favoriet WHERE persoon_id = $gebruikerid AND huis_id = $huisid";
$dataFavoriet = mysqli_query($con, $getFavoriet) or die(mysqli_error($con));
?>
<?php while($detailsFavoriet = mysqli_fetch_assoc($dataFavoriet)): ?>                   
    <?php if($getFavoriet || mysqli_num_rows($getFavoriet) > 0): ?>
        <span class="favorite" style="margin-top: 0px; color: #f44336;"><i class="fa fa-heart" style="margin-right: 0px;"></i></span>
    <?php else: ?>
        <span class="favorite" style="margin-top: 0px;"><i class="fa fa-heart" style="margin-right: 0px;"></i></span>
    <?php endif; ?>
<?php endwhile; ?>

When mysqli_num_rows is above 0, I see a red heart next to the house information, but when mysqli_num_rows is below 0, I see no heart next to the house information.

$con is created in config.php

$con = mysqli_connect("localhost","root","kerimbjk","huizenverkoop");

if (!$con) {
    echo "Kan geen verbinding maken met MySQL" . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

I would be nice if someone could help me with this problem.

Edit: Table(favoriet):

+--------------------------+
| id | huis_id | persoon_id|
| 1  |   4     |     2     |
| 2  |   6     |     4     |
+--------------------------+

Table(huis)(full version: http://prntscr.com/bk5zkl ):

+------------------------+
| id | prijs | stad      |
| 1  | 146521| Amsterdam |
| 2  | 125932| London    |
+------------------------+

Table(persoon):

+-----------------------+
| id |username|password |
| 1  | test123|encrypted|
| 2  | demo   |encrypted|
+-----------------------+

favoriet means favourite, huis means home, prijs means price, stad means city, persoon means person.

My "house" loop, you can also see the "favourite" loop. I can't past the whole code for some reason, so here it is: http://pastebin.com/Ld2rqcQX

Some issues:

  • You have a loop on an SQL result set with in it another SQL statement that gets executed, which really is not that efficient. It is better to do it with one SQL statement, and outer join in it the favoriet table.
  • You apply the CSS class favorite whether it is a favourite or not, while you should logically only apply for a favourite. Connected to that: don't put a color style in addition to that. That colour should be defined inside the favorite class.
  • Your code is vulnerable to SQL injection because you inject $gebruikersid inside an SQL string. This is not how you should do it. Instead use a prepared statement and pass the argument separately.

Here is some untested code you could use for solving the above issues:

<?php

$detailsSql = 
    "SELECT   huis.*, favoriet.id AS fav_id 
    FROM      huis
    LEFT JOIN favoriet ON favoriet.huis_id = huis.id
    WHERE     favoriet.persoon_id = ?"; 

$stmt = mysqli_prepare($con, $detailsSql) or die(mysqli_error($con));
mysqli_stmt_bind_param($stmt, "i", $gebruikerid);
mysqli_stmt_execute($stmt);
$detailsRes = mysqli_stmt_get_result($stmt);

while ($detailsHuis = mysqli_fetch_assoc($detailsRes)): ?>
<div class="item overzicht-item">
    <!-- ... etc ... -->
            <span class="<?php echo $detailsHuis['fav_id'] ? 'favorite' : '' ?>" 
                  style="margin-top: 0px;">
                <i class="fa fa-heart" style="margin-right: 0px;"></i>
            </span>
    <!-- ... etc ... -->
</div>
<?php endwhile; ?>

The ... etc ... parts can remain like you have it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM