Jump to content

Recursive Tree w/ Adjacency List and array?


paruby

Recommended Posts

I have a database table with categories and subcategories, with the structure of ::

categoryid, categoryname, parentid

 

I have found numerous examples of printing out a tree structure of the table, and it works, but I would like to get all the data into an array, ultimately to print the array to a listbox to select from.  The array structure would be categoryid => categoryname.  The basic query I have now is as follows ::

 

function createCatTree($thisParentCatID=0,$depth=0)  {
$catIDQ = mysql_query("SELECT * from category WHERE parentcatid = $thisParentCatID;");
while ( $catIDQRow = mysql_fetch_array($catIDQ) )   {
	$thisCatName = $catIDQRow['categoryname'];
	$thisCatID = $catIDQRow['categoryid'];
        for($i = 0; $i < $depth; $i++) $spcstr.= "   ";  // create spaces to mimic a tree structure
	$value = $spcstr.$thisCatName;  //  add spaces 
	echo $value;
	createCatTree($thisCatID,$depth+1);
}
}

 

I was hoping to send the categoryid and categoryname to an array, like

$arr[$thisCatID] = $value;

then change my function call to be

createCatTree($thisCatID,$depth+1,$arr);

to send the array as a variable back to the function to hold the data going forward. Also, I added a section at the top of the function to see if the array needed to be initialized, like

if ($arr == "") {
	$arr = array();
}

but this does not work.  Is there any way to use this basic recursive code and save all the data to an array to be worked on at a later time?

Link to comment
Share on other sites

Hope this helps others - As a workaround, I just added the listbox code directly into my function, like this ::

echo "<option value=\"$thisCatID\">$value</option>\n";

Then, when calling my function, I wrapped the <select> nodes, ie ::

echo "<select name='catTree' size='10'>\n";
createCatTree();
echo "</select>";

 

But, I still would like to be able to hold the data in an array, so I can use it in different ways as needed.

Thanks,

Pete

Link to comment
Share on other sites

So for your first question, to determine whether you have an array, there is isset() and is_array(). In your case I don't really think you need that code since you're planning to pass in the array as a parameter.

 

I believe what you're looking for is pass by reference.  Using your original example:

 

function createCatTree($thisParentCatID=0, $depth=0, &$arr)  {

 

When declared in this manner, you can change $arr inside the function, and you will be changing the array that you pass.

 

Link to comment
Share on other sites

That is the pass-by-reference operator.  If you don't use that, variables are passed by value, which is to say, that a copy of the variable is made.  Once the function concludes the copy is disposed of.  With pass by reference, the actual variable is passed into the array, and any changes made to it inside the function will be retained when function execution is completed.

Link to comment
Share on other sites

  • 1 month later...

Gizmola,

 

Thanks again for your help so far.  Here is what I get when i try to print out the array (print_r($arr);) ::

 

Array
(
    [12] => Beverages
    [15] =>    Soda
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
    [13] => Frozen Foods
    [14] =>    Ice Cream
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
    [13] => Frozen Foods
    [14] =>    Ice Cream
    [17] =>    Popsicles
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
    [13] => Frozen Foods
    [14] =>    Ice Cream
    [17] =>    Popsicles
)

Array
(
    [12] => Beverages
    [15] =>    Soda
    [16] =>    Milk
    [13] => Frozen Foods
    [14] =>    Ice Cream
    [17] =>    Popsicles
)

 

Can you tell me how to just return the last set of array?

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.