17691

Traverse Array and Display in markup

I want to traverse this array and display, in bulleted divs. I know this look a lot suspicious, like do it yourself. But I cant get past the nested foreach problem, especially, when it comes to deph of the threads. I got an answer in a previous question Traverse Array and Display In Bullet Points, but i'm not sure how to change the markup in the solution that was given.... or if its possible. Below is the array, and i have a link to the pastebin of the markup i would like, but would be awesome if the markup can be changed easily in the future, ie markup views separated from logic.

Array ( [1] => Array ( [id] => 1 [comment_title] => comment title [comment] => a [comment_date] => comment date [parent_id] => 0 [children] => Array ( [3] => Array ( [id] => 3 [comment_title] => comment title [comment] => this is another comment [comment_date] => comment date [parent_id] => 1 [depth] => 0 [child_count] => 0 [children] => ) [4] => Array ( [id] => 4 [comment_title] => comment title [comment] => this is a third comment [comment_date] => comment date [parent_id] => 1 [depth] => 0 [child_count] => 0 [children] => ) ) [depth] => 1 [child_count] => 2 ) [2] => Array ( [id] => 2 [comment_title] => comment title [comment] => this is yet another comment [comment_date] => comment date [parent_id] => 0 [children] => Array ( [5] => Array ( [id] => 5 [comment_title] => comment title [comment] => another comment [comment_date] => comment date [parent_id] => 2 [children] => Array ( [7] => Array ( [id] => 7 [comment_title] => comment title [comment] => Hey im a comment! [comment_date] => 1/1/1970 [parent_id] => 5 [children] => Array ( [8] => Array ( [id] => 8 [comment_title] => comment title [comment] => I disagree [comment_date] => 1/1/1970 [parent_id] => 7 [children] => Array ( [9] => Array ( [id] => 8 [comment_title] => comment title [comment] => My comment is... [comment_date] => 1/1/1970 [parent_id] => 8 [children] => Array ( [10] => Array ( [id] => 8 [comment_title] => comment title [comment] => Hey im a comment! [comment_date] => 1/1/1970 [parent_id] => 9 [depth] => 0 [child_count] => 0 [children] => ) ) [depth] => 1 [child_count] => 1 ) ) [depth] => 2 [child_count] => 1 ) ) [depth] => 3 [child_count] => 1 ) ) [depth] => 4 [child_count] => 1 ) [6] => Array ( [id] => 6 [comment_title] => comment title [comment] => This is the last comment [comment_date] => 1/1/1970 [parent_id] => 2 [depth] => 0 [child_count] => 0 [children] => ) ) [depth] => 5 [child_count] => 2 ) )

Here is the markup i want to use. http://pastebin.com/MBppxiC7

Answer1:

In the code that I provided you before, just try adding pieces of code and you will get how it works. Once you begin understanding the code I made, you will be able to extend it and add your extra features.

Answer2:

you can try making a recursive function which checks for the child_count and if its >1 then function will again traverse the children key of that array to get the children. Basically you can create a function and can call it when needed.

Recommend

  • TinyMCE when adding bullets the bullets do not show in front end/view
  • SLF4J - What is a dangling or detached marker?
  • Twitter json API foreign characters
  • Using BOOST_FOREACH with a constant intrusive list
  • What caused hibernate generate a update clause?
  • BeautifulSoup: Can't convert NavigableString to string
  • Needing help with JSON problems
  • position: fixed with margin: auto in IE9/10
  • Colorize knockoutjs comment bindings in code editor
  • Unable to run app-client that is accessing an EJB on GlassFish v3
  • Why am I getting an Array of an Array with Doctrine 2 & Symfony 2 Data Transformer?
  • Is there any purpose for h2-h6 headings in HTML5?
  • Prolog Ambiguous Output
  • How to draw a line dynamically in android [duplicate]
  • Android Studio Can't Find tools.jar
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • android.support.v7.widget.Toolbar VectorDrawableCompat IllegalStateException when using support lib
  • Checking if an array in C is symmetric
  • Redux Form - Not able to type anything in input
  • How to view images from protected folder with php?
  • Loading .coffee files via a view in Rails
  • Webgrid not refreshing after delete MVC
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Jquery UI tool tip close icon
  • Typescript - Unable to get 'import' statement to function
  • Fetching methods from BroadcastReceiver to update UI
  • How reduce the height of an mschart by breaking up the y-axis
  • Excel - Autoshape get it's name from cell (value)
  • Submit form in a displaytag pagination
  • Check if a string to interpolate provides expected placeholders
  • WinForms: two way TextBox problem
  • ActionScript 2 vs ActionScript 3 performance
  • To display the title for the current loaction in map in iphone
  • Calling of Constructors in a Java
  • RestKit - RKRequestDelegate does not exist
  • Transpose CSV data with awk (pivot transformation)
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • Qt: Run a script BEFORE make