如果条件为真则显示 div

我之前问过这个问题并得到了答案,但我想做一些不同的事情。我正在创建一个社交网站,让人们可以互相发送消息。opened如果列是,我想用蓝色背景显示 div == 0。但我现在只显示 else 列中的 div,该列是空白的。


因此,即使条件成立并且背景颜色应该显示蓝色,背景颜色也会立即直接变为其他颜色。有什么帮助吗?


public function getConvos () {


    $userLoggedIn = $this->user_obj->getUsername();

    $return_string = "";

    $convos = array();


    $query = $this->con->prepare('SELECT user_to, user_from, opened FROM messages WHERE

     user_to = ? OR user_from = ? ORDER BY id DESC');

    $query->bind_param("ss", $userLoggedIn, $userLoggedIn);

    $query->execute();

    $query_result = $query->get_result();


    while ($row = $query_result->fetch_assoc()) {


        $user_to_push = ($row['user_to'] != $userLoggedIn) ? $row['user_to'] : $row['user_from'];


        if(!in_array($user_to_push, $convos)) {

            array_push($convos, $user_to_push);

        }


        $color = ($row['opened'] == '0')  ? "#DDEDFF" : "";

    }



    foreach ($convos as $username) {


        $user_found_obj = new User($this->con, $username);

        $latest_message_details = $this->getLatestMessage($userLoggedIn, $username);

        

        $return_string .= "<a href='messages.php?u=$username'><div class='user_found_messages'

                        style='background-color: $color;' >

                        <img src='" . $user_found_obj->getProfilePic() . 

                        "' style='border-radius: 5px; margin-right: 5px;'> 

                        ". $username ."<br/>".

                        $latest_message_details . "</div></a>";

    }


    return $return_string;

}

.user_found_messages {


padding-left: 10px;

padding-top: 20px;

padding-bottom: 10px;

height: auto;

border-bottom: 1px solid #d9d9d9;

}


.user_found_messages:hover {


background-color: #e6ffff;

color: #669999;

}


.user_found_messages img {


height: 105px;

float: left;

margin-top: 1px; 

margin-right: 5px;

}


元芳怎么了
浏览 101回答 1
1回答

慕尼黑的夜晚无繁华

这个问题是由于每次迭代中的 $color 覆盖而发生的。尝试这个:$i = 0;while ($row = $query_result->fetch_assoc()) {&nbsp; &nbsp; $user_to_push = ($row['user_to'] != $userLoggedIn) ? $row['user_to'] : $row['user_from'];&nbsp; &nbsp; if(!in_array($user_to_push, $convos)) {&nbsp; &nbsp; &nbsp; &nbsp; array_push($convos, $user_to_push);&nbsp; &nbsp; }&nbsp; &nbsp; $color[$i] = ($row['opened'] == '0')&nbsp; ? "#DDEDFF" : "";&nbsp; &nbsp; $i++;}foreach ($convos as $key => $username) {&nbsp; &nbsp; $user_found_obj = new User($this->con, $username);&nbsp; &nbsp; $latest_message_details = $this->getLatestMessage($userLoggedIn, $username);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $return_string .= "<a href='messages.php?u=$username'><div class='user_found_messages'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; style='background-color: ". $color[$key]; ."' >&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src='" . $user_found_obj->getProfilePic() .&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "' style='border-radius: 5px; margin-right: 5px;'>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ". $username ."<br/>".&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $latest_message_details . "</div></a>";}
打开App,查看更多内容
随时随地看视频慕课网APP