1

Need help with the below. I want to call all rows concerned yet I only get one. I have tried all possible methods to output this and all I get is one result from different sessions. The getID function calls active session user ID but results are the same for all sessions.

    public function getChildId()
{
    $child_id = $this->db->query("SELECT  firstname, lastname
        FROM " . $this->db->table("customers") . "
        RIGHT JOIN " . $this->db->table("genealogy") . " on parent_id
                WHERE parent_id = '". $this->customer->getID(). "'"
    );
   foreach ($child_id as $child => $child_id->num_rows ) {
           $child = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );
   }
    return $child;
}

}

On var_dump ($child) I get: array (size=2) 0 => string 'John' (length=8) 1 => string 'Doe' (length=9) I have 3 customer entries/rows on the database

5
  • 1
    on parent_id = ? Commented Jan 1, 2017 at 18:32
  • You're recreating the array on each iteration. You need to define the array outside the loop and add to it within. Commented Jan 1, 2017 at 18:34
  • @Sougata Bose. That is where I am joining the tables Commented Jan 1, 2017 at 18:35
  • $child[] = array(......) Commented Jan 1, 2017 at 18:37
  • Yeah, ON parent_id isn't much of a condition Commented Jan 1, 2017 at 18:51

1 Answer 1

1

In each iteration you are overwriting or recreating array $child, hence at the end you just have only one element (last row of iteration) in array $child.

Change

$child = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );

To

( Push one or more elements onto the end of array in each iteration )

$child[] = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );

You can also use array_push like below

array_push(
           $child,
           array(
                  $this->name = $child_id->row['firstname'],
                   $this->surname = $child_id->row['lastname']
               )
           );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.