I am working on a Private Messaging Service (PMS) on my website. This is how my 'message' table on my MySQL database look like.

id sender_id receiver_id message
1 2 3 hi
2 3 2 You good?
3 2 3 sure
4 1 3 hello
5 3 1 yes

Now, i am confused about something. I want that when a user (that is when $_SESSION['id' = 3]) visits the 'message' page, what shows up is the last message from a conversation with the last conversation coming at the top. Something like this:

  1. Yes
  2. sure

Then, when the user clicks on for example, 'Yes', he gets to see all the conversation he had with user with id = 2, i.e.

hi

You good?

sure.

Please how do I go about this?

Member Avatar for diafol

this will do for a simple system.

SELECT ... FROM table WHERE $_SESSION['id'] IN(sender_id, receiver_id) ORDER BY id DESC

However sometimes u want to have several different conversations with same person, so each conversation has its own title etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.