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 Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/ 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. Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166418 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? Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166428 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']; } } Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166433 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. Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166435 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? Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166440 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* Link to comment https://forums.phpfreaks.com/topic/35236-php-class-problem/#findComment-166443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.