gibbo1715 Posted February 16, 2012 Share Posted February 16, 2012 All I am not the best at PHP and have been trying to work out how to make a treeview from an array. I managed to get as afar as populating my array as i would like and I can get those values back but now im really stuck What id love to be able to do is use my area field as the parent and my title field as the child and have a counter on the parent with no of child nodes. If someone can help me out with that bit id be really greatful, as I ve already sorted my javascript etc this is my last problem and its driving me nuts Many Thanks for looking and i really do appreciate any help Gibbo <?php $row = 0; $myarray = array(); foreach ($view->result as $result) { $arg = $view->render_field('field_decision_type_value', $row); $arg2 = $view->render_field('title', $row); $myarray[$row]["area"]=$arg; $myarray[$row]["title"]=$arg2; $row++; } asort($myarray); $last = count($myarray) - 1; $rowcount=0; while ($rowcount <= $last) { print $myarray[$rowcount]["area"]; print $myarray[$rowcount]["title"].'|<br />'; $rowcount++; } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2012 Share Posted February 16, 2012 Your question is about an array that you have (which I guess the code above is creating) and outputting it in a specific format. You need to describe the format of the array and how you want the output generated. The code you have above really doesn't make sense to what I understand you have/want. The code above will simply create a multidimensional array with each sub-array consisting of two values "area" and "title". You state "id love to be able to do is use my area field as the parent and my title field as the child", but they are not a parent/child of each other in the current format of your array. They are siblings. Further the asort() wouldn't do anything (if I understand the code correctly) since it will only sort the values of the first level of the array, which are arrays. It wouldn't sort by the values of the sub-arrays. If you have multiple records with the same "area" then you need to make the area the index in the parent array with the "title" as the values in sub-arrays. But, I am really just guessing here because I really have no idea what your data looks like since you only provided code for building the array. You really need to show the input data you are working with and an example of the output you want generated. But, here is a *guess* at what I *think* you may want. //Put results into a multi-dimensional array with //the area as the key and the titles as values $data_array = array(); foreach ($view->result as $result) { //Hey look, descriptive variable names! $area = $view->render_field('field_decision_type_value', $row); $title = $view->render_field('title', $row); $data_array[$area][] = $title; } //Sort the array by the 'area' key values ksort($data_array); //Output the results foreach($data_array as $area => $titles) { //Display area header w/ title count $title_count = count($titles); echo "<b>{$area} ({$title_count})</b>\n"; //Sort the titles asort($titles); //Display the titles for the current area echo "<ol>\n"; foreach($titles as $title) { echo "<li>{$title}</li>\n"; } echo "</ol><br>\n"; } Quote Link to comment Share on other sites More sharing options...
gibbo1715 Posted February 16, 2012 Author Share Posted February 16, 2012 Many thanks, i just needed to re add the $row++; and that gave me exactly what I was after Thanks so much for helping with this, been trying to figure it out all day but new to PHP, certainly learning a lot though Quote Link to comment 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.