Jump to content

Recommended Posts

Hello,

I've got a text file that contains a menu structure:

HEADER 1

ITEM1 > ITEM1-1.HTML (or PHP)

ITEM2 > ITEM1-2.HTML (or PHP)

HEADER 2

ITEM1 > ITEM2-1.HTML (or PHP)

ITEM2 > ITEM2-2.HTML (or PHP)

ITEM3 > ITEM2-3.HTML (or PHP)

I'd like to use Javascript to build this menu, so that if I make a change (either add or delete to the text file) it is reflected in the web application. The menu should be vertical, left-aligned with the content appearing on the right pane.

+----------+---------------------------------------------+
|  ITEM  |   CONTENT                              |

|             |                                                    |

|             |                                                    |

+----------+---------------------------------------------+

Can someone direct me to a library that could do something like this?

Thx,

M

Edited by MeOnTheWeb
Additional clarification
Link to comment
https://forums.phpfreaks.com/topic/307367-automatic-menu-generation/
Share on other sites

Do it in the HTML directly. It'll save you a lot of time and effort.

If you want to explore how one might do this academically, then I would say to forget text files and use something that's easy for you to read and write and especially easy for a computer to read: JSON. There are a number of ways to get that data into the page, then you have code to work with it.

  • Great Answer 1

I have to agree strongly with requinix here.  There are many ways to format files to include configuration data that have support libraries and functions allowing you to read & write and convert them into data structures the language understands.  The ones I typically see are .ini, yaml and xml. 

You also have the option of using a plain old php array and serialize().  

Since your desire is to then manipulate this data and output html via javascript code, as suggested, json is the single best practice which you see used just about everywhere for REST api's as it is the best way to get serialized data into javascript objects.

You could base your structure on a php array and use json_encode to convert it to json for output to your menu code, or just store it in json format on disk.  Either way you will be able to accomplish what you want in a standard way that is highly supported both by php and javascript.  

 

Here's some code to consider:

<?php

$menu = array ('Menu1' => array('sub item1' => 'subitem1.html', 'sub item2' => 'subitem2.html'), 'Menu2' => array('sub item3' => 'subitem3.html'), 'Menu3' => array() );

var_dump($menu);

echo json_encode($menu);

 

Outputs:

array(3) {
  ["Menu1"]=>
  array(2) {
    ["sub item1"]=>
    string(13) "subitem1.html"
    ["sub item2"]=>
    string(13) "subitem2.html"
  }
  ["Menu2"]=>
  array(1) {
    ["sub item3"]=>
    string(13) "subitem3.html"
  }
  ["Menu3"]=>
  array(0) {
  }
}
{"Menu1":{"sub item1":"subitem1.html","sub item2":"subitem2.html"},"Menu2":{"sub item3":"subitem3.html"},"Menu3":[]}

 

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.