Jump to content

recursive tee solution


narjis

Recommended Posts

I'm really fond of your coding and need some help too I'm also trying to make a tree and save it in an xml file such that parent_id of one node is inserted in as pages key to the father node.By running this query

$sql = "Select * from category order by parent_id ASC";

I'm getting the following result.

Array

(

    [0] => Array

        (

            [category_id] => 2

            [parent_id] => 0

            [category_name] => Clothes

        )

 

    [1] => Array

        (

            [category_id] => 5

            [parent_id] => 1

            [category_name] => T-shirts

        )

 

    [2] => Array

        (

            [category_id] => 6

            [parent_id] => 1

            [category_name] => Cotton-Shirts

        )

 

    [3] => Array

        (

            [category_id] => 1

            [parent_id] => 2

            [category_name] => Upper

        )

 

    [4] => Array

        (

            [category_id] => 4

            [parent_id] => 2

            [category_name] => Lower

        )

 

    [5] => Array

        (

            [category_id] => 3

            [parent_id] => 4

            [category_name] => Pants

        )

 

)

 

My code is as follows but not executing correctly

function searchTree($arr,$node,$i){

if(!in_array($node['parent_id'],$arr[$i])) {

$stmt =  '<pages_'.$i.'>';

$stmt .=$node;

$stmt .='</pages_'.$i.'>';

return $stmt;}

else{

//print_r($returnTree);

print_r ($arr[$i]);//['category_id'];

$returnTree = ($arr[$i]['category_id']==$node['parent_id'])?

searchTree($arr,$node,$i++): searchTree($arr,'',$i++);

}

}

$i=0;

foreach($links as $link){

$myArr = searchTree($links,$link,$i);}

 

The result should be as follows:

<page_1>

        <label>Clothes</label>

        <uri>#</uri>

<pages>

 

        <page_1_1>

                  <label>Upper</label>

                  <uri>#</uri>

                  <pages>

                              <label>T-Shirt</label>

                              <uri>#</uri>

                              <page_1_1>

                                    <page_1_1_1>

<label>Short Sleaves</label>

<uri>#</uri>

</page_1_1_1>

<page_1_1_2><label>Long Sleaves</label><uri>#</uri>

</page_1_1_2></page_1_1></page_1>

<page_1_2>

                  <label>Lower</label>

                  <uri>#</uri>

                  <pages>....</page_1_2>

 

Hopefully you can unsderstand this problem. Please help me.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.