Apologies for only replying now. When you replied it was after 23:00 and I had to get up for work at 05:00.
Here is all the code.
First the form. 2 x seperate forms on seperate pages:
[php]
<?php
/*connecting oop4.php to user_find.php using the include keyword*/
include ("user_find.php");
?>
<form name='users' method='POST' action='oop4.php'>
Peter Pan
Elvis Presley
Pollen Ndlanya
Clark Kent
Kaizer Motaung
Lucas Radebe
[/php]
Second Form:
[php]
<?php
include ("user_find.php");
?>
<form name='users' method='POST' action='oop5.php'>
Peter Pan
Elvis Presley
Pollen Ndlanya
Clark Kent
Kaizer Motaung
Lucas Radebe
[/php]
Next the user_find page.
[php]
<?php
/*connecting user_find.php with user.php using connect keyword*/
include ("user.php");
/*connecting to database and tables using mysql commands.*/
mysql_connect("127.0.0.1", "root", "")or die ("Could not connect to server");
mysql_select_db("490942")or die ("Could not connect to database");
/*Check if submit button in oop4.php was pressed and if so, assigning value to
$ID. Calling getUserById function and sending $ID to that function as argument.*/
if (isset($_REQUEST["submit1"]))
/*Assigning value to $ID + 1 because option values start at 0, but user id's
in user table auto increment from 1.*/
{
$ID=$_REQUEST["user1"] + 1;
$user = new User();
$user->getUserById($ID);
/*calling getFirstName and getLastName function to return name of person whose user ID was sent
to getUserById function*/
echo "
User's name is: " .$user->getFirstName(). " " .$user->getLastName()."
";
/*Calling getFreinds function and assigning return value to $friens. The .implode
keyword is used to join array elements with a string, as $friens is an array.*/
$friends = $user->getFriends();
echo "
Friends with: " .implode(', ',$friends). "
";
echo "
User known as: " .$user->getDisplayName(). "
";
}
/*Check if submit button in oop5.php was pressed and if so, assigning value to
$ID. Calling getUserById function and sending $ID to that function as argument.*/
if (isset($_REQUEST["submit2"]))
{
$ID=$_REQUEST["user2"] + 1;
$user = new User();
$user->getUserById($ID);
echo "
User known as: " .$user->getDisplayName()."
";
/*calling getFriends() function and assigning that fallue to $friends variable*/
$friends = $user->getFriends();
foreach ($friends as $friendsNames){
echo "
Friends with: ". $friendsNames ."
";
}
}
?>
[/php]
Lastly the user page
[php]
<?php
/*Creating the class for the functions to be used by the code in user_find.php*/
class User{
/*Function used to capture the user_id and to extract the details from the database
table "users" and using the $this keyword to point to the current objects*/
public function getUserById($user_id){
$sql="SELECT 'first_name', 'last_name', 'display_name' FROM 'users' WHERE 'user_id' ='". $user_id"'";
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
$this->user_id = $user_id;
$this->first_name = $row['first_name'];
$this->last_name = $row['last_name'];
$this->display_name = $row['display_name'];
return true;
}
/*function called to return display_name to user_find.php using the $this keyword*/
public function getDisplayName (){
return $this->display_name;
}
/*function called to return first_name to user_find.php using the $this keyword*/
public function getFirstName (){
return $this->first_name;
}
/*function called to return last_name to user_find.php using the $this keyword*/
public function getLastName (){
return $this->last_name;
}
/*function called to extract the friends id's using a sql query and returning value
in the friends array variable*/
function getFriends(){
$sql="select friend_id from user_friends where user_id =" .$this->user_id;
$results = mysql_query($sql);
$friends = array();
while ($row = mysql_fetch_array($results)){
$friends[] = $row['friend_id'];
}
return $friends;
}
/*function called to extract friends first and last names using sql query and
returning value to user_find.php*/
function getFriendsById($friends) {
$sql="SELECT `first_name`, `last_name` FROM `users` WHERE `user_id` ='".$friends."'";
$results = mysql_query($sql)or die("Selection Query Failed !!!
".mysql_error());
$friendsNames = array();
while ($row = mysql_fetch_array($results)){
$friendsNames[] = $row['first_name'].', '.$row['last_name'];
} return $friendsNames[0];
}
}
?>
[/php]