title: making a instant messenger box with php and mysql?
body: <p>i am trying to create an instant messaging box for users on my site, badically if a user goes onto another users profile they can write in the chat box and send an instant message to the other user.</p>
  
  <p>I'm having trouble getting the mysql to get the results and filter it so that if
  user 1 messages user 2 then only user 1 and user 2 can see the conversation.</p>
  
  <p>At the moment though only the logged in user or ".$_SESSION['user_id']." can see the conversation but i want both users to be able to see their conversation between each other. </p>
  
  <p>the other problem is that if user 3 messages user 2 as well as user 1 then user 2 gets all of the conversations from user 1 and 3 but i only want one conversation between each user per box.</p>
  
  <p>my database ptb_chats looks like this:</p>
  
  <pre><code>id  |   to_user_id  |  from_user_id   | date_added|   content  |
  
  2            2               1         April 2011      hello
  </code></pre>
  
  <p>here's my php:</p>
  
  <pre><code>&lt;?php
      $chat_set = get_chats();
  while ($chat = mysql_fetch_array($chat_set)) { ?&gt;
               &lt;?php echo "&lt;div class=\"chat_row\"&gt;".$chat['content']."&lt;/div&gt;"; ?&gt;
          &lt;? } ?&gt;
  </code></pre>
  
  <p>here's my mysql:</p>
  
  <pre><code>function get_chats() {
              global $connection;
              global $profile_id;
              $query = "SELECT *
                          FROM ptb_chats, ptb_profiles
                          WHERE ptb_profiles.user_id = ptb_chats.from_user_id
                          AND ptb_chats.to_user_id=".$_SESSION['user_id']."
                          ORDER BY ptb_chats.date_added ASC";
              $chat_set = mysql_query($query, $connection);
              confirm_query($query, $connection);
              return $chat_set;
          } 
  </code></pre>
  
  <p>please can someone show me what i need to do to get this to work? thanks</p>