paruby Posted March 21, 2011 Share Posted March 21, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/ Share on other sites More sharing options...
paruby Posted March 21, 2011 Author Share Posted March 21, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/#findComment-1190141 Share on other sites More sharing options...
gizmola Posted March 21, 2011 Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/#findComment-1190150 Share on other sites More sharing options...
paruby Posted March 21, 2011 Author Share Posted March 21, 2011 Thank you gizmola. I will test it in a bit. My question now is, what does the "&" do before the variable name (&$arr)? I have seen it before, and tried googling it, but have not found it...? Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/#findComment-1190359 Share on other sites More sharing options...
gizmola Posted March 21, 2011 Share Posted March 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/#findComment-1190440 Share on other sites More sharing options...
paruby Posted April 22, 2011 Author Share Posted April 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/231235-recursive-tree-w-adjacency-list-and-array/#findComment-1204733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.