3

I am currently stuck with my logic.

I have created a database table for snedimg messages

table messages looks like this

id | sender_id | reciver_id | msg                      | send_date |
1  | 2         |  6         | hello there i am testing | 2013-01-15 

So what i am totally stuck with is the removing the message logic. The problem what i dont understand how to solve is, lets say user with the id 6 gets the message, and removes it by id, that way who sent the message wont be able to see it either.

So i am a bit lost at this, if someone could give me a hint on a logic i would really be happy

EDIT WITH MORE EXPLAINED DETAILS

So the problem is

id | sender_id | reciver_id | msg                      | send_date |
1  | 2         |  6         | hello there i am testing | 2013-01-15 

this is the table. User recives the message, reads it, and for some reason wants to remove it.

Probmel is the DELETE LOGIC, i know how to delete rows, what i dont know is how to solve the problem if the reciver deletes the message, the sender will be able to see it un the he/she deletes it.

For example on facebook they use archive instead of delete, this is the logic what i dont understand

3
  • the message should be duplicated, add column type = ENUM('sent','received')
    – user1646111
    Commented Feb 2, 2013 at 15:30
  • 1
    What about adding two columns active_for_sender and active_for_reciver then just put a non-zero for active and a 0 for not active or "deleted"
    – Cjueden
    Commented Feb 2, 2013 at 15:32
  • Not enough clear for me. Why you want to remove the message? Commented Feb 2, 2013 at 15:32

2 Answers 2

3

Just set the reciver or sender id to -1 on deletion and have a check afterwards if both are -1 (or would be -1) delete that row.

e.g.

on deletion by reciver:
if ($sender_id == -1) {
  $query = "DELETE FROM messages WHERE id=$id";
} else {
  $query = mysql_query("UPDATE messages SET reciver_id=-1 WHERE id=$id";
}
mysql_query($query)

That way it would be still availabe until both delete it

Edit:
Just as additional thought for that solution:
You could set both to -1 instead of deleting it on the second user deletion so you got a kind of archive and delete too old ones by a cronjob.

1

if you want sender see the message even after it's deleted by the receiver, you can use the following logic:

  1. Add a new column to your table named deleted, where values 1 means the message has been deleted, and 0 means it's not deleted yet.
  2. If this is the receiver try to see their messages: WHERE deleted = 0
  3. If this is the sender simply show all; don't use the above WHERE clause.
  4. for removing the message, in your PHP delete function first check the value of deleted, if it is 1 you can permanently delete the row from your table using SQL DELETE. Otherwise, you just need to update the value of deleted column to 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.