#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    4
    Rep Power
    0

    Creating links on a line above a dynamically generated table


    I have made a page that dynamically generates a course list from a database. The page looks something like this:

    A-B-C-D-E-F-G-H-I-J-K-L-M-N-O-P-Q-R-S-T-U-V-W-X-Y-Z
    Course Name Course Number
    Abnormal Psychology 60100001
    Access Expert 60155400
    T Back to Top
    Testing 4 8950566

    I get the names to generate dynamically making the table the exact number of rows it needs to be depending on the number of courses in the database. I get the letter header, the T, to generate dynamically when the first letter of the course changes so that all the courses starting with the same letter show up under a header row with that letter above it.

    Now I need to get that list of letters at the top to link to the header rows. I can dynamically add <a name=""> to the header row as its generated and in the "" I just insert the first letter I extracted to test for a change in the first letter. But the row A-B-C-etc... is generated before the rows of course names. My supervisor wants only the letters that have courses to show up as links. Say that the table generates no courses that begin with the letter X, then the X in the top row of the table will just be printed without being a link.

    I don't think I can insert an if statement in that top letter line because it is outside of my for loop that generates the rows of the table. I could make a for loop up there and run through the hashmap testing the first letter of all the courses before I generate them and make links first. Would it be ok to run though the hashmap 2x in one page? Test all the first letters of the courses and make links, then run through the hashmap again to make the rows?

    Any reply is greatly appreciated!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    4
    Rep Power
    0
    Ok, I figured this one out. It is a bit complicated but basically I had to run through the hashmap two times. Once to post all the courses and generate the table. The second time was to create the top header that runs through the alphabet and creates links for letters that have courses under them.

    So first I made 2 arrays. One holds the letters of the alphabet and the other holds the same number of elements as the alphabet but stores all 0s. I have 2 for loops. One loops through the first array, the one holding all the letters of the alphabet. Then the second for loop inside this one loops through the courses. All it does is test, does the first letter of this course match any of the letters in the alphabet. When a match is found then the int array assigns that element of the array to 1. So when the alphabet for loop is checking B the second element in alphabet[] or alphabet[1], if a course's first letter matches then it sets countArray[1] to 1. It doesn't matter if it does this once or 7 times.

    When these for loops are done I have an array that holds 1s for the letters that matched and 0s for the letters that didn't match. Let's say the letter L was found to be a match. Letter L is the 12th element in alphabet[] or element alphabet[11] since all arrays start at 0. So in my countArray element countArray[11] will hold a 1. If L was not found then the 12 element in countArray would not change, it would stay at what I initialized the array with, all 0s.

    Then when I go to print A-Z at the top of my table, I make one more for loop. It loops 26 times, testing if countArray[0] == 1, print alphabet[0] as a link, otherwise just print it normal.

    Effective, easy for me to understand (which isn't saying much, lol) and relatively mess-free.

    If anyone has an idea of how I could have done this easier I'd like to hear some theories, in case I have to recreate a similar page in the future.

IMN logo majestic logo threadwatch logo seochat tools logo