简体   繁体   中英

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.

how to do this? any idea?

here is my code from my 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:

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

this is a broad example though. You really should join the messages table on the users table

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.

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.

So you have an username in the messages table that means that you maybe have an user table with all the users informations. 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.

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

I hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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