【问题标题】:Displaying link if no row exists in the database, otherwise display text如果数据库中不存在行,则显示链接,否则显示文本
【发布时间】:2016-04-08 22:39:27
【问题描述】:

我的数据库中有一个名为flagged_posts 的表,它包含以下列:

id
thought_id
flagged_by_id

我想要做的是,如果登录用户已经标记了帖子,那么不要让他们再次标记帖子,我试图通过删除锚链接并将其替换为一条消息。

这是我的代码的 sn-p:

<?php

$query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' "."ORDER BY id DESC LIMIT {$start}, {$limit}");

while ($row = mysqli_fetch_array($query)) {
    $thought_id      = $row['id'];
    $message_content = $row['message'];
    $date_of_msg     = $row['post_details'];
    $thoughts_by     = $row['added_by'];
    $attachent       = $row['attachment'];
    $shared          = $row['shared'];

    // getting the id of the user who is logged in.
    $see_if_flagged_q = mysqli_query($connect, "SELECT id FROM users WHERE username = '$username'");
        $getting_deets = mysqli_fetch_assoc ($see_if_flagged_q);
            $logged_in_user_id = $getting_deets ['id'];

    echo "
        <div class='more_options' style='float: right;'>"; 
            $see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id ='$logged_in_user_id' ");
                while ($getting_deets2 = mysqli_fetch_assoc ($see_if_flagged_q2)){
                        $flagged_post_by_id = $getting_deets2 ['flagged_by_id']; 

                    // If the user logged in has not flagged the post, i.e. there is no data in the database ..
                    // .. which says their user id has flagged this thought_id.. then display the link...       
                    if ($logged_in_user_id == $flagged_post_by_id){
                            echo "<a href='/inc/flagged_post.php?id=$thought_id'> Flag </a>";
                    } 
                    // if there is data stating this user has flagged this thought_id, then echo a message
                    if ($logged_in_user_id != $flagged_post_by_id) {
                            echo "Flagged";
                    }

                }
    echo "  </div>";                
}
?>

所以假设我以Conor 登录。 Conor 的 id 为 8(从 users 表获得的 ID)。 Conor 标记一个 id 为 209 的帖子(thought_id 来自user_thoughts 表)。所以在我的flagged posts 表中,我将看到以下行:

id: 1
thought_id: 209
flagged_by_id: 8

目前,链接和消息都没有出现。如果我更改我的查询,即$see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts ");(删除了 WHERE 子句),那么我会收到四次回显消息Flagged(因为flagged_posts 表中有四行,它们在每个帖子上都是回显,甚至那些没有被登录用户标记的。

更新:

首先是更新后的代码:

            $see_if_flagged_q2 = mysqli_query($connect, "SELECT * FROM flagged_posts WHERE flagged_by_id = '$logged_in_user_id'");
            $test_num = mysqli_num_rows ($see_if_flagged_q2);
                $getting_deets2 = mysqli_fetch_assoc ($see_if_flagged_q2);
                        $flagged_post_by_id = $getting_deets2['flagged_by_id'];

                        if ($flagged_post_by_id == $logged_in_user_id){
                            echo "<a href='/inc/flagged_post.php?id=$thought_id'> Flag </a>";
                            echo $test_num;
                        } 
                        if ($flagged_post_by_id != $logged_in_user_id) {
                            echo "Flagged";
                        }

有了以上内容,现在所有帖子的链接都会出现,即使它们被标记。我已经回显了 $flagged_post_by_id 和 '$logged_in_user_id',它们都回显了 12 的值(users 表中的 Conor 的 ID)。值是正确的,$test_num 返回的行数也是正确的。

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    好的,这是对原始代码的修改。我将数据收集部分移到了前面,因此在对想法运行 while 循环之前,我们有一个设置部分。我在这里和那里更改了一个变量名。基本上,我们建立一个标记条目列表,然后在 while 循环中,工作更简单。如果当前行 id 在 flagged_posts 数组中,它被标记,否则显示链接。

    // get the id of the current user
    $user_id_q = mysqli_query($connect, "SELECT id FROM users WHERE username = '$username'");
    $getting_deets = mysqli_fetch_assoc($user_id_q);
    $logged_in_user_id = $getting_deets['id'];
    
    // build array of posts flagged by current user
    $flagged_posts_q = mysqli_query($connect, "SELECT thought_id FROM flagged_posts WHERE flagged_by_id = '$logged_in_user_id'");
    $flagged_posts = array();
    while ($row = mysqli_fetch_array($flagged_posts_q)) {
        $flagged_posts[] = $row['thought_id'];
    }
    
    $query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' "."ORDER BY id DESC LIMIT {$start}, {$limit}");
    
    while ($row = mysqli_fetch_array($query)) {
        //You could just use $row['foo'] down below, and skip all this
        /* 
        $thought_id      = $row['id'];
        $message_content = $row['message'];
        $date_of_msg     = $row['post_details'];
        $thoughts_by     = $row['added_by'];
        $attachent       = $row['attachment'];
        $shared          = $row['shared'];
        */
    
        echo "<div class='more_options' style='float: right;'>"; 
            if (in_array($row['id'], $flagged_posts)){ 
                echo "Flagged";
            } else {
                echo "<a href='/inc/flagged_post.php?id=".$row['id']."'> Flag </a>";
            }
        echo "</div>";                
    }
    

    【讨论】:

    • 这似乎部分有效。我有一个thought 和一个id210 被登录的用户标记。当我想到那个想法时,它说Flagged 和那些没有标记的,提供了链接,这正是我想。但是,然后我决定清除flagged_posts 表中的所有行,然后我在$flagged_posts 上看到未定义的变量错误,并且`in_array() 期望参数2 是数组,给定. Basically, when there are no rows in the flagged_posts` 表为空,我得到了错误。如果有一行,那么代码也完全按照我想要的方式工作。第 1/2 部分。
    • 我在flagged_posts表中手动插入了一行,说明212thought_id(不存在),已被ID为12的用户标记(登录用户)。然后代码工作。似乎它只需要在flagged_posts 表中存在一行即可工作。第 2/2 部分。
    • 啊,我刚刚又测试了。所以我以 ID 为 12 的 Conor 身份登录。如果 flagged_thoughts 表中没有 flagged_by_id 为 12 的行,则错误将显示在页面上。
    • 答案已编辑 - 我们只需要初始化数组,使其始终是一个数组,即使它是空的。
    • 完美运行 :) 感谢您所做的一切!
    猜你喜欢
    • 2017-09-18
    • 2022-12-06
    • 1970-01-01
    • 2015-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-14
    • 1970-01-01
    相关资源
    最近更新 更多