50303

Populate Dropdown Menu in PHP from JSON

Question:

I have the following JSON in a file menu.txt:

[{ "title":"About", "url":"/about", "nodes":[ {"title":"Staff","url":"/about/staff"}, {"title":"Location","url":"/about/location"} ]},{ "title":"Contact", "url":"/contact" }]

Using the following Javascript:

var menu = // JSON from menu.txt function parseNodes(nodes) { var ul = document.createElement('UL'); for(var i=0; i < nodes.length; i++) { ul.appendChild(parseNode(nodes[i])); } return ul; } function parseNode(node) { // takes a node object and turns it into a <li> var li = document.createElement('LI'); var a = ('<a href="'+node.url+'">'+node.title+'</a>'); li.innerHTML = a; if(node.nodes) li.appendChild(parseNodes(node.nodes)); return li; } document.getElementById('menu').appendChild(parseNodes(menu));

I can create the following HTML:

<div id="menu"> <ul> <li><a href="/about">About</a></li> <ul> <li><a href="/about/staff">Staff</a></li> <li><a href="/about/location">Location</a></li> </ul> </li> <li><a href="/contact">Contact</a></li> </ul> </div>

Is there an easy way to do this in PHP as to be more search-engine friendly? Preferably a PHP script that I could include menu.php in my pages?

<?php $file = "menu.txt"; $json = json_decode(file_get_contents($file), true); // ???? ?>

Answer1:

This should get you started ;)

<?php function parseNodes($nodes) { $ul = "<ul>\n"; foreach ($nodes as $node) { $ul .= parseNode($node); } $ul .= "</ul>\n"; return $ul; } function parseNode($node) { $li = "\t<li>"; $li .= '<a href="'.$node->url.'">'.$node->title.'</a>'; if (isset($node->nodes)) $li .= parseNodes($node->nodes); $li .= "</li>\n"; return $li; } $json = '[{ "title":"About", "url":"/about", "nodes":[ {"title":"Staff","url":"/about/staff"}, {"title":"Location","url":"/about/location"} ]},{ "title":"Contact", "url":"/contact" }]'; $nodes = json_decode($json); $html = parseNodes($nodes); echo $html;

Recommend

  • Batch file working differently if ran in administrator mode
  • Convert Func to Func
  • How do I prepend to a stream in Bash?
  • ng-repeat not working with table but works with list
  • react native create element with string
  • Swipe Gesture Recognizer not working for me
  • Firefox augments the content-type of XMLHttpRequest
  • Return to second to last URL in MVC (return View with previous filter conditions applied)?
  • Function JavaScript : on Menu CSS HTML
  • Can't delete li from to-do list
  • Why does the following throw an “Object doesn't support property or method 'importNode
  • ASP.NET windows authentication should always ask for credentials
  • Google analytics measurement protocol session timeout and query time limits
  • Python/Javascript: WYSIWYG html editor - Handle large documents fast and/or design theory
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Is there a package like bigmemory in R that can deal with large list objects?
  • Getting error 'Cannot read property 'document' of undefined' while importing exp
  • Azure webjobs output logs indexing taking very long
  • CakePHP ACL tutorial initDB function warnings
  • ADO and msqli connections very slow
  • Marklogic : Query response time is very high
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • FileReader+canvas image loading problem
  • Using jQuery closest() method with class selector
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Change an a tag attribute in JavaScript based on screen width
  • Jquery - Jquery Wysiwyg return html as a string
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal