sean04 Posted October 17, 2010 Share Posted October 17, 2010 Can someone give me a hand on getting this working 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 Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/ Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 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(..); } } Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123334 Share on other sites More sharing options...
sean04 Posted October 18, 2010 Author Share Posted October 18, 2010 Yes it does contain a Database class Thanks for the reply! So I wouldn't do anything like I posted right? Like loading all the users information? or should I just do that on the profile.php Thanks again, Sean Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123453 Share on other sites More sharing options...
ignace Posted October 18, 2010 Share Posted October 18, 2010 On the profile page you would call: $user = $gateway->findUserById(1); Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123482 Share on other sites More sharing options...
sean04 Posted October 18, 2010 Author Share Posted October 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123490 Share on other sites More sharing options...
Bodhi Gump Posted October 18, 2010 Share Posted October 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123499 Share on other sites More sharing options...
sean04 Posted October 18, 2010 Author Share Posted October 18, 2010 Actually what I've done doesn't really make any sense i guess lol. Thanks, Sean Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123528 Share on other sites More sharing options...
Bodhi Gump Posted October 19, 2010 Share Posted October 19, 2010 Actually what I've done doesn't really make any sense i guess lol. Well, back to the drawing board with new lessons learned, and some questions on your mind to guide you, then. We all have to do that. Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1123964 Share on other sites More sharing options...
sean04 Posted October 20, 2010 Author Share Posted October 20, 2010 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 and i know I'm not returning the correct value lol. Thanks for any help, Sean Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1124167 Share on other sites More sharing options...
sean04 Posted October 20, 2010 Author Share Posted October 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1124219 Share on other sites More sharing options...
Bodhi Gump Posted October 21, 2010 Share Posted October 21, 2010 I'm not entirely sure what you want your code to do. Could you describe, in one or two sentences, what you want to print? Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1124910 Share on other sites More sharing options...
ignace Posted October 21, 2010 Share Posted October 21, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/216108-user-class-help/#findComment-1124933 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.