0

I has this function to get root of user and its work .

   public function getRootUser()
{
    $query = "SELECT id, nameOfPerson, parent FROM person WHERE parent is null";
    $statment = $this->db->prepare($query);
    $statment->execute();
    echo '<ul id ="family">';
    while($family = $statment->fetch(PDO::FETCH_OBJ)){

        echo '<li>'. $family->nameOfPerson;
      }
}

And i have this function to get the child of the root , but its not work why?

public function getChildOfParentUser($parentId)
{

        $parentId = $family->id;
        $query1 = "SELECT id, nameOfPerson, parent FROM person WHERE id = $parentId";
        $statment1 = $this->db->prepare($query1);
        $statment1->bindValue('id', $family->id);
        $statment1->execute();
        if($statment1->rowCount() > 0){
        echo '<ul>';
        while($family2 = $statment1->fetch(PDO::FETCH_OBJ)){
            echo '<li>' . $family2->nameOfPerson . '</li>';
        }
            echo '</ul>';
        }
            echo '</li>';

            echo '</ul>';
}

he give this notice

Notice: Undefined variable: family in /var/www/html/Family.php on line 59

Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 59

Notice: Undefined variable: family in /var/www/html/Family.php on line 62

Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 62

this is some data from my table

id  nameOfPerson    parent
3   John             NULL
4   Michel            3
5   Husam             4
6   Khalaf            5
7   Mark              5
----------------------------
Second View
  • 154
  • 9
  • This two function have some relation then only you will get access to a private member or else use **GLOBAL** keyword to access variable globally like in `getChildOfParentUser($parentId){ GLOBAL $family;}` – Ganesh May 29 '18 at 12:14
  • @Ganesh i try it he give me this notice Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 59 Notice: Trying to get property 'id' of non-object in /var/www/html/Family.php on line 62 – Second View May 29 '18 at 12:17
  • Kindly post Full code of `Family.php` file or use **pastebin** – Er. Amit Joshi May 29 '18 at 12:31
  • @Er.AmitJoshi https://pastebin.com/nf5y8Aa2 – Second View May 30 '18 at 07:01
  • @husamalmasri kindly can you provide the sample data of person table – Er. Amit Joshi May 30 '18 at 07:26
  • I update my post check it please – Second View May 30 '18 at 07:31
  • how ever i am checking but don't you think your `Family` class missing the `public $db;` property which you are using in the `Family` constructor. – Er. Amit Joshi May 30 '18 at 07:57
  • @husamalmasri is that your desired result https://ibb.co/bKTCTJ – Er. Amit Joshi May 30 '18 at 08:55
  • @deceze kindly remove it from the duplicate question as i think it look like but it is not . – Er. Amit Joshi May 30 '18 at 09:15
  • @Er. How's it _not_ a duplicate? Clarify the question to clearly distinguish it from the existing duplicates. – deceze May 30 '18 at 09:18
  • As i think user want to short out the function `public function getChildOfParentUser($parentId)` is not working. so in my opinion user having some logical errors here. And i think it wont be good to change question by my own i think user have to do it his own. @deceze @husamalmasri – Er. Amit Joshi May 30 '18 at 09:40
  • @Er. The immediate problem is variable scope. The _bigger_ problem is that there's no apparent relationship between those two functions, and we can't even really suggest the best solution without knowing how they are related. So at best I'd close this as "unclear" otherwise. – deceze May 30 '18 at 09:43
  • 1
    But we are here to help not to close the question. I think .Rest is on you. – Er. Amit Joshi May 30 '18 at 09:46

0 Answers0