View Full Version : fetch data from txt file
hogtied
06-01-2002, 09:22 AM
I know this is easy stuff to some of you guys but I really don't have the time to look it up.
This is what I'm tring to do:
On my web site I have a Site Rating form that sends data to a text file called SiteRating.txt.
All that is entered into the test is and number 1-5 for each survey.
I was wondering would it be possible to read and count how manys 1's or 2's and so on is in the file??
I think the code would be something like this if it was capable:
loadvariable = (SiteRating.txt, 0, count);
Although I don't know about this thing called level 0.
function count()
{
count how many 1's, 2's, 3's and so on (im not sure on how to code this either..)
}
if someone could help me, I'd be really greatful.
Thanks
_level0 is the main timeline... in many cases it is the same as _root.
so loadVariablesNum('siterating.txt,0);
is telling flash to load the info found in siterating to the main timeline, so that if you are trying to reference them from multiple nested movieclips you would specify _level0.myVariable so that flash knows where to look for the variable.
make sense?
the code used to count the 1s, 2s, .... would totally depend on how the file is formated.
so, how is the file formated?
hogtied
06-02-2002, 04:18 AM
thank you for your reply tg. I understand the level 0 is the same as _root... is there other levels?
As for the file, it hasn't been created as of yet. Since for every survey with is only a star rating system from 1-5 there will be only 1 entry per line. So I believe the file would look something like this..
"SiteRating.txt"
1
1
4
5
3
3
3
2
1
4
on so on.....
I'll ask you this, what is the best format for actionScript?
Comma, Tab, space, or preformatted?
first part:
yes, many levels: _level0,_level1,_level2... don't know the limit, but it is high. you shouldn't need to use them all. heh.
second part:
if you set up your text file like:
rate=1,1,1,3,4,5,2,3,4,3,4,2,3,5,5,1,3&
you can run this code to grab the variables and put them into an array:
//put an empty movieclip to use as the data loader. name it loader
//declare an array to hold the ratings
var ratings=new Array();
//the variables will be loaded into loader's timeline.
loader.loadVariables("siterating.txt");
make sure you give flash enough time to get all the data before trying to use the data. do a search on 'swonk' to learn more. to make sure the data has been loaded before you use it put this code 'on' you mc 'loader'
onClipEvent(data){
_root.ratings=rate.split(",");
/*
'split' tells flash to take all values
(separated by a ',') in the string 'rate'
and put them into the array ratings
(found on the _root timeline).
now that the array is loaded,
you can tell flash to run a
function now or do what you
want to process your data and
display the results.
*/
}
once in an array, you can just sort it or run through a loop and count how many 1's you have etc. etc.
i've been a bit long winded here, hope it all makes since. been along day... moved a piano and freezer, i'm not gonna bother checking for typos clarity etc.
ask if you have any questions.
good luck.
hogtied
06-02-2002, 05:24 AM
i see how what you described would work, but I have no control of how the file is going to look like.... I'll explain in detail:
I have a website and at the bottom it asks users "How would you rate this site?" then it has 5 star rating picture with raido buttons in front. If a user clicks lets say 3 star. Then the form sends that information to a file called SiteRating.txt with the value 3. I could have it send data like rate = 3. What I understand of this is that everytime a new user clicks on a rating the data will be enter into the file per row per user. From this the file would look something like:
1
3
3
4
2
3
1
if i have the form send data with the "rate=" it would look the same as above but with the "rate=" in front of the number. This is what I don't understand on how to get that information and to count how many of the same numbers there are. So I could display how many people choose which stars. For instance in the example above 2 people thought my site is worth 1 star and 3 people think my site is worth 3 stars.
If this still doesn't make sense let me know, but again thanks for repling..
files that flash gets data from needs to be in a pretty specific formula:
variableName = variable value
if you have multiple variables, they need to be separated by an ampersand ('&').
so it could look like this:
var=value&var2=value&var3=value
for your situation, see if you can figure out a way to get your file to look like this (maybe):
rates=1
1
2
3
3
1
4
5
5
4
5
3
3
2
then when you try the code i gave you earlier you can try this code as an alternate:
onClipEvent(data){
_root.ratings=rates.split("\n");
//if that doesn't work try:
//_root.ratings=rates.split("\r\n");
//or
//_root.ratings=rates.split("\r");
//for testing purposes (to see what your array looks like)
trace(_root.ratings.join());
}
'\n' is the character for a linefeed
'\r' is the character for a carriage return
so basically your telling flash to add an element to the array everytime it finds a linefeed in the string rates.
1 possible problem: the last character in the file may be a linefeed. this will add an element to the array with a null value. this might hose your array, but i don't think so.
hogtied
06-02-2002, 09:40 PM
thanks tg... I'll give that a try and let you know what happened.. in the meantime do you think that there is another easier way to do this. Maybe javascript or C++ or even VB? except the last two won't work for me because my web page is running o a unix server.
i'm sure there are other ways of doing this, but i don't know if they would be easier.
btw, how is the file being written when the user submits the rating?
hogtied
06-03-2002, 08:45 AM
Here I'll write out the code as it stands now.
each raido button acts like a submit button. As soon as you click on the radio button it sends the data to a file called SiteRatings.txt.... I've created this page in FrontPage 2000 and the data is being sent through something called a webbot. It works it just appends the new data to a new line and i don't think there is much i can do about that but write my own CGI. Which i don't have a program to do that in execpt for C++ and VB but I'm not sure if they'll work like perl or whatever.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.