Jhilom
Jhilom

Reputation: 1028

Creating Structured array by Recursive function

I have a table

enter image description here

Which I want show recursively like below picture

enter image description here

I am using a recursive function in php

function  reccall($cat_id)
 {
   global $no,$recArray;
   $sql = "SELECT a.*
      FROM cat_master   
           WHERE 
        parent_id = $cat_id 
      ORDER BY 
        id ASC
      ";
         $result = mysql_query($sql) or die("Could not fetech Recursively");
    while($row = mysql_fetch_object($result))
    {
      $recArray[$no]['value'] = mysql_real_escape_string($row->value);
      $recArray[$no]['id'] = $row->id;
       ++$no;
      reccall($row->id);
    }
    return $recArray;    
 } 

but I am not able to generate a structured array like how the order is not the picture. A simple array is created all the time. Can anyone help me with creating the structured array like the order shown above.

Upvotes: 0

Views: 376

Answers (2)

Jhilom
Jhilom

Reputation: 1028

use this function instead of your function and your problem will be solved I hope

function reccall($cat_id)
 {
     $sql = "SELECT a.*
      FROM cat_master   
           WHERE 
        parent_id = $cat_id 
      ORDER BY 
        id ASC
      ";
         $result = mysql_query($sql) or die("Could not fetech Recursively");
    while($row = mysql_fetch_object($result))
    {
      $recArray[$no]['main']['value'] = mysql_real_escape_string($row->value);
      $recArray[$no]['main']['id'] = $row->id;         
      $recArray[$no]['child'] = reccall($row->id);
       ++$no;
    }
    return $recArray;    
 } 

Upvotes: 1

ShaaD
ShaaD

Reputation: 626

<?
// I identified this function separately because it is performed only once, for preparing data
// It's collect an array of all parents in the correct order for each id
function dest($array) {
    foreach($array as $key=>$value) {
        if($value['pid']==0) continue;

        $pid = $key;
        $array[$key]['dest'] = array();
        while ( $pid = $array[$pid]['pid'] ) {
            if($key == $pid) exit("this tree is broken");
            $array[$key]['dest'][] = $pid;
        }
    }

    return $array;
}
// Recursive function that puts the items in the correct tree. removes the parameter dest.
function tree($array) {
    foreach($array as $key=>$value) {
        if( is_array($value['dest']) && !empty($value['dest']) ) {
            $pid = array_pop($value['dest']);
            if( empty($value['dest']) ) unset($value['dest']);

            $array[$pid]['childrens'][$key] = $value;
            $array[$pid]['childrens'] = tree($array[$pid]['childrens']);
            unset($array[$key]);
        }
    }

    return $array;
}

$array = array(
    1 => array(
        'title'=>'q',
        'pid'=>0,
        ),
    2 => array(
        'title'=>'w',
        'pid'=>1,
        ),
    3 => array(
        'title'=>'e',
        'pid'=>0,
        ),
    4 => array(
        'title'=>'r',
        'pid'=>2,
        ),
    5 => array(
        'title'=>'t',
        'pid'=>1,
        ),
);

$tree = tree( dest($array) );

echo '<pre>';
print_r($array);
print_r($tree);
?>

By the way, I should note that these arrays are not very useful. Better to use the result of the function dest().

Upvotes: 1

Related Questions