pouncer Posted January 22, 2007 Share Posted January 22, 2007 [code=php:0]class Profile_Details { var $username; var $sql; var $row; // Constructor. function Profile_Details($uname) { $this->username = $uname; $userid = $_SESSION['UserID']; $sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'"); $row = mysql_fetch_assoc($sql); echo $row['Forename']; //this line echos correctly } function Get_Name() { return "gfgs" . $row['Forename']; } } [/code]but the on the other page when i do<?php echo $profile->Get_Name(); ?>it just echos gfgsand not the name after it, anyone see where im going wrong Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 $row only exists in the constructor, and is never saved as $this->row. What you should do is have a var $forename;then in the constructor add $this->forename = $row['Forename'];and in Get_Name() return $this->forename. Quote Link to comment Share on other sites More sharing options...
pouncer Posted January 22, 2007 Author Share Posted January 22, 2007 what if i do$this->row = mysql_fetch_assoc($sql);and then would the Get_name work? Quote Link to comment Share on other sites More sharing options...
pouncer Posted January 22, 2007 Author Share Posted January 22, 2007 thanks jesirose. this worked class Profile_Details { var $username; var $sql; var $row; // Constructor. function Profile_Details($uname) { $this->username = $uname; $userid = $_SESSION['UserID']; $this->sql = mysql_query("SELECT * FROM user_profile WHERE user_id='$userid'"); $this->row = mysql_fetch_assoc($this->sql); } function Get_Name() { return "gfgs" . $this->row['Forename']; } } Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 Yes, but it's not really how the class should be designed. It would work. Quote Link to comment Share on other sites More sharing options...
pouncer Posted January 22, 2007 Author Share Posted January 22, 2007 hm, but ive got lots of functions like this.. function Get_Name() { return $this->row['Forename']; } function Get_Surname() { return $this->row['Surname']; } function Get_Age() { return $this->row['Age']; } function Get_Gender() { return $this->row['Gender']; } function Get_Location() { return $this->row['Location']; }should i put those all in the constructor? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 22, 2007 Share Posted January 22, 2007 That's what I would do. Think of the class as a blueprint. Every user has an age, gender, name - those should all be vars for it.var $age; var $gender;Plus, that's less for you to write. $this->age is cleaner than $this->row['age'], right?Redbull: you're right, they are valid but it doesn't follow what I've been taught about OOP design - so I'm just trying to explain how I've always done classes, the way my professors and boss showed me. I could be wrong. *shrug* Quote Link to comment 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.