CastorWillis Posted February 26, 2015 Share Posted February 26, 2015 Hi there, I have search everywhere for a similar problem or example and cannot find one that helps my code to work. It is a really basic family tree webpage. I am building it as my learning platform. Following is the PHP code I am using: <?php $link = mysql_connect('localhost', 'MY_DB', 'MY_PASSWORD'); if (!$link) { die('Could not connect: ' . mysql_error()); } class treeClass { public $rowstart = "1"; public $mother = ""; public function fillpoolLrg() { $loopResult = ''; $events = mysql_query("SELECT name FROM pikiconz_data.tangata WHERE id = '$rowstart'"); while($row = mysql_fetch_array($events)) { $loopResult .= ' <div class="poolLrg pool">'.$row['name'].'</div>'; $mother = $row['mother']; } echo $loopResult; return $mother; } public function fillMedPb2() { $loopResult = ''; $events = mysql_query("SELECT name FROM pikiconz_data.tangata WHERE id = '$mother'"); if($row = mysql_fetch_array($events)) { $loopResult .= ' <div class="poolMed pool">'.$row['name'].'</div>'; } else { $loopResult .= ' <div class="poolMed pool">'.'ADD'.'</br>'.'PERSON'.'</div>'; } echo $loopResult; } } ?> So, for the life of me I just can not get this code to display the div that is in the function. It works if I remove the class.. just not WITH the class.. I have looked everywhere and can not help but think it is either something really simple OR I am using classes in a completely erroneous way. Any help, thoughts or general advice on where to head for answers would be greatly appreciated! Thank you in advance. Castor Quote Link to comment https://forums.phpfreaks.com/topic/294906-help-with-classes-can-not-get-it-to-work/ Share on other sites More sharing options...
NotionCommotion Posted February 26, 2015 Share Posted February 26, 2015 Classes just define the methods and properties to make an object. They do nothing by themselves. First you need to make an object from the class using something like $myTreeClass=new treeClass(), and then evoke a method using something like $myTreeClass->fillpoolLrg() (unless you are using a constructor, but don't worry about that for now). Quote Link to comment https://forums.phpfreaks.com/topic/294906-help-with-classes-can-not-get-it-to-work/#findComment-1506781 Share on other sites More sharing options...
Sanjib Sinha Posted February 27, 2015 Share Posted February 27, 2015 AS you said: It works if I remove the class.. just not WITH the class.. I think you would better write the class again. Keeping in mind, every class should have single responsibility. It is also not a good practice to hit the database directly from your class where you are displaying data so you can have it encapsulated. And finally as NotionCommotion said create an instance. Best Wishes. Quote Link to comment https://forums.phpfreaks.com/topic/294906-help-with-classes-can-not-get-it-to-work/#findComment-1506957 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.