Jump to content
Sign in to follow this  
CastorWillis

Help with classes - Can not get it to work

Recommended Posts

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

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.