Displaying information from my Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Displaying information from my Database

Post by cybastud »

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

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>";
?>
user.php

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
}


}


?>
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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

In getFriends() you have to use the id values you have to retrieve the names of the friends from the user table
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Thanks. I use the getFriends() method on another page to retrieve and the display just the id values of the friends of that user. I cannot change this otherwise it affects my other page which is working correctly. How do I create another method to retrieve the display_names of the selected users friends?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

You could create a new method from scratch but since getFriends() already has some of the code you need copy the code and add the required code, then rename it to getSelectedFriends() - or whichever name will not cause conflicts within the class
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Something like this...?

Code: Select all

public function getSelectedFriends(){ 
                $sql = 'select display_name from users where user_id = '.$this->user_id;
                $results = mysql_query($sql);
                $friends = array(); // array
                while($row = mysql_fetch_array($results)){ 
                                $friends[] = $row['display_name'];
                }
                return $friends; 
How would I display this information like the above screen shots

Code: Select all

echo "<p>Friends with: ". $friends; // HERE IM NOT REALLY SURE WHAT TO DO
        
        echo "<p>Friends with: " .$friends;  // HERE AS WELL
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

Since an array is returned you can use a foreach loop;

Code: Select all

<?php
 $friends = $this->getSelectedFriends();
 foreach ($friends as $friendNames) {
    echo '<p>Friends with ' . $friendNames . '</p>';
 }
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Great I will test it when I get home, but am I on the right track with my getSelectedFriends() method?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

If i understood the original question correctly then the script should do what you have in mind :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Thanks, I will test later
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Im getting the following error now

( ! ) Fatal error: Using $this when not in object context in C:\wamp\www\oop5.php on line 170

Any ideas?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Displaying information from my Database

Post by Celauran »

You should be using $user, not $this
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Yes I tried that but then if I select Peter Pan for example then I get "Friends with Peter Pan" and so on. It just prints out the user's name again not their friends as in the DB.... :?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

How do you link the person and their friends together; via id?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
cybastud
Forum Newbie
Posts: 13
Joined: Tue Apr 17, 2012 1:01 pm

Re: Displaying information from my Database

Post by cybastud »

Thats right.

If you look at the user_frieds table user 1 is friends with user 3 and 5, user 2 is friends with users 5 and 1 and so on....
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Displaying information from my Database

Post by social_experiment »

I should have explained a bit better; assume we want person A's friends, you find person A's id (say 2). To select all the persons' friends you have to look for all friends that has an id value of 2 in their friends column.

Can you explain the user_friends table, what the purpose is of each column
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply