3

I have this table in MySQL:

id       name            mother
1        grandma         0
2        myuncle         1
3        mymom           1
4        me              3
5        mysister        3
6        myson           4
7        new_grandma_son 1

I almacenate this info in an array called data[]

$data=array(
        array("id"=>1,"name"=>"grandma",        "mother"=>0),
        array("id"=>2,"name"=>"myuncle",        "mother"=>1),
        array("id"=>3,"name"=>"mymom",          "mother"=>1),
        array("id"=>4,"name"=>"me",             "mother"=>3),
        array("id"=>5,"name"=>"mysister",       "mother"=>3),
        array("id"=>6,"name"=>"myson",          "mother"=>4),
        array("id"=>7,"name"=>"new_grandma_son","mother"=>1)
    );

And for make a family tree I am using this recursive function:

function tree($data, $mom = 0, $level = 0){
     foreach ($data as $row){
          if ($row['mother'] == $mom) {
               echo str_repeat("-", $level).$row['name']."<br>";
               tree($data, $row['id'], $level);
          }
          else $level++;
     }
}

When I call the function tree($data); it shows this:

grandma
-myuncle (level 1)
-mymom
----me (level 4??)
---------myson (level 9??)
----mysister
----new_grandma_son (level 4??)

I have the mistake in the else $level++;, because is adding levels when $row['mother'] != $mom, going through all the rows, but I don't know how to make it. Anyone knows? Thank you.

Solution (By Frits van Campen):

               tree($data, $row['id'], $level+1);

          // (eliminate this else $level++; )

Thanks!

3 Answers 3

4
function tree($data,$mom=0,$level=0){
     foreach($data as $row){
          if($row['mother']==$mom){
               echo str_repeat("-",$level).$row['name']."<br>";
               tree($data,$row['id'],$level+1);
          }
     }
}

I think this fixes your code. Can you supply the $data so I can test it?

Sign up to request clarification or add additional context in comments.

4 Comments

I edited my question ;) and I works!! but so strange... because at the begining I tried to put $level++; just before that function: echo str_repeat("-",$level).$row['name']."<br>"; $level++; tree($data,$row['id'],$level); And it didn't work. I didn't try your solution because i thought it was the same...! Thank you very much ;)
You don't want $level++, you only need $level+1 whenever you go one level deeper.
You can use also ++$lavel. tree($data,$row['id'],++$level); But not tree($data,$row['id'],$level++);
I did with my logic for file n folder structure but your $level+1 solution saved my day.
1

You may try like this, which returns value in a Array format. I used this function for Item category Tree,

    function categoryDropDown($categoryArray, $parentId, $level, $options)
{   
    $level++;
    foreach ($categoryArray  as  $array)
    {
        if($array['parentId'] == $parentId)
        {
            $opt = str_repeat(">> ", $level-1) . $array['name'] ;         
            $options[$array['id']] =  $opt;
            $newParent = $array['id'];              
            $options = categoryDropDown($categoryArray, $newParent, $level , $options);         
        }
    }   
    return $options;    
}

----output----

//Array Key contains category Id.


Array
(
    [1] => Electronics
    [2] => >> Mobile
    [5] => >> >> Mobile Accessories
    [7] => >> >> >> Sub Category 1
    [9] => >> >> >> Sub Category 3
    [3] => >> Laptop
    [6] => >> >> Laptop Accessories
    [8] => >> >> >> Sub Category 2
    [10] => >> >> >> Sub Category 4
    [4] => >> Tablet
    [11] => Main Category 2
)

You can refer some advance version of this code at here How to display select multiple drop down from using array in php?

Comments

0

Cant you just replace the str_repeat("-", $level) with str_repeat("-", $row['mother'])?

Or maybe in your case str_repeat("-", $row['mother'] - 1) as you don't want to start with a - in front of grandma.

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.