简体   繁体   English

PHP:如何显示与MySQL中其他行不同的特定行?

[英]PHP:how to show specific row different from others in MySQL?

I am creating a php chat application and I have two panels of this chat, 1 is admin area and other is client chat area and the content in both panel areas shows from 1 table called messages I want to show only admin content or name in different colour other than the client users. 我正在创建一个php聊天应用程序,我有两个聊天面板,一个是管理员区域,另一个是客户端聊天区域,两个面板区域中的内容均来自1个表,称为messages我只想显示管理员内容或名称客户端用户以外的颜色。

how to do this? 这个怎么做? any idea? 任何想法?

here is my code from my display_messages.php 这是我的display_messages.php代码

session_start();
require_once 'cn.php';
require_once 'protect.php';



$fiveMinutesAgo = time() - 1000;

$sql = "SELECT * FROM messages ORDER BY message_time  ";
$result = mysqli_query($cn,$sql) or
    die(mysqli_error($cn));



while ($row = mysqli_fetch_assoc($result)) {
    $user = $row['username'];
    $msg_content = $row['message_content'];
    $hoursAndMinutes = $row['message_time'];

    echo '<p><cite class="fa fa-user"><b>'. $user .':</b></cite> <output>'. $msg_content .'</output> <time class="fa fa-clock-o time">: ' . $hoursAndMinutes . ':</time></p>';

}

any help would be appreciated. 任何帮助,将不胜感激。

thanks. 谢谢。

Supposing you had a column in the messages table called role which could be 'admin' or 'user' then your loop might look like: 假设您在messages表中有一列名为role的列,该列可以是“ admin”或“ user”,则您的循环可能类似于:

while ($row = mysqli_fetch_assoc($result)) {
    $user = $row['username'];
    $msg_content = $row['message_content'];
    $hoursAndMinutes = $row['message_time'];
    $class = $row['role'];

    echo '<p class="'.$class.'">'.
            '<cite class="fa fa-user">'.
              '<b>'. $user .':</b>'.
            '</cite>'.
            '<output>'. $msg_content .'</output>'.
            '<time class="fa fa-clock-o time">: ' . $hoursAndMinutes . ':</time>'.
           '</p>';

}

And you could add 您可以添加

.admin{color:red}
.user{color:black}

to your css 给你的CSS

this is a broad example though. 这是一个广泛的例子。 You really should join the messages table on the users table 您确实应该在users表上加入messages表

Edit 编辑

Change your query to: 将查询更改为:

 $sql = "SELECT m.*,u.role FROM messages m ".
        "JOIN users u on u.username = m.username ORDER BY message_time";

and add a column to your users table with 并在列中添加到您的用户表

  "ALTER TABLE `users` ADD `role` VARCHAR(5);"

in your sql client. 在您的sql客户端中。

But this won't work very well if you have more than one user with the same name, the messages table should have a userid column, not a user name column and you should join on that. 但是,如果您有多个具有相同名称的用户,这将无法很好地工作,messages表应该具有一个userid列,而不是一个user name列,并且您应该加入该列。

So you have an username in the messages table that means that you maybe have an user table with all the users informations. 因此,您在messages表中有一个用户名,这意味着您可能有一个user表,其中包含所有用户信息。 If you dont have that YOU SHOULD. 如果您没有,那应该。

session_start();
require_once 'cn.php';
require_once 'protect.php'

$fiveMinutesAgo = time() - 1000;

 $sql = "SELECT messages.username,message.message_content,messages.message_time,user.type as user_type FROM messages JOIN user on messages.username = user.username  ORDER BY message_time  ";
$result = mysqli_query($cn,$sql) or
die(mysqli_error($cn));



  while ($row = mysqli_fetch_assoc($result)) {
      $user = $row['username'];
      $msg_content = $row['message_content'];
      $hoursAndMinutes = $row['message_time'];
      $role = $row['role'] //the user role e.g admin, normal,etc.
      echo '<span class={$role}><p><cite class="fa fa-user"><b>'. $user .':</b></cite> <output>'. $msg_content .'</output> <time class="fa fa-clock-o time">: ' . $hoursAndMinutes . ':</time></p></span>';

 }

Now you need to create a CSS class for every role in your user database. 现在,您需要为用户数据库中的每个角色创建一个CSS类。

.admin{color:pink;} .normal{color:red;} .admin {color:pink;} .normal {color:red;}

I hope this helps you. 我希望这可以帮助你。

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

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