Jump to content

User class help...


sean04

Recommended Posts

Can someone give me a hand on getting this working :P maybe how to use it and what not.. I'm trying but not very successful lol. I want to call the loaduser on the profile page..

 


<?php
include "Database.php";
class User{

//Constructor - Create a new user
function User(){
}

//Constructor - Load an existing user
function LoaddUser($userID){
	$this->LoadUser($UserID);
}

function LoadUser($userID){
	//$db = new Database();
	$results = mysql_fetch_array(mysql_query("SELECT * FROM userInfo WHERE User_ID = '$userID'"));

	$UserID = $results[userID];
	$Email_Address = $results[Email_Address];
	$Password = $results[Password];
	$Name = $results[Name];
	$Screen_Name = $results[screen_Name];

	return $results;
}
}

?>


 

Thanks,

Sean

Link to comment
Share on other sites

I hope Database.php contains a Database class?

 

If so, you write:

 

class UserGateway {
     private $db = null;
     function __construct(Database $db) {
         $this->db = $db;
     }
     
     function getAllUsers() {
         return new UserList($this->db->fetchAll('SELECT * FROM users'));
     }
     
     function findUserById($id) {
         return new User($this->db->fetchRow('SELECT * FROM users WHERE id = ..'));
     }
     
     function findUsersByLastName($lastName) {
         return new UserList(..);
     }
}

Link to comment
Share on other sites

Thanks! I will give this a try!

 

Do you recommend anything like this as a function?

 

function LoadUser($userID){

//$db = new Database();

$results = mysql_fetch_array(mysql_query("SELECT * FROM userInfo WHERE User_ID = '$userID'"));

$UserID = $results[userID];

$Email_Address = $results[Email_Address];

$Password = $results[Password];

$Name = $results[Name];

$Screen_Name = $results[screen_Name];

return $results;

}

 

or should i just implement that right in the profile page?

 

Thanks,

Sean

Link to comment
Share on other sites

1) I see that stuff from $results go into variables like $Name in the function LoadUser. What happens to these variables after the function returns $results?

 

2) What happens to the array that LoadUser returns?

 

3) Go and google about PHP Arrays and how String indexes work in Arrays.

Link to comment
Share on other sites

Thanks! :)

 

Another question... how would I get this to output on the profile page? Here is the function:

function GetFriends($User_ID){
		if($User_ID != null){
			$getUserFriends = mysql_query("SELECT * FROM `userfriends` WHERE `User_ID` = '$User_ID'") or die(mysql_error());
			$totalUserFriends = mysql_num_rows($getUserFriends);
			while ($UserFriends = mysql_fetch_array($getUserFriends)) {
				$getRequesterInfo = mysql_fetch_array(mysql_query("SELECT * FROM `viewInformation` WHERE `User_ID` = '$UserFriends[Friends_User_ID]'")) or die(mysql_error()); //gets requests
				if($getRequesterInfo[Picture_Link] != "") {
					echo"<a href='user_profile.php?User=$RequesterInfo[user_ID]'><img src='$RequesterInfo[Picture_Link]' border='2' height='75' width='75'></a><p/>"; }else{echo"<a href='user_profile.php?User=$RequesterInfo[user_ID]'><img src='".$defaultProfilePicture."' border='2' height='75' width='75'></a><p/>";
				}
				echo "<a href='user_profile.php?User=$RequesterInfo[user_ID]'>$RequesterInfo[screen_Name]</a> - ";
				echo "<a href='?verifydelete&quickViewUser=$RequesterInfo[user_ID]'>Remove</a><br><p/></div>";
	}
	return(GetFriends);

}

 

That function is in a class called User... Here is what I'm using on the profile page to call it:

$user = $User->GetFriends($User_ID);

 

And that doesn't work lol... I know it probably need a lot of cleaning up :P and i know I'm not returning the correct value lol.

 

Thanks for any help,

Sean

 

Link to comment
Share on other sites

Ok I got it working finally. I just need to know if this is the proper way to do it? If there is something that would work better please let me know.

 

function GetFriends($User_ID){

	if($User_ID != null){

	$getUserFriends = mysql_query("SELECT * FROM user_friends WHERE $_SESSION[userID] = '$User_ID'") or die(mysql_error());
	$totalUserFriends = mysql_num_rows($getUserFriends);

	while ($UserFriends = mysql_fetch_array($getUserFriends)) {
		echo"<div class=ViewList>";
		$getFriendInformation = mysql_fetch_array(mysql_query("SELECT * FROM `viewInformation` WHERE `User_ID` = '$UserFriends[Friends_User_ID]'"));
		if($getFriendInformation[Picture_Link] != "") {
			echo"<a href='user_profile.php?User=$getFriendInformation[user_ID]'><img src='$getFriendInformation[Picture_Link]' border='2' height='75' width='75'></a><p/>"; }else{echo"<a href='user_profile.php?User=$getFriendInformation[user_ID]'><img src='".$defaultProfilePicture."' border='2' height='75' width='75'></a><p/>";
		}
		echo "<a href='user_profile.php?User=$getFriendInformation[user_ID]'>$getFriendInformation[screen_Name]</a> - ";
		echo "<a href='?verifydelete&quickViewUser=$getFriendInformation[user_ID]'>Remove</a><br><p/></div>";
	}
	return $UserFriends;
}

 

This is what I use to call the function:

 

User::GetFriends(1);

 

 

Suggestions?

 

Thanks,

Sean

 

 

Link to comment
Share on other sites

If you want to call it like:

 

User::GetFriends(1);

 

Then you should add static to your function declaration:

 

static function GetFriends(..

 

But IMO it doesn't make any sense because User is a representation of the actual user on your system and it would make more sense to write something like:

 

$User->GetFriends();

 

Which would return a FriendList (or UserList)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.