dsm17496 2015-11-11 08:57 采纳率: 100%
浏览 54
已采纳

聊天管理员不同颜色?

I have developed a simple chat system. I am trying to make the admin color red to stand out among the chat users. My method takes the session username and if the session username is the admin then it turns everyone's text color red. How can I make it so only the admin color is red (everyone else is black)?

Here is my php code:

<?php
    error_reporting(E_ALL & ~E_NOTICE);
    session_start();

    if  (isset($_SESSION['id'])) {
        $userId = $_SESSION['id'];
        $username = $_SESSION['username'];
        $userLabel = $_SESSION['nickname'];
    }

    $connect = mysqli_connect("", "", "", "root");
    mysqli_select_db($connect, "webclyde_root");    
    $result1 = mysqli_query($connect, "SELECT * FROM chat ORDER by id DESC");
    while ($extract = mysqli_fetch_assoc($result1)) {
        if ($username == "admin") {
            echo "<br><b><font color='#D00003'>" . $extract['name'] . ":</b></font> " . $extract['message'];
        }
        else {
            echo "<br><b><font color='#000000'>" . $extract['name'] . ":</b></font> " . $extract['message'];
        }
    }
?>
  • 写回答

2条回答 默认 最新

  • dsdsm2016 2015-11-11 09:03
    关注

    The font tag is not supported in HTML5. Use a CSS style attribute instead, for example:

    echo "<br><b><p style='color:#D00003'>" . $extract['name'] . ":</b></p>" . $extract['message'];
    

    Example for the if / else logic:

    if($username=="admin"){
        $color = "#D00003";
    } else {
        $color = "#000000";
    }
    
    echo "<p style='color:".$color."'>". $extract['name'] . "</p>";
    

    Or use the statement RamRaider mentioned instead of the if / else statement:

    $color = $username=='admin' ? 'red' :'black';
    

    Also, you are checking if the session $username is 'admin'. I can imagine you want to check if the username from the database is 'admin'. In that case replace $username with $extract['name'] in the if statement.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图