PDA

View Full Version : MySql, loop and Flash



RubenFlash
June 14th, 2007, 06:17 PM
Hi! :beer:

I was wondering whats the best approach to do a query to a mysql database, and getting all the results from a table into Flash. I was thinking something like this:



var a:Number = 1;
var total:Number = 3;
var loads = new LoadVars();
var sends = new LoadVars();

sends.sendAndLoad('go.php?rand='+getTimer()+random (9999), loads, 'POST');

loads.onLoad = function(Success:Boolean){
if(Success){
//attaches some Movieclip
something = this.variable;
if(a<total){
a++;
sends.sendAndLoad('go.php?rand='+getTimer()+random (9999), loads, 'POST');
}
}
}
The go.php does a query to the database. So this does a sort of a loop, and, each time it gets the variables from the go.php, it sends again the request to another query. But I'm worried about the efficiency of this approach, because it does a query for every movie I want to attach.
So, is it better to do a single query in php and get all the results to an array and then send it to Flash? But how can I do that? I think that flash doesnt "reads" array sent from php, or does it?

Anyway, any help you could provide, I would be grateful. Thanks!

nickcherryjiggz
June 14th, 2007, 06:57 PM
I think it would be much more efficient to do one query and get all the information you need in one shot. While I don't know how to pass arrays back and forth between Flash/PHP (might be possible, dunno?), you could just turn the array into a string, send it, then convert it back to array form, something like:

PHP

//I'm rusty, so this code might not work, but the idea is fine
$flashString = "";
for ($i = 0; $i < sizeof($myArray); $i++){
$flashString .= $myArray[$i] . "!";
}
echo "flashString=" . substr($flashString,0,-1);
Flash

myArray = dataReceiverLoad.flashString.split("!");

borrob
June 15th, 2007, 08:10 AM
It's not possible to send arrays from php to flash.
And what nickcherryjiggz said would indeed be the better way to handle this.
Only one sqlquery and only one sendandload
should be a lot quicker.

But for the result:
You can just define the variables for flash so you could do
say we have a table client with fields id and name:
// exec some sql ( you'll have to define this yourself )
$returnstring = "";
$row = mysql_fetch_object( $result );
while( $row )
{
$returnstring .= "&id_" . $row->id . "=" $row->id;
$returnstring .= "&name_" . $row->id . "=" . $row->name ;
$row = mysql_fetch_object( $result );
}
// get the number of rows ( you'll have to define this yourself )
$returnstring .= "&client_row_num=" . $num_rows;
echo $returnstring;
this i found makes accessing the results in flash realy easy...

RubenFlash
June 15th, 2007, 11:40 AM
It's not possible to send arrays from php to flash.
And what nickcherryjiggz said would indeed be the better way to handle this.
Only one sqlquery and only one sendandload
should be a lot quicker.

But for the result:
You can just define the variables for flash so you could do
say we have a table client with fields id and name:
// exec some sql ( you'll have to define this yourself )
$returnstring = "";
$row = mysql_fetch_object( $result );
while( $row )
{
$returnstring .= "&id_" . $row->id . "=" $row->id;
$returnstring .= "&name_" . $row->id . "=" . $row->name ;
$row = mysql_fetch_object( $result );
}
// get the number of rows ( you'll have to define this yourself )
$returnstring .= "&client_row_num=" . $num_rows;
echo $returnstring;
this i found makes accessing the results in flash realy easy...

Thanks nickcherryjiggz (http://kirupa.com/forum/member.php?u=43239) and borrob for the answers. I found out that its impossible to send arrays from php to flash, as you said, borrob. So my solution is actually very similar to yours (I think?). Something like this:



<?php

$i = 1;
$query = sprintf("SELECT * FROM table");
$result = mysql_query($query);

while($row = mysql_fetch_array($result))
{
$id[$i] = $row['id'];
$name[$i] = $row['name'];
echo "&name".$i."=".$name[$i];
echo "&id".$i."=".$id[$i];
$i++;
}

echo "&i=".$i;

?>
It echos out the array in variables, something like name1, id1, name2, id2. Then, in Flash, I cycle through the object that receives the variables, and at the same time, I attach the movie correspondent to that iteraction.

But anyways, thanks again for the help, you guys! :beer: