I work on a message system. in this message system admin can send messages to single user, user group or all users and users just can see messages.
know i want to find unread messages in user panel.
i designed two database table like this :
1- msg_messages :
| id | title | messages | date | status |
2- msg_control :
| id | message_id | from | toUser | toGroup | status |
Know i think about this two methods of saving users who saw the messages.
Method 1 : create a string with readers id Like this : 1,5,9,12,... and check users id with this string by Strpos php function or in_array function.
Method 2 : create a new table like below table and save readers id in that :
| id | message_id | readers_id | date |
which one of this two method is better ?
A new way to find unread messages ...
after think more about this problem i decided to use a new method by combining method 1 and method 2. actually i added a new column to user table and named it read_msg . system will save read message_id in this filed in a string like this 1,5,9,12,98,125,... for each user (message_id in this filed refer to msg_control table ) and when we want to find unread messages just need to compare msg_control ids with this filed.
we use strpos to compare read_msg and message_id because its faster than is_array (reference).
strposorin_arrayin php you may want to use MySQLsNOT FIND_IN_SET(1, readers)if you are using a a set to find unread messages.