/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=html]$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>”;
}[/code]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@dangerousprinceauthorDec 18.2011 — No worries, found the problem - just needed to take:

[code=html]$user_array = array();[/code]

out of the while loop
×

Success!

Help @dangerousprince spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...