There is such a table how to change it so that you can count the number of new messages for each user:

user_id admin_id subject text 

    2 answers 2

    Hello

    First of all, you need to add a column to your table like: active If this line equals 1, the message is Unread.

    After that, we can use this code, (I will do it in sessions) :

     <?php session_start(); $sql = mysql_query("SELECT * FROM message WHERE user_id='".$_SESSION['user_id']."' AND active='1'"); if(mysql_nuw_rows($sql) > 0){ echo "У Вас новых сообщений: ".mysql_nuw_rows($sql); } ?> 

    Code Explanations:

    1. Table message - This is your message table.
    2. mysql_nuw_rows() - Returns the number of rows.
    3. If active = 1 - Message not read

    I hope that I clearly stated :)

    • After issuing the user / click on the tag "read" - reset the flag "active" ajax'om or in the usual way. - istem
    • @istem, Yes, you are right :) - k0mar
    • 2
      Only active = 1 is a terribly unthinkable name / value. Better to somehow beat the words read, shown or something else like that. IMHO, of course. - xEdelweiss

    On good, you should have at least two tables:

     1) Таблица пользователей(и админов тоже): user_id, user_type и т.д. 2) Таблица сообщений: message_id, message_text, message_status, sender_id, reciever_id 

    Information about the readability of the message is stored in message_status.

    In general, there is such a thing as normal forms, and so I remember it very vaguely. Perhaps it would be much more intelligent to make 3 tables.

    UPD: Number of new posts:

     SELECT COUNT(*) FROM message_table WHERE reciever_id = user_id AND message_status = 0 

    user_id - ID of the user whose messages we count

    message_status = 0, provided that 0 means unread message, and -1 read