0

我有一个私人消息系统,我有这个函数可以返回对话中所有用户的 ID(发件人除外):

function findOtherUsersInConversation($conversation_id) {
    $sender = findMessageSenderId($conversation_id);
    $query  = mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
    while ($row = mysql_fetch_array($query)) {
        $user_id = $row['user_id'];
        print_r($user_id);
    }
}

print_r 像这样返回 ID(例如 id100 和 id 101):

100101//which is not what i'm trying to do

我有另一个函数可以在数据库中查找用户名,因此对于每个用户 ID,我想以这种格式获取他们的用户名:

echo usernameFromId($user_id)// this should echo out all the username like this (user a, user b, user c)

我想我必须做一个 foreach 循环,但我想不出怎么做。

4

4 回答 4

1

尝试这个:

function findOtherUsersInConversation($conversation_id){
    $sender = findMessageSenderId($conversation_id);
    $query =  mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
    $users = array();
    while ($row = mysql_fetch_array($query)) {
        $users[] = usernameFromId($row['user_id']); // fetch user name and add it to array
    }
    return implode(', ', $users); // return a string separated by commas
}

findOtherUsersInConversation(10); // conversation id 10
于 2012-10-01T10:12:15.373 回答
1

像这样试试

function findOtherUsersInConversation($conversation_id) {
$sender = findMessageSenderId($conversation_id);
$query  = mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id =   '$conversation_id' AND user_id !=$sender");
$cnt=0;
while ($row = mysql_fetch_array($query)) {
    $user_id = $row['user_id'];
   if($cnt==0):
       $comma_separated .=$user_id;
   else: 
      $comma_separated .=",".$user_id;
   endif;   
   $cnt++;
}
 return $comma_separated
}

$getID=findOtherUsersInConversation(10); 

$arrayID= explode( ',', $getID);// split string from comma(,)

print_r($arrayID);// print all ID's as you want

愿这会帮助你。

于 2012-10-01T10:37:35.840 回答
0
function findOtherUsersInConversation($conversation_id){
  $sender = findMessageSenderId($conversation_id);
  $query =  mysql_query("SELECT user_id FROM message_partecipant WHERE conversation_id ='$conversation_id' AND user_id !=$sender");
  $usernameArr = array();
  while ($row = mysql_fetch_array($query)) {
    $user_id= $row['user_id'];
    array_push($usernameArr, usernameFromId($user_id));
  }

  $comma_separated = implode(",", $usernameArr);

  echo $comma_separated;
}
于 2012-10-01T10:20:01.377 回答
0

如果您只想查看您的信息数组,请尝试:

var_dump($array);

否则在 foreach 中尝试输出您的数组:

foreach($array as $var){
    echo $var;
}
于 2012-10-01T10:23:05.437 回答