PDA

View Full Version : Splitting text into separate arrays?



bombdigiti
May 5th, 2008, 01:33 AM
hey, i am trying to split some data from a database that is being pulled by php into separate arrays. I can bring all the information into a single array and trace its position in that array but i need to have the information split depending on the the php output.

for example:
&gallerytitle=imageone|imagetwo|imagethree|&galleryimages=img1.jpg|img2.jpg|img3.jpg
i need the gallerytitle to be one array and the galleryimages to be another array

any help would be amazing, thanks!

code
ActionScript Code:

import flash.events.Event;
import flash.net.*;

var myRequest:URLRequest = new URLRequest("http://localhost/media_gallery/loadinfo.php");
var myLoader:URLLoader = new URLLoader();
var myVariables:URLVariables = new URLVariables();

myRequest.method = URLRequestMethod.GET;
myRequest.data = myVariables;

function onLoaded(evt:Event):void {
var namearray:Array = new Array();
var valuestring:String = myLoader.data;
trace(valuestring);
namearray = valuestring.split("|");
trace("here we get the data back: "+namearray[15]);
}

myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);

amarghosh
May 5th, 2008, 02:07 AM
//this code would help u started:
var src:String = "gallerytitle=imageone|imagetwo|imagethree|&galleryimages=img1.jpg|img2.jpg|img3.jpg";
var urlvars:URLVariables = new URLVariables(src);
trace(urlvars.gallerytitle);//imageone|imagetwo|imagethree|
trace(urlvars.galleryimages);//img1.jpg|img2.jpg|img3.jpg
var titles:Array = String(urlvars.gallerytitle).split("|");
var images:Array = String(urlvars.galleryimages).split("|");
trace(titles);//imageone,imagetwo,imagethree,
trace(images);//img1.jpg,img2.jpg,img3.jpg

the last comma in the titles is because there is an extra whitespace (due to unwanted '|' at the end of gallerytitle) in the array;

bombdigiti
May 5th, 2008, 02:27 AM
ok, great... so how would i go about getting the output from php into that string?
something like this ?



var myRequest:URLRequest = new URLRequest("http://localhost/media_gallery/loadinfo.php");
var myLoader:URLLoader = new URLLoader();
var myVariables:URLVariables = new URLVariables();

myRequest.method = URLRequestMethod.GET;
myRequest.data = myVariables;

function onLoaded(evt:Event):void {
//var namearray:Array = new Array();
//var valuestring:String = myLoader.data;
var src:String = myLoader.data;
var urlvars:URLVariables = new URLVariables(src);
trace(urlvars.gallerytitle);//imageone|imagetwo|imagethree|
trace(urlvars.galleryimages);//img1.jpg|img2.jpg|img3.jpg
var gallerytitles:Array = String(urlvars.gallerytitle).split("|");
var galleryimages:Array = String(urlvars.galleryimages).split("|");
trace(gallerytitles);//imageone,imagetwo,imagethree,
trace(galleryimages);//img1.jpg,img2.jpg,img3.jpg
}

amarghosh
May 5th, 2008, 04:40 AM
var myRequest:URLRequest = new URLRequest("http://localhost/media_gallery/loadinfo.php");
myRequest.method = URLRequestMethod.GET;
var myLoader:URLLoader = new URLLoader();
myLoader.addEventListener(Event.COMPLETE, onLoaded);
myLoader.load(myRequest);

function onLoaded(evt:Event):void
{
trace(evt.target.data);//this should be a valid html query string (without first '&' that u had in ur example)
var src:String = myLoader.data;
var urlvars:URLVariables = new URLVariables(src);
trace(urlvars.gallerytitle);//imageone|imagetwo|imagethree|
trace(urlvars.galleryimages);//img1.jpg|img2.jpg|img3.jpg
var gallerytitles:Array = String(urlvars.gallerytitle).split("|");
var galleryimages:Array = String(urlvars.galleryimages).split("|");
trace(gallerytitles);//imageone,imagetwo,imagethree,
trace(galleryimages);//img1.jpg,img2.jpg,img3.jpg
}

dzhedzho
May 5th, 2008, 07:36 AM
well you have 3 options,
1. Have your php script format the var so that you don't have that last"|"
2. After splitting check if the last element is empty stingand pop it if it is
3. Use .match(/[^|]+/g)); that is slower than split but pretty good, you can also add other delimiter characters after the "|"

bombdigiti
May 5th, 2008, 10:30 AM
k, amarghosh i think we are back to where we were the last time you were giving me a hand.
the out put shows me all the info from the php with the "|" but then gives me this error now:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at as3loadvars_split_fla::MainTimeline/onLoaded()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

and you thought it was something with the php, correct?
i'm just trying to figure it out, you wanted me to stop having the php output "&"s ?

Alex Lexcuk
May 5th, 2008, 12:10 PM
http://dnadillo.dn.ua/mur_gallery/php/AC3_PHP.swf
http://dnadillo.dn.ua/mur_gallery/php/GaleryScoresText.txt
http://dnadillo.dn.ua/mur_gallery/php/mur_galery_AC3_PHP.rar

bombdigiti
May 5th, 2008, 07:07 PM
i was hoping to do it timeline based... im still kinda sketchy on the packages

amarghosh
May 5th, 2008, 11:52 PM
i'm just trying to figure it out, you wanted me to stop having the php output "&"s ?

not all of them. only the first one. '&' should not be the first character in the string.
compare
"var1 is abcd and var2 is xyz" with
"and var1 is abcd and var2 is xyz"

which one makes sense?

so is the case with
"var1=abcd&var2=xyz" and
"&var1=abcd&var2=xyz"

so change ur php so that there won't be an & before the first variable; use an if or something;

bombdigiti
May 6th, 2008, 12:52 AM
nice, this is getting me somewhere... thanks a ton amarghosh
i am going to see if i can get the rest of the information into strings...

amarghosh
May 6th, 2008, 04:41 AM
i added some code to handle the first '&'.
i think now u may not have to change php

//try this and let me know how it's working;
var src:String = "&gallerytitle=imageone|imagetwo|imagethree|&galleryimages=img1.jpg|img2.jpg|img3.jpg";
var urlvars:URLVariables = new URLVariables();
var amp:int = src.indexOf('&');
if(amp != 0)
urlvars.decode(src);
else
while(amp != -1)
{
var eq:int = src.indexOf('=', amp);
var name:String = src.substring(amp + 1, eq);
amp = src.indexOf('&', eq)
var value:String = src.substring(eq + 1, (amp == -1 ? src.length : amp));
urlvars[name] = value;
}
trace(urlvars.gallerytitle);
trace(urlvars.galleryimages);
var titles:Array = String(urlvars.gallerytitle).split("|");
var images:Array = String(urlvars.galleryimages).split("|");
trace(titles);
trace(images);