[Solved]How to loop in this result

I have this result

lev1       lev2        lev3      lev4
5            7          9         null

5           7           10        null

5          8           null       null

I want to make loop so that I can I output like this,

[[{v:'5', f:'5'}, ''],[{v:'7', f:'7'},'5'],[{v:'8', f:'8'},'5'],[{v:'9', f:'9'},'7'],[{v:'10', f:'10'},'7']]

but I could not get what I want.

$cmd = $this->connection->prepare('SELECT t1.user_id AS lev1
 , t2.user_id as lev2
 , t3.user_id as lev3
 , t4.user_id as lev4
  FROM tree_table AS t1
LEFT OUTER
  JOIN tree_table AS t2 
    ON t2.p_id = t1.user_id
LEFT OUTER
  JOIN tree_table AS t3 
    ON t3.p_id = t2.user_id
LEFT OUTER
  JOIN tree_table AS t4 
    ON t4.p_id = t3.user_id
 WHERE t1.user_id = 5');
     $cmd->execute();
    $records = $cmd->fetchAll(PDO::FETCH_ASSOC);

   foreach($records as $row){
     // here I'm confuse how to make the data,I got wrong output here.
     //  $rowdata[] = [['v' => $row['lev1'], 'f' => $row['lev1']], (string)$row['lev1'],$row['lev1']];
   
  }

Thank you in advance.

You’re not the only one that’s confused. I can’t see how your two examples relate to each other.

Your first is 4 x 3 and your second 5 x 3

And I don’t see how you derive the second set of numbers by any combination of the first set.

I could not edit anymore my first post,I will just post another example.

This is the result of select statement below.

And I’m using this Google OrgChart to display a heirachical format,
my problem is that I’m confuse on how to loop in my while loop.in order to form the format.

just like in the google orgchart.

This is my php code.

    $cmd = $this->connection->prepare('SELECT t1.memid AS lev1
            , t2.memid as lev2
            , t3.memid as lev3
            , t4.memid as lev4
    
            FROM mytree AS t1
            LEFT OUTER
            JOIN mytree AS t2
            ON t2.parentid = t1.memid
            LEFT OUTER
            JOIN mytree AS t3
            ON t3.parentid = t2.memid
            LEFT OUTER
            JOIN mytree AS t4
            ON t4.parentid = t3.memid
            WHERE t1.memid = ?');
     $cmd->execute(array('1'));
    
    
     $rec = array();
    
    
     while ( $row =  $cmd->fetch(PDO::FETCH_ASSOC)) {
         for ($i = 0; $i < 4; $i++) {
             $rec[] = [['v' => $row['lev2'], 'f' => $row['lev2']], (string)$row['lev1'], $row['lev2']];
         }
    
    
     }

 return json_encode($rec);

Thank you in advance.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.