Current location: Hot Scripts Forums » General Web Coding » JavaScript » Newbie help - accessing a table


Newbie help - accessing a table

Reply
  #1 (permalink)  
Old 08-18-07, 11:48 PM
dtmp dtmp is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Newbie help - accessing a table

Just need a little help to point me in the right direction to research this. I need to access an 8x8 data table using two supplied variables(x and y). What's the correct operator or function for this in javascript. In my newbie mind, I came up with 16 if-then statements, I'm sure there's a better way.
Reply With Quote
  #2 (permalink)  
Old 08-20-07, 01:38 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
While I am not very sure of what you mean but you could use getElementById() and name your cells by their coordinates (e.g. first cell 1_1, next 1_2, etc)

so whenever you want to access the cell at row 3 , column 5 you just call:
document.getElementById('3_5')
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 08-20-07, 06:45 AM
dtmp dtmp is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hmm, is getElementById() only for elements in the html page? The table I'm referring to is shipping costs for various zones.

After further research, I'm thinking maybe a multidimensional array to store the table info.

To further explain the purpose of the script:
User enters zipcode, # of widgets and carrier(2 options). zone is determined by zipcode, then shipping is looked up in one of the two carrier tables using zone and # of widgets.
Reply With Quote
  #4 (permalink)  
Old 08-20-07, 05:37 PM
jfulton's Avatar
jfulton jfulton is offline
Community VIP
 
Join Date: Apr 2006
Location: Los Angeles, CA
Posts: 660
Thanks: 0
Thanked 0 Times in 0 Posts
"table" = database table, HTML <table>, or something completely different? Could you try to explain your problem a bit more?
Reply With Quote
  #5 (permalink)  
Old 08-22-07, 03:03 PM
TwoD TwoD is offline
Community VIP
 
Join Date: Sep 2003
Location: 404
Posts: 1,813
Thanks: 0
Thanked 0 Times in 0 Posts
If you're just want a lookup-table, here's an easy way..
myArray = [] is short for myArray = new Array()
javascript Code:
  1. myTable = [
  2.    [1, 2, 3],
  3.    [4, 'Hello', 6],
  4.    [7, 8, 9]
  5.  ]
  6. getOne = myTable[0][0]
  7. getHello = myTable[1][1]
  8. getEight = myTable[2][1]
  9. getNine = myTable[2][2]
And there you have a multidimensional array...
If you need to use keys which are not integers, you'll have to use an Object instead of Arrays.
javascript Code:
  1. myTable2 = {
  2.    'key 1': {
  3.       'subkey 1': 1,
  4.       'subkey 2': 2,
  5.       'subkey 3': 3
  6.       },
  7.    'key 2': {
  8.       'subkey 4': 4,
  9.       'subkey 5': 'Hello',
  10.       'subkey 6': 6
  11.       },
  12.    'key 3': {
  13.       'subkey 7': 7,
  14.       'subkey 8': 8,
  15.       'subkey 9': 9
  16.       }
  17. }
  18.  
  19. getOne2 = myTable2['key 1']['subkey 1']
  20. getHello2 = myTable2['key 2']['subkey 5']
  21. getNine2 = myTable2['key 3']['subkey 9']

Hope that helps.
__________________
[W3Schools - learn all about the standards.] [QuirksMode - Browser Quirks] [MS's Online Reference Docs] [DOM in Gecko.]
Please pay attention to stickys, announcements and forum rules, thank you.
Please also remember Code Wrappers and [SOLVED] Marking, this helps everyone.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MYSQL database countll Database 2 06-19-07 04:20 PM
What do you think of my database structure? Oskare100 Database 5 12-27-06 07:43 AM
Pretty simple ( i think) question any help!?!? 0o0o0 PHP 2 04-07-06 10:47 AM
accessing DOM HTML Elements in a dynamically built table. drdexter33 JavaScript 3 02-04-06 03:43 PM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM


All times are GMT -5. The time now is 11:31 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.