Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 28th, 2003, 12:22 PM
Lindagain Lindagain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 4 Lindagain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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!

Reply With Quote
  #2  
Old May 29th, 2003, 08:01 AM
Lindagain Lindagain is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 4 Lindagain User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Creating links on a line above a dynamically generated table


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway