简体   繁体   English

其他语句未显示

[英]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%. 我正在尝试制作“收藏夹”功能,但第一步不能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. 这是我在所有房屋都将返回的while循环中使用的代码,因此$ detailsHuis ['id']来自该while循环。

<?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. 当mysqli_num_rows大于0时,我在房屋信息旁边看到一个红色的心,但是当mysqli_num_rows小于0时,我在房屋信息旁边看到一个红色的心。

$con is created in config.php $ con在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 ): 表(huis)(完整版本: 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. 最喜欢的人表示最喜欢的人,惠斯人表示家,prijs表示价格,stad表示城市,persoon表示人。

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 由于某种原因,我无法遍历整个代码,所以在这里是: 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. 在一个SQL结果集上有一个循环,其中包含另一个要执行的SQL语句,这实际上效率不高。 It is better to do it with one SQL statement, and outer join in it the favoriet table. 最好使用一个SQL语句来完成此操作,然后在其中加入最favoriet表。
  • You apply the CSS class favorite whether it is a favourite or not, while you should logically only apply for a favourite. 无论是否favorite ,都可以应用CSS类favorite夹,而从逻辑上讲,您应该只申请收藏夹。 Connected to that: don't put a color style in addition to that. 与此相关:不要添加任何颜色样式。 That colour should be defined inside the favorite class. 该颜色应在favorite类中定义。
  • Your code is vulnerable to SQL injection because you inject $gebruikersid inside an SQL string. 您的代码容易受到SQL注入的攻击,因为您将$gebruikersid注入到SQL字符串中。 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. ... etc ...部分可以像您拥有的一样保留。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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