Mat
Mat

Reputation: 6324

Get value from an array

I have a private message system and I have this function that returns the IDs of all the users in the conversation (except the sender):

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 return the Ids (for instance id100 and id 101)like this:

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

I have another function that find the username in the database so for each user id I would like to get their usernames in this format:

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

I think I have to do a foreach loop but I can't think how.

Upvotes: 0

Views: 119

Answers (4)

MCSell
MCSell

Reputation: 315

If you want to view the array only for your Information try:

var_dump($array);

otherwise try it in a foreach to output your array:

foreach($array as $var){
    echo $var;
}

Upvotes: 0

drsndodiya
drsndodiya

Reputation: 1685

Try like this

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

May this will Help you.

Upvotes: 1

Warren
Warren

Reputation: 163

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;
}

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39724

Try this:

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

Upvotes: 1

Related Questions