Displaying information from my Database
Posted: Tue Apr 17, 2012 1:04 pm
Hi
I need help with the following exercise I've been busy with for the past two weeks now and I'm stuck right at the very end
Below is the tables of my db
user table
user_id, first_name, last_name, display_name
1, Peter, Pan, Peter Pan
2, Elvis, Presley, King of Rock
3, Pollen, Ndlanya, Trompies
4, Clark, Kent, Superman
5, Kaizer, Motaung, Chincha Guluva
6, Lucas, Radebe, Roo
user_friends table
user_friend_id, user_id, friend_id
1, 1, 3
2, 1, 5
3, 2, 5
4, 2, 1
5, 3, 4
6, 3, 2
7, 4, 2
8, 4, 1
9, 4, 5
10, 5, 1
11, 5, 3
12, 6, 1
13, 6, 3
Below is my code
user.php
This is the output Im looking for when I select user1, 2, 3,4 and 5:
User known as: Peter Pan // on all users, this dipsplays fine the below is where I need help
Friends with Pollen Ndlanya
Friends with Kaizer Motaung
Return
User known as: King of Rock
Friends with Kaizer Motaung
Friends with Peter Pan
Return
User known as:Trompies
Friends with Clark Kent
Friends with Elvis Presley
Return
User known as: Superman
Friends with Elvis Presley
Friends with Peter Pan
Friends with Kaizer Motaung // This is the one with 3 friends
User known as: Chincha Guluva
Friends with Peter Pan
Friends with Elvis Presley
Return
Any assistance will be much appreciated
Thanks
I need help with the following exercise I've been busy with for the past two weeks now and I'm stuck right at the very end
Below is the tables of my db
user table
user_id, first_name, last_name, display_name
1, Peter, Pan, Peter Pan
2, Elvis, Presley, King of Rock
3, Pollen, Ndlanya, Trompies
4, Clark, Kent, Superman
5, Kaizer, Motaung, Chincha Guluva
6, Lucas, Radebe, Roo
user_friends table
user_friend_id, user_id, friend_id
1, 1, 3
2, 1, 5
3, 2, 5
4, 2, 1
5, 3, 4
6, 3, 2
7, 4, 2
8, 4, 1
9, 4, 5
10, 5, 1
11, 5, 3
12, 6, 1
13, 6, 3
Below is my code
Code: Select all
<?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 "<form Method='POST' ACTION='test.php'>";
$select_box = '<select name="user_id">'; // 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 .= '<option value="'. $uid .'">'. $uname .'</option>'; // list the users in the drop down list and assign the user id as the options value
}
$select_box .= "</select>";
print $select_box; // print the list
echo "<br /><br />";
echo "<input type='submit' name='submit' value='Submit' />";
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 "<p>User known as: " .$user->getDisplayName(). "<p>"; // call method getDisplayName()
// the above works well
$friends = $user->getFriends(); // This is where I need help
echo "<p>Friends with: "; // HERE IM NOT REALLY SURE WHAT TO DO
echo "<p>Friends with: "; // HERE AS WELL
// ALSO for user_id 3 has 3 friends and for this user I need to display the 3rd friend also here????
echo "<p><a href='test.php'>Return</a></p>";
}
echo "</form>";
?>Code: Select all
<?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
}
}
?>User known as: Peter Pan // on all users, this dipsplays fine the below is where I need help
Friends with Pollen Ndlanya
Friends with Kaizer Motaung
Return
User known as: King of Rock
Friends with Kaizer Motaung
Friends with Peter Pan
Return
User known as:Trompies
Friends with Clark Kent
Friends with Elvis Presley
Return
User known as: Superman
Friends with Elvis Presley
Friends with Peter Pan
Friends with Kaizer Motaung // This is the one with 3 friends
User known as: Chincha Guluva
Friends with Peter Pan
Friends with Elvis Presley
Return
Any assistance will be much appreciated
Thanks