SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Evangelist CapitalWebHost's Avatar
    Join Date
    Apr 2003
    Location
    Albany, NY
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    List tags from arrays code needed.

    Need script to build HTML code for 3 level list from 3 arrays.

    For example.

    I have 3 arrays..

    (1)
    Year[1]='2002';
    Year[2]='2003';

    (2)
    Month[1][1]='January';
    Month[1][2]='February';
    ...
    Month[1][12]='December';
    Month[2][1]='January';
    ...
    Month[2][4]='April';

    (3)
    Report[1][1][1]="Report A"
    Report[1][1][2]="Report B";
    Report[1][2][1]="Report C";
    Report[2][1][1]="Report D";


    I need a script that would take these 3 arrays and build this:

    Code:
    <ul>
       <li>2002</li>
    	  <ul>
    		  <li>January</li>
    			  <ul>
    				  <li>Report A</li>
    				  <li>Report B</li>
    			  </ul>
    		  <li>February</li>
    			  <ul>
    				  <li>Report C</li>
    			  </ul>
    		   <li>March</li>
    		   ....
    		   <li>December</li>
    		</ul>
       <li>2003</li>
    	  <ul>
    		  <li>January</li>
    			  <ul>
    				  <li>Report D</li>
    			  </ul>
    		  <li>February</li>
    		  <li>March</li>
    		  <li>April</li>
    	   </ul>
    
    </ul>

    Thoughts?

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have 3 arrays..
    Not exactly...you've got a bunch of nested arrays as well, some seemingly redundant. It's unclear how you're structuring your data (and why). Reading out arrays into formatted HTML is easy; it's the conceptualizing of the process that's important. Could you explain what's happening?

  3. #3
    SitePoint Evangelist CapitalWebHost's Avatar
    Join Date
    Apr 2003
    Location
    Albany, NY
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically, what I'm trying to do is build a dynamic menu. The array structure above is what's in place currently and is used to populate 3 dropdowns (first selection determines second boxes, which determines 3rd), a 3 tier menu.

    I want to convert this structure to use a collapsing list format via dhtml and javascript. THAT part I have done and works fine...Since these arrays are already built and populated, I'd figured I'd use the same structure, but by all means please suggest something better if you want.

    The idea is that each month we produce a set of 7-8 reports..same reports, just for the previous months data.

    Currently the first single level array is the year...and this gets a new element added to it at start of year.

    Year[x]

    The next one is the months for each of the years in the previous array...it gets added to as the year progresses...

    Months[x][y]

    where x corresponds to the 'x' value in the Years array, and 'y' is the month (1-12). This is like this so that ONLY the months that have data will show, not upcoming months, months that were before the original start of this method, etc.

    The third one holds several values....and is formatted as..

    Reports[x][y][z]

    where 'x' again goes back to it's related Years[x] value, 'y', relates back to the Months[x][y], and 'z' is the report order. The Reports value has 2 parts, the report title, and the URL where it's located.

    I suppose you might be able to get away with ONE array of year/month/title/url for building a list format like I want..hmm. This structure is from a freebie script at www.dhtml.com I think...it's been in production here for a few years not, but it's getting unwieldly to update and I just want to make the navigation a little more smoother...

    I suppose I could use one array, it might also have the side benefit of being able to build lists based on sort order....By Year/month/report or by Report/year/month. That would be the ultimate...but my Javascript skills are rusty as heck..lol.

    If only our 0S/390 USS servers would run friggin' PHP...lol


    Hmm..then again, maybe using XML instead of a JS array for the input would be better....

  4. #4
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use associative arrays.

    Code:
    var Report=new Array();
    Report["2002"]=new Array();
    Report["2002"]["january"]=new Array();
    Report["2002"]["january"]["id12345678"]="Report A";
    Report["2002"]["january"]["id12345679"]="Report B";
    etc.

  5. #5
    SitePoint Evangelist CapitalWebHost's Avatar
    Join Date
    Apr 2003
    Location
    Albany, NY
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, but that wouldn't be condusive to building the list dynamically...

  6. #6
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what you meant by condusive , I guess you meant you wouldn't know what to do with those associative arrays, so I've made a function to pull out a list of results as you wanted.

    Code:
     
    var Report=new Array();
    Report["2002"]=new Array();
    Report["2002"]["january"]=new Array();
    Report["2002"]["january"]["id12345678"]="Report A";
    Report["2002"]["january"]["id12345679"]="Report B";
    Report["2002"]["february"]=new Array();
    Report["2002"]["february"]["id12345680"]="Report C";
    Report["2002"]["february"]["id12345681"]="Report D";
    Report["2002"]["february"]["id12345682"]="Report E";
     
    function makeList(array){
       var dStr="<ul>";
       if (typeof array=="object"){
    	  for (var i in array){
    		 dStr+="<li>"+i;
    		 if (typeof array[i]=="object") {dStr+=makeList(array[i]);}
    		 else {dStr+=" --> "+array[i];}
    		 dStr+="</li>";
    	  }
       }
       dStr+="</ul>";
       return dStr;
    }
     
    var str=makeList(Report);
    document.write(str);
    I hope it helps.

  7. #7
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's actually spelt conducive:
    conducive

    adj : tending to bring about; being partly responsible for; "working conditions are not conducive to productivity"; "the seaport was a contributing factor in the growth of the city"; "a contributory factor" [syn: contributing(a), contributive, contributory]
    MarcusJT
    - former ASP web developer / former SPF "ASP Guru"
    - *very* old blog with some useful ASP code

    - Please think, Google, and search these forums before posting!

  8. #8
    SitePoint Evangelist CapitalWebHost's Avatar
    Join Date
    Apr 2003
    Location
    Albany, NY
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    M@cro is korrect...lol...my speeling ain't sew grate at tymes. LOL.

    Anyway..

    Thanks, Nacho..that EXACTLY what I was looking for...only thing is I need the last tier of the list to be a link, since the value of each array element will be a url to the report.
    Code:
    2002
    • january
      • id12345678 --> Report A
      • id12345679 --> Report B
    should be.. 2002
    [img]images/smilies/smile.gif[/img] You did in that time what I would have spent a week on..lol..

    I guess I could do this...
    Code:
    <LI>2002 var Report=new Array();
    Report["2002"]=new Array();
    Report["2002"]["january"]=new Array();
    Report["2002"]["january"]["MCR"]="http://somelink.com";
    Report["2002"]["january"]["Started Tasks"]="http://somelink.com";
    Report["2002"]["february"]=new Array();
    Report["2002"]["february"]["MCR"]="http://somelink.com";
    Report["2002"]["february"]["Started Tasks"]="http://somelink.com";
    Report["2002"]["february"]["Batch"]="http://somelink.com";
    
    function makeList(array){
    var dStr="<ul>";
    if (typeof array=="object" ){
    for (var i in array){
    dStr+="<li>"+i;
    if (typeof array[i]=="object" ) {dStr+=makeList(array[i]);}
    else {dStr+=" [<a href=""+array[i]+"">View Report</a>]";}
    dStr+="</li>";
    }
    }
    dStr+="</ul>";
    return dStr;
    }
    Last edited by CapitalWebHost; May 31, 2003 at 10:46.

  9. #9
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi CapitalWebHost,

    I was going to just post you the code but I changed my mind. I thought that including a link into a given code is something you should be able to do by yourself or unless make an effort for it.

    Try to modify the code I've given you to suit your needs EXACTLY.

    If you try it and do not succeed, come back and I'll help you out with the rest. Deal?

  10. #10
    SitePoint Evangelist CapitalWebHost's Avatar
    Join Date
    Apr 2003
    Location
    Albany, NY
    Posts
    417
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sounds fair..lol..


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •