0

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.

2 Answers2

1

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.

andrew
  • 9,313
  • 7
  • 30
  • 61
  • I think it should work, i will try it and then let you know, stay with me. –  Jul 19 '14 at 20:11
  • how can I put class in col `role` for admin and user? –  Jul 19 '14 at 20:13
  • how do you currently know if a user is admin or not? do you have something in the user table to say? – andrew Jul 19 '14 at 20:15
  • well you need it so `ALTER TABLE users ADD role VARCHAR(5)` and change the query to `"SELECT m.*,u.role FROM messages m JOIN users u on u.username = m.username ORDER BY message_time"` – andrew Jul 19 '14 at 20:23
  • what it looks like in your answer ? can you update it in your answer? –  Jul 19 '14 at 20:25
  • ok now I have created col `role` in my users table, only I just need to use your this query? I just want to confirm it. please say yes if i only have to do this. –  Jul 19 '14 at 20:31
  • the users cant join with the same username. –  Jul 19 '14 at 20:33
  • obviously you will need to edit the users table putting either 'admin' or 'user' in the role coluimn – andrew Jul 19 '14 at 20:35
  • ok this is done, but tell me the logic of how the role will insert into the col role with their specific role admin or user? –  Jul 19 '14 at 20:35
  • @Mr.beginner thats why i suggested adding a userid column to the messages table, the userid should match the id of the user in the users table. I used username because you don't have a userid column – andrew Jul 19 '14 at 20:37
  • I have `id` in messages table. –  Jul 19 '14 at 20:39
  • @Mr.beginner yes, but that is the id of the message? not the user that posted it – andrew Jul 19 '14 at 20:40
  • see the structure of both tables, is look like. `users` `id,username,join_date` and `messages` `id,username,message_content,message_time` –  Jul 19 '14 at 20:40
  • exactly thats why you need a userid column in the messages table so that you know which user it belongs to, having a username column is the incorrect method then you can `JOIN users u on u.id = m.userid` instead – andrew Jul 19 '14 at 20:42
  • ok, let me try , if it works I will let you know, stay with me. –  Jul 19 '14 at 20:43
  • yes it works, but there is problem with the message arrangement. see yourself. https://www.4thgenhost.com/chat/chat.php –  Jul 19 '14 at 20:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57615/discussion-between-andrew-and-mr-beginner). – andrew Jul 19 '14 at 20:57
0

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.

Denzyl Dick
  • 293
  • 3
  • 11
  • what does this long `$sql` query??? and what structure should I have in my tables? My tables look like, `users` `id,username,join_date` and `messages` `id,username,message_content,message_time` –  Jul 19 '14 at 20:23
  • Join means "join(user) this table with message where the username of message equal the username of user ". You are on the right path!. – Denzyl Dick Jul 19 '14 at 20:49
  • i dont have message table or not the user, I have `messages` and `users` named tables –  Jul 19 '14 at 20:51