TripleDeal TripleDeal - 1 year ago 50
PHP Question

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.

$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;

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


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

Table(huis)(full version:

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


| 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:

Answer Source

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:


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

$stmt = mysqli_prepare($con, $detailsSql) or die(mysqli_error($con));
mysqli_stmt_bind_param($stmt, "i", $gebruikerid);
$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>
    <!-- ... etc ... -->
<?php endwhile; ?>

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download