Jump to content

Arranging arrays?


mackieimages

Recommended Posts

I am in the process of trying to create a very simple form/widget using some javascript. I'm new to this and have run through all the tutorials but can't seem to add it all together to get what I want. If someone could help kick-start this for me that would be greatly appreciated. Here's an example of what I'm trying to accomplish: I have three arrays (and this is assuming of course I should be using arrays)var age = new Array (5);age[0] = "50"age[1] = "55"age[2] = "60"age[3] = "65"age[4] = "70"var provider = new Array (5);provider[0] = "MetLife"provider[1] = "Prudential"provider[2] = "MedAmerica"provider[3] = "John Hancock"provider[4] = "Alliaz"var cost = new Array (5);cost[0] = "$153.95"cost[1] = "$165.68"cost[2] = "$204.90"cost[3] = "$254.30"cost[4] = "$382.12" My goal is to create a form that has two drop-downs containing the values within the 'provider' and 'age' arrays. Based on whatever combination of the two a user chooses, and after hitting a submit button, they will be given one of the values from the 'cost' array (FYI - there would probably be more values in the 'cost' array in the future).Am I even on the right track? My problem is I don't know which function to use and how (if I should be) combining this with an if statement. I just need to see the basic structure of this and should be able to take it from there...I hope. Thanks in advance for any help.

Link to comment
Share on other sites

you will probably want to do something like this

//replace $0.00 with the real costvar lookup = {	'50': {		'MetLife': '$0.00',		'Prudential': '$0.00',		'MedAmerica': '$0.00',		'John Hancock': '$0.00',		'Alliaz': '$0.00'	},	'55': {		'MetLife': '$0.00',		'Prudential': '$0.00',		'MedAmerica': '$0.00',		'John Hancock': '$0.00',		'Alliaz': '$0.00'	},	'60': {		'MetLife': '$0.00',		'Prudential': '$0.00',		'MedAmerica': '$0.00',		'John Hancock': '$0.00',		'Alliaz': '$0.00'	},	'65': {		'MetLife': '$0.00',		'Prudential': '$0.00',		'MedAmerica': '$0.00',		'John Hancock': '$0.00',		'Alliaz': '$0.00'	},	'70': {		'MetLife': '$0.00',		'Prudential': '$0.00',		'MedAmerica': '$0.00',		'John Hancock': '$0.00',		'Alliaz': '$0.00'	}	};var age = document.getElementById('agelist').value;var provider = document.getElementById('providerlist').value;var cost = lookup[age][provider];

This is going to get messy in a hurry. You may want to consider looking at a database to store this stuff then doing a query to look it up. Are the costs based on a forumla or something. That would be much easier than maintaining a lookup table.

Link to comment
Share on other sites

You probably would want to create objects as ANG suggests. I show it done with arrays as you provided them. To get the values out of your data, you can try DOM functions like these. Even if you do go with a database solution on the server side, this approach is also suitable to AJAX if you want to go that route in the future.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>    <head>        <title></title>        <script type="text/javascript">            var age = ["50","55","60","65","70"];            function ce(t) {                return document.createElement(t);            }            function gg(id) {                return document.getElementById(id);            }            function buildAgeSelector () {                var ages = ce("select");                ages.id = "age";                gg("panel").appendChild (ages);                var a;                                for (var i = 0; age[i]; i++) {                    a = ce("option");                    ages.appendChild(a);                    a.id = "ages_" + i;                    a.value = age[i];                    a.text = age[i];                }            }            window.onload = buildAgeSelector;        </script>    </head>    <body>        <div id="panel"></div>    </body></html>

Link to comment
Share on other sites

Thank you for the help. I'll start playing around with this code while trying not to break anything. The costs are not based on any formulas, at least in this example. That may be something I try to deal with later but for now these are set costs that will not vary.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...