-
Load variables into Array
I am using LoadVars.sendAndLoad() to load PHP generated response from database into Flash.
The problem is that I am loading several Rows from MySQL.
Each row represents one folder.
Each row has three fields:
title
folderx
foldery
My output from PHP looks like this:
title=CXZXc&folderx=33&foldery=55&title=DNA&folder x=200&foldery=300&title=Cars&folderx=400&foldery=2 00&
So when using sendAndLoad(), I need to load this long string of variables BACK into Flash but have them into an array so I each Array element can have
title, folderx, and foldery
So something like this
FOLDERS= new Array(new Array("title-1","folderx-1" "foldery-1"), new Array("title-2","folderx-2" "foldery-2"));
something like that. I don't care how the Flash array looks, just so that I can access each 'FOLDER' collection separately.
I feel like I'm almost there..... I know I can use Array.push( ) to push variables into an array.... and perhaps I need to build a delimiter into PHP to designate when we are on a new "row". But I need a push in the right direction. thanks.
-
Maybe something like this:
PHP Code:
var urlvars:String = String("title=CXZXc&folderx=33&foldery=55&title=DNA&folder x=200&foldery=300&title=Cars&folderx=400&foldery=2 00");
toArray = function(vars) { var temp:Array = new Array(); vars = vars.split(' ').join(''); vars = vars.split('&'); for(var i = 0; i < vars.length; i++) { temp.push({variable:vars[i].split('=')[0],value:vars[i].split('=')[1]}); } return temp; }
for(var j = 0; j < toArray(urlvars).length; j++) { trace(toArray(urlvars)[j]['variable'] + ' = ' + toArray(urlvars)[j]['value']); }
Returns:
title = CXZXc
folderx = 33
foldery = 55
title = DNA
folderx = 200
foldery = 300
title = Cars
folderx = 400
foldery = 200
-
Thanks, sstalder, for such a detailed response and code.
The string does not come into Flash like that, though.....how can I get the string to come into flash like that? With sendAndLoad, Flash is reading those variables in one by one, of course, discarding the ampersand symbols.
-
I suppose I could loop through all the loaded variables and then concatenate whichever symbols in front and back of each one and then put them in one long string, to approximate the way they look in PHP.
Thanks again for the code, by the way. Its simple, fast, and elegant.
SO... I AM ALMOST THERE.....how do I loop through my loadVars Object to get all of the loaded variables in it? I tried myLoadVarsObject.toString(); and this gave a messy and unexpected result.
Last edited by nmuta; 08-07-2008 at 02:22 PM.
-Nmuta
-
So how do I loop through my loadVars Object to get all of the loaded variables in it from my PHP file? This is the one key I need.....I've got it from there.
-
Can you show me the code you currently have, maybe I can help adjust that to work?
-
Bearded (M|G)od
Your problem is how you are echoing from php. If you echo out a title variable twice, it's going to get overwritten.
You need to increment a number along with the variable names if you're going to do it that way. Such as: title0, title1, title2, etc.
PHP Code:
echo 'title0=something&title1=another&title2=another+yet&total=3';
Code:
var lv:LoadVars = new LoadVars();
lv.onLoad = buildArray;
lv.sendAndLoad("myTestScript.php", lv);
function buildArray($success:Boolean):Void
{
if(!$success) return;
var arr:Array = new Array();
for(var i:Number = 0; i < parseInt(lv.total); i++)
{
var temp:Array = new Array();
temp.push(lv["title"+i]);
arr.push(temp);
}
}
Something along those lines. Take note of the total variable being sent as well. The best way to be handling this though is sending back XML.
-
my PHP part
I thought of that same thing.... adding a number to the end of each "Row" item. I just need to add to my code a number to attach on to the end of each variable as I'm looping through my database Rows, to indicate in php which row I'm currently on.
here is my php code.
You can see that it takes my variables from flash and then does a database query based on what it's getting from Flash. It's flexible, and it works well:
PHP Code:
function getdata($theid,$tablename,$thearray,$link) {
$query = "select * from $tablename where id='$theid' "; $result = mysql_query($query, $link) or die('Cannot run this query: '.mysql_error(). '<br>Please contact the <a href="mailto:nmuta@newmediatech.net"> webmaster </a>');
while($Row = mysql_fetch_assoc ($result)) {
//[B]build the string that is needed. [/B] $newarrai = explode(",",$thearray); $output= '&'; for ($i=0;$i< (count($newarrai))+1; $i++){ $zz = $newarrai[$i];
$output.= $zz.'='.$Row[$zz]."&"; }
}//end while
}//end function get data
echo $output;
-
Last edited by wopsreg; 06-25-2010 at 01:46 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|