www.webdeveloper.com
Results 1 to 2 of 2

Thread: Message system - user select

  1. #1
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96

    Exclamation Message system - user select

    Hey guys

    I have an SQL table with users' messages and here's what the values are:

    id, uid, to, sent, msg, date

    id, the unique ID with auto increment (and primary key)
    uid, the ID of the user who is sending the message
    to, the ID of the user who the message is to
    msg, the message
    date, the timestamp

    The problem I'm having is trying to select the users who the user has sent or received messages from/to. I keep getting repeats or a user is missing. I had an idea of going through each user and going through validation to make sure that person isn't already there but it doesn't seem to work properly.

    $id is from the session variable $_SESSION['id'] which is declared in an include file.

    Any help would me most appreciated.

    Here's my PHP code:

    HTML Code:
    $get_conversations = mysql_query("SELECT DISTINCT `uid`,`to` FROM `messages` WHERE `to`='$id' OR `uid`='$id'");
    				if(mysql_num_rows($get_conversations)>0) {
    					while($fetch_conversations = mysql_fetch_array($get_conversations)) {
    						list($uid,$to) = $fetch_conversations;
    						if($uid==$id) {
    							$user = $to;
    						}
    						if($to==$id) {
    							$user = $uid;
    						}
    						$user_array = array();
    						if(!in_array($user, $user_array)) {
    							array_push($user_array, $user);
    						}
    					}
    // this bit is just where the $user is changed into their username
    					foreach($user_array as $user) {
    						$get_user = mysql_query("SELECT `name` FROM `users` WHERE `id`='$user'");
    						while($fetch_user = mysql_fetch_array($get_user)) {
    							list($name) = $fetch_user;
    						}
    						echo "<li><input type=\"hidden\" value=\"$user\" />$name</li>\n";
    					}
    				} else {
    					//no convos
    					echo "<li>No convos</li>";
    				}

  2. #2
    Join Date
    Aug 2011
    Location
    Worthing, West Sussex
    Posts
    96
    No worries, found the problem - just needed to take:

    HTML Code:
    $user_array = array();
    out of the while loop

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles