Need help displaying information correctly

Here is my code:

[php]<?php
include(‘user.php’);

$user = new User(); // new instance of User
$names = $user->retrieveNames(); // call retriveNames() method and assign the result to variable names
echo “”;
$select_box = ‘’; // assign list to variable $select box
foreach ($names as $name) { // loop
// $name is an array holding the user_id and name
list( $uid,$uname ) = $name; // assign name to a list
$select_box .= ‘’. $uname .’’; // list the users in the drop down list and assign the user id as the options value
}
$select_box .= “”;
print $select_box; // print the list
echo “

”;
echo “”;

if (isset($_POST[‘user_id’])){ // checks if the form was submitted
$user = new User(); // new instance of User
$user_id = $_POST[‘user_id’]; // assign the user id to variable user_id
$user->getUserById($user_id); // call the getUserById method
echo "

User known as: " .$user->getDisplayName(). “

”; // call method getDisplayName()

$friends = $user->friendsNames(); // call method getFriends()

    // The above works perfectly fine, this is where I need assistance
echo "<p>Friends with: ".implode(',',$friends). "</p>"; 

echo "<p><a href='test.php'>Return</a></p>";

}
echo “”;
?>[/php]

The above displays the following info:

User known as: Peter Pan

Friends with: Pollen Ndlanya,Kaizer Motaung

Return

which is the correct info but I want it to be displayed as:

User known as: Peter Pan

Friends with: Pollen Ndlanya

Friends with: Kaizer Motaung

Return

Here is the user.php code

[php]<?php
class User{ // user class

public function getUserById($user_id){ // method getUserById()
$sql = 'select first_name, last_name, display_name from users where user_id = '.$user_id; // select name and display name from users table
$results = mysql_query($sql); // assign query to variable $results
$row = mysql_fetch_array($results); // array
$this->user_id = $user_id; // create a property
$this->fist_name = $row[‘first_name’]; // create a property
$this->last_name = $row[‘last_name’]; // create a property
$this->display_name = $row[‘display_name’]; // create a property
return true;
}

public function getDisplayName(){ // method getDisplayName()
return $this->display_name; //return display nmame
}

public function getFriends(){ // method getFriends()
$sql = 'select friend_id from user_friends where user_id = '.$this->user_id; // select freind id from users_friends table
$results = mysql_query($sql); // assign query to variable $results
$friends = array(); // array
while($row = mysql_fetch_array($results)){ // loop through the array
$friends[] = $row[‘friend_id’]; // place all ‘friend id’ values in an array called $friends
}
return $friends; // return users friends
}

public function retrieveNames() // method retrieveNames()
{
$sql = “select user_id, first_name, last_name from users”; //first_name and last_name is the fields of the database i need to list
$qry = mysql_query($sql); // assign query to variable $qry

 while ($row = mysql_fetch_array($qry)) { // loop through the array
	 $names[] = array( $row['user_id'], $row['first_name']. " ".$row['last_name'] ); // place all 'name' values selected in an array called $names
	 // pass the user_id and name separately
 }

 return $names; // return the names

}

public function friendsNames(){ // method getFriends()
$sql = 'select friend_name from user_friends where user_id = '.$this->user_id; // select freind id from users_friends table
$results = mysql_query($sql); // assign query to variable $results
$friends = array(); // array
while($row = mysql_fetch_array($results)){ // loop through the array
$friends[] = $row[‘friend_name’]; // place all ‘friend id’ values in an array called $friends
}
return $friends; // return users friends
}

}

?>[/php]

Any assistance will be appreciated

Thanks

Well, it does exactly what you programmed it to do. It displays a list from the array.
To fix it, you would have to loop thru the array doing one display at a time.
So, this code which displays the ENTIRE array:
// The above works perfectly fine, this is where I need assistance
echo "

Friends with: ".implode(’,’,$friends). “

”;
echo “

Return

”;
Would have to be changed to something like this: (not tested…)
// The above works perfectly fine, this is where I need assistance
foreach ($friends as $friend)
echo "

Friends with: " . $friend . “

”;
echo “

Return

”;

Hope that helps…

Yes it does, thanks :smiley:

Glad I could help… Good luck with your project!

Sponsor our Newsletter | Privacy Policy | Terms of Service