douqiju2520 2016-06-23 18:48
浏览 30
已采纳

其他声明没有出现

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

  • 写回答

1条回答 默认 最新

  • doupishan3309 2016-06-23 20:06
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?