I have a private messaging script, where users can send messages to each other.
I have a problem showing if the user viewed the message.
What I am trying to do is that if:
- User1 sent the PM to User2
- The PM should be marked viewed for user1, but not user2.
- User2 replies to the PM to User1
- The PM should be marked viewed for user2, but not user 1.
In my database I have:
viewed_sender
and viewed_receiver
. They are either 0 or 1 (1 if the message has been viewed, and 0 if it hasn't)
viewed_sender
standard value is of course 1.
When writing a reply post, I update viewed_sender
and viewed_receiver
like this:
if($pmData['sender_id'] == $userdata['id']): //If you're the sender
$sender = 1;
else:
$sender = 0;
endif;
//Update viewed $id is the id of the conversation.
if($sender == 0){
mysql_query("UPDATE private_messages SET viewed_receiver='0' WHERE id='$id'");
if(mysql_affected_rows()==0)
return 2;
}
Then when I am showing whether the message is viewed for the logged in user, I do it like this:
//Viewed.
if($convoData['sender_id'] == $userdata['id']):
$sender = 1;
else:
$sender = 0;
endif;
if($sender == 1 && $convoData['viewed_sender'] == 1){
$viewed = false;
}elseif($sender == 0 && $convoData['viewed_receiver'] == 0){
$viewed = true;
}else{
$viewed = false;
}
if($viewed == true){
echo '<img src="/images/icons/comments.png" style="border-radius: 0px !important;-moz-border-radius:0px !important;-webkit-border-radius: 0px !important;float: right;margin-right: 30px;" height="12" width="12">';
}
That doesn't work. What am I doing wrong in the if statement?