Jump to content

Array Not Defined


The Little Guy

Recommended Posts

OK... I am sending my function a word like: Art, Business, Games... etc.

 

The problem is that when I print the word, to the screen it prints, but then when I try to do something with it like this:

 

function buildSubCats(cat){
var lvl1Arts = Array();
lvl1Arts[0] = "Painting";
lvl1Arts[1] = "Digital Art";
lvl1Arts[2] = "Drawing";
lvl1Arts[3] = "Models";
lvl1Arts[4] = "Sewing";
lvl1Arts[5] = "Weaving";
lvl1Arts[6] = "Origami";
var len = eval("lvl1"+cat+".length");
for(var i=0;i<len;i++){
	opt += '<option id="'+eval("lvl1"+cat[i])+'" value="'+eval("lvl1"+cat[i])+'">'+eval("lvl1"+cat[i])+'</option>';
}
}

 

If I were to send the word Art I would get an error saying: lvl1A is not defined

 

First of all where does A come from? A should be Arts, not A

Second it only does the first letter for some of the options, the ones with only one letter after lvl1, have sub arrays, otherwise it shows the full thing (lvl1Books).

 

Why is it doing this?

 

The page: http://secret.publicsize.com/advanced

Link to comment
https://forums.phpfreaks.com/topic/70929-array-not-defined/
Share on other sites

eval is evil.

 

Are you using a multidimensional arrays? if so, use a hash/object instead of an array. then you wont need to use eval to get to the sub array

 

let me see what i can do for you

 

function buildCat(cat){

var lvl1 = {

'Arts': ['Painting', 'Drawing']

};

for(arr in lvl1[cat] ){

console.log(lvl1.Arts[arr]);

}

}

 

console.log only works in ff if you have firebug installed. try alert() instead if you do not have that set up. but from my example you should be albe to build out your select box. notice how i use {} [] as shorthand for new Object or new Array - that is in the interest of saving time and processing, there is no need to call the object

Link to comment
https://forums.phpfreaks.com/topic/70929-array-not-defined/#findComment-356635
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.