0

Greeting,

am working on a recursive tree menu, but am getting stuck on somthing.

Recursive code

 $get_parent = "0";
    $rep_1 = mysqli_query($connexion,' SELECT * FROM category');
    $get_array = array();
    while($don_1 = mysqli_fetch_array($rep_1))
    {
    $get_array[$don_1['id']] = array("id" => $don_1['id'], "id_parent" => $don_1['id_parent'], "title" => $don_1['title']);
    }

    function tree_2($array,$parent,$currLevel=0)
    {
    foreach($array as $key => $value)
    {
        if($value['id_parent'] == $parent)
    {
    echo "".str_repeat("-", $currLevel)."id : ".$value['id']." | id_parent : ".$value['id_parent']." | title : ".$value['title']."<br/>";


    $currLevel++;               
    $children = tree_2($array,$key,$currLevel);             
    $currLevel--;
            }
        }
    }
    echo tree_2($get_array,$get_parent);

My table

== Table structure for table category
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|title|varchar(20)|No|
|id_parent|int(11)|Yes|NULL
|path|varchar(11)|No|
== Dumping data for table category

|1|ELECTRONICS|0|0
|2|TELEVISIONS|1|1
|3|TUBE|2|2
|4|LCD|2|2
|5|PLASMA|2|2
|6|PORTABLE ELECTRONICS|1|1
|7|MP3 PLAYERS|6|2
|8|FLASH|7|2
|9|CD PLAYERS|6|2
|10|2 WAY RADIOS|6|3
|11|PLANS|0|0
|12|SPITFIRE|11|1
|13|MP3 PLAYERS|8|2

This code work's very well,

however am stuck for when i want to get only one branch. Like

FLASH

CD PLAYERS

WAY RADIO.

In fact, i'd like to do like into forums, where we can get the path back to the forum root. Like

FLASH > CD PLAYERS > WAY RADIO.

If any idea !

1 Answer 1

1

after some try, I finally found a solution to get from a php recursive tree only one branch. am searing the code, for those who are looking for this kind of script.

Sample source come from this blog : Managing Hierarchical Data in MySQL

The table source :

===Database model3

== Table structure for table category

|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|title|varchar(20)|No|
|id_parent|int(11)|Yes|NULL
|path|varchar(11)|No|
== Dumping data for table category

|1|ELECTRONICS|0|0
|2|TELEVISIONS|1|1/2
|3|TUBE|2|1/2/3
|4|LCD|2|1/2/4
|5|PLASMA|2|1/2/5
|6|PORTABLE ELECTRONICS|1|1/6
|7|MP3 PLAYERS|6|1/6/7
|8|FLASH|7|1/6/7/8
|9|CD PLAYERS|6|1/6/9
|10|2 WAY RADIOS|6|1/6/10
|11|PLANS|0|0
|12|SPITFIRE|11|11/12
|13|LOW QUALITY|8|1/6/7/8/13

Before the code, some explanation :

With php recursive tree, we are able to get from an array a tree with its branch, like this :

  • ELECTRONICS
    • TELEVISIONS
      • TUBE
      • LCD
      • PLASMA
    • PORTABLE ELECTRONICS
      • MP3 PLAYERS
      • FLASH
        • LOW QUALITY
      • CD PLAYERS
      • 2 WAY RADIOS
  • PLANS
    • SPITFIRE

The very simple code that I use for the sample over :

$get_parent = "0";
$rep_1 = mysqli_query($connexion,' SELECT * FROM category');
$get_array = array();
while($don_1 = mysqli_fetch_array($rep_1))
{
    $get_array[$don_1['id']] = array("id" => $don_1['id'], "id_parent" => $don_1['id_parent'], "title" => $don_1['title']);
}
mysqli_free_result($rep_1);
function tree($array,$parent,$currLevel=0)
{
    foreach($array as $key => $value)
    {
        if($value['id_parent'] == $parent)
        {
            echo "".str_repeat("-", $currLevel)."title : ".$value['title']."<br/>";
            $currLevel++;
            tree($array,$key,$currLevel);
            $currLevel--;
        }
    }
}
echo tree($get_array,$get_parent);

The The Adjacency List Model is great for simple mysql query, however, after several days in google search, I found that it was a real head hash to Retrieving a Single Path as we must know the final level, for static menu its fine, but for other purpose like forum parent or pages parent I found it wasn't the best way to process for Retrieving a Single Path.

I documented about the The Nested Set Model, on paper, it's great. But I found it was a bit a mess when INSERT / UPDATE and DELETE are requested.

I finally did some test with the path enumeration : Hierarchical data in MySQL (and other RDBMS) and found a solution for Retrieving a Single Path, like this :

  • ELECTRONICS
    • PORTABLE ELECTRONICS
      • FLASH
        • LOW QUALITY

The very simple code that am using :

$get_parent = "0";
$rep_1 = mysqli_query($connexion,' SELECT * FROM category WHERE id=7');
$get_array = array();
while($don_1 = mysqli_fetch_array($rep_1))
{
    $explod_array = explode("/",$don_1['path'],9999);
    foreach ($explod_array as $key=>$value)
    {
            $rep_2 = mysqli_query($connexion,' SELECT * FROM category WHERE id="'.$value.'"');
            while($don_2 = mysqli_fetch_array($rep_2))
            {
                $get_array[$don_2['id']] = array("id" => $don_2['id'], "id_parent" => $don_2['id_parent'], "title" => $don_2['title']);
            }
            mysqli_free_result($rep_2);
    }
}
mysqli_free_result($rep_1);
function tree_1($array,$parent,$currLevel=0)
{
    foreach($array as $key => $value)
    {
        if($value['id_parent'] == $parent)
        {
            echo "".str_repeat("-", $currLevel)."id : ".$value['id']." | id_parent : ".$value['id_parent']." | title : ".$value['title']."<br/>";
            $currLevel++;
            tree_1($array,$key,$currLevel);
            $currLevel--;
        }
    }
}
echo tree_1($get_array,$get_parent);

This way, I keep the same php recursive tree menu code. I do not charge my mysql table with a big query.

The badest point, is that I will have to code a bit more for the INSERT / UPDATE AND DELETE query, but I already worked on it, and it's doable with a little code.

I hope it will help.

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.