ShoeLace1291 Posted August 2, 2009 Share Posted August 2, 2009 I have a class that gets the info for a specific user(not necessarily the user that is logged in) and puts it into an array. I include the class file at the top of a php page using include('classes/member.php'). How would I use the class to output a certain bit of information like this: echo $member->getinfo->displayName($memberID); This would be my regular PHP page: <?php include('classes/member.php'); $member = new member; $memberID = 1; echo $member->getinfo->displayName($memberID); ?> This is my php class: <?php class member { function getinfo($memberID){ $query = mysql_query("SELECT * FROM members WHERE memberID = '".$memberID."' LIMIT 1") or die("Member3: ".mysql_error()); $memberinfo=mysql_fetch_array($query); $member = array( 'memberID' => $memberinfo["memberID"], 'displayName' => $memberinfo["displayName"], 'email' => $memberinfo["email"] ); } } ?> Link to comment https://forums.phpfreaks.com/topic/168452-set-variables-with-php-class/ Share on other sites More sharing options...
gevans Posted August 2, 2009 Share Posted August 2, 2009 Take a look at the following code and see what you think. There are another two methods that need to be made to have it nice and open for you. If you don't understand anything just ask. <?php class member { var $member = array(); function setinfo($memberID){ $query = mysql_query("SELECT * FROM members WHERE memberID = '".$memberID."' LIMIT 1") or die("Member3: ".mysql_error()); $memberinfo=mysql_fetch_array($query); $this->member = array( 'memberID' => $memberinfo["memberID"], 'displayName' => $memberinfo["displayName"], 'email' => $memberinfo["email"] ); } function getinfo(){ if(empty($this->member)) return FALSE else return $this->member; } function getid(){ if(empty($this->member)) return FALSE else return $this->member['memberID']; } } ?> Link to comment https://forums.phpfreaks.com/topic/168452-set-variables-with-php-class/#findComment-888616 Share on other sites More sharing options...
trq Posted August 2, 2009 Share Posted August 2, 2009 Your setinfo() method needs cleaning up too. function setinfo($memberID){ if ($query = mysql_query("SELECT * FROM members WHERE memberID = '".$memberID."' LIMIT 1")) { if (mysql_num_rows($query)) { $this->member = mysql_fetch_array($query); return true; } } return false; } Link to comment https://forums.phpfreaks.com/topic/168452-set-variables-with-php-class/#findComment-888619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.