PDA

View Full Version : combo box from external data source


Grizzly
10-19-2004, 05:02 AM
hi all, im trying to get external data and populate my combo box, i have a text file named cars.txt which contains:

&carName=Mazda3,Mazda6,Mazda Tribute &carValue=car1.htm,car2.htm,car3.htm &totalCars=3

the first variable carName is for the label , while the carValue is its corresponding value

i used this code to get the file:
for the first frame:
stop();
var carURL:String = "cars.txt";
var cars:LoadVars = new LoadVars();
function loadScript() {
cars.load(carURL);
gotoAndStop("loading");
}
loadScript();
then on the second frame ("loading" frame)
function carsLoaded(){
gotoAndStop("display");
}
cars.onLoad= carsLoaded;

then finally the last frame ("display" label):
stop();
var allCars:Number = cars.totalCars;
var carName:String = car.carName.split(",");
var carValue:String = car.carValue.split(",");
cars_cb.addItem("Choose a car");
for(i=0;i>allCars;++i) {
cars_cb.addItemAt(i, carName+i, carValue + i);
};

unfortunately i'm not getting any results, all i see in the combobox is "Choose a car", i traced the variables and i get a "undefined,undefined"

sombody help!!! :confused:

CyanBlue
10-19-2004, 06:54 PM
This works on one frame test file...
stop();
var carURL:String = "cars.txt";
var cars:LoadVars = new LoadVars();
cars.load(carURL);
cars.onLoad = carsLoaded;
function carsLoaded()
{
trace(this);
var allCars:Number = parseInt(this.totalCars);
var carName:String = this.carName.split(",");
var carValue:String = this.carValue.split(",");
cars_cb.addItem("Choose a car");
for (i = 0; i < allCars; ++i)
{
trace("i = " + i + " carName[i]= " + carName[i] + " carValue[i] = " + carValue[i]);
cars_cb.addItemAt(i + 1, {label:carName[i], data:carValue[i]});
}
}

Grizzly
10-20-2004, 04:50 AM
Finally working! Thanks Cyan! :D

but i'm a bit confused at where i have gone wrong. and i don't quite get :
var allCars:Number = parseInt(this.totalCars);
var carName:String = this.carName.split(",");
var carValue:String = this.carValue.split(",");

what does parseInt do? and what happens when you do split a string? is it just one string or is it divided into several variables or into an array :confused: ?

Appreciate it if someone could shed a light on the subject, Thanks :D

CyanBlue
10-20-2004, 05:10 AM
Don't call me Cyan cuz I am not Cyan... If you call me that one more time, I'm going to remove your name from my memory... :D

Okay... parseInt() function is used to convert the string to numeric value because all the external data coming into the Flash is 'supposed' to be the string, so you'd need to manually convert it... (It looks like Flash is actually handling it as numeric value most of the times, but there are cases where Flash is having problem, so I think it is a good habit to make...)

split() function... You might want to read the Flash manual for that, but the gist is that what you have is going to be put into the array item, yet you were calling it wrong... You'd need to call it like carName[i] to access the data within the array...

annexion
10-21-2004, 05:54 AM
Just to clarify...

parseInt doesn't just convert a string to a number. It essentially has the purpose of stripping whitespace and converting to a number. However, it does not strip valid alpha characters from the beginning of a string. It will, however, strip valid alpha characters from the end of the first sequence of numbers, ignoring any numbers thereafter.

Consider the following:


a="aaa9";
b=" \n\r\t9aaa";
c="9aaa";
trace(parseInt(a));
trace(parseInt(b));
trace(parseInt(c));