PDA

View Full Version : How to send PHP data to Flash?


rubbersoul
04-25-2004, 08:31 PM
OK....here's what I'm trying to do. I want to basically click a button and it populates on dynamic text field showing all the fiels in my one table called players (it's for a hockey league). So I have a database called 'stats' a table in there called 'players' and a bunch of fields (i.e. name, goals etc)...I wrote a .php page with the following code
<?php

//Set up constants
define ('MYSQL_HOST', 'localhost');
define ('MYSQL_USER', 'myName');
define ('MYSQL_PASS', 'myPassword');
define ('MYSQL_DB', 'stats');
//If we fail to connect
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
die('Failed to connect to host "' . MYSQL_HOST . '".');
}
else
{
echo 'Connected to mysql server ' . MYSQL_HOST . ' as user ' . MYSQL_USER . ' ';
}
//Tell mysql which database to use
mysql_select_db(MYSQL_DB);
echo 'Database ' . MYSQL_DB . ' selected for use.';

//Select entire table
$result = mysql_query('SELECT * FROM players');

echo $result;

?>

** As a side note when I view this page in a browser I get 'Connected to mysql server localhost as user myName
Database stats selected for use.'' So that part looks cool.....

Now for flash....here's where I get stuck....I have a new flash file open with 2 layers. On the first layer I have my dnamic text box and on the second an actions layer. I give the dynamic text box a variable and instance name of 'content' and on the actions layer I assign the following:
loadVariables("http://localhost/script.php", "this", "GET");

I test in flash and nothing.....I know I'm missing stuff....can any1 help?

killGerald.com
04-25-2004, 09:45 PM
A couple things that might help

1) "content" is reserved by flash (it's used in the Loader class [for the Loader component]). You shouldn't use reserved keywords for var names - try $content or _content instead. And try content_txt for the textField.
2) you didn't mention if you assigned the 'content' variable to the results of the php page.
3) to read data from an external file, flash looks for &'s to tell it where to look -
you should try (within the script.php) echo "&result=".$result;
4) I'm not sure if you used the onLoad(success) function, try this if you haven't


//declare the LoadVars object:
results_lv = new LoadVars();
//inside the onLoad function we'll use with result_lv (see below), "this" refers to the
//LoadVars object, not the main timeline. Use the following code to make a
// reference to the main timeline
results_lv._Parent = this; //make sure you spell _Parent as is - _parent is reserved

reults_lv.onLoad = function(ok){
if(ok){
this._Parent.content_txt.$content = this.result;
}else{
this._Parent.content_txt.$content = "Unable to load from db";
}
};

//it's a good idea to have the load function after the onLoad - just in case the
//data comes in super-fast:
results_lv.load("http://localhost/script.php", "GET");

//give some feedback to the user as the data loads:
if(!results_lv.loaded){
content_txt.text = "data loading";
}




5) I haven't tested any of this - so it may not work as expected ;).

-Gerald.

rubbersoul
04-25-2004, 10:04 PM
OK...I tried your code.....I get the 'data loading' part....yipeee!!!! At least I'm a bit closer. As for your question #2 2) you didn't mention if you assigned the 'content' variable to the results of the php page.

I don't understand. I gave you all of my PHP above. What you see is what you get. I have nothing else....for the text box, should content_txt be used as both the instance and variable name? Still need help....thxs

killGerald.com
04-25-2004, 11:41 PM
Hey rubbersoul,

nevermind question number two.

Give this a try, it should get your code working:

(within the onLoad function):

if(ok){
this._Parent.content_txt.text = this.result;
}else{
this._Parent.content_txt.text = "Unable to load from db";
}

Lemme know if it works out for you,

Gerald.

rubbersoul
04-26-2004, 06:47 AM
Nope....not working. Let me show you what I used

//declare the LoadVars object:
results_lv = new LoadVars();
//inside the onLoad function we'll use with result_lv (see below), "this" refers to the
//LoadVars object, not the main timeline. Use the following code to make a
// reference to the main timeline
results_lv._Parent = this; //make sure you spell _Parent as is - _parent is reserved

reults_lv.onLoad = function(ok){
if(ok){
this._Parent.content_txt.text = this.result;
}else{
this._Parent.content_txt.text = "Unable to load from db";
}
};

//it's a good idea to have the load function after the onLoad - just in case the
//data comes in super-fast:
results_lv.load("http://localhost:300/script.php", "GET");

//give some feedback to the user as the data loads:
if(!results_lv.loaded){
content_txt.text = "data loading";
}

Is this right???? Still not working!

CyanBlue
04-26-2004, 07:06 AM
Howdy and Welcome... :)

You are missing the fact that Flash understands the external data in the form of variableName=value pair combined with '&' sign...

So, your first two echo lines, Connected to mysql... and Database..., will work in your web browser, but it is useless and making Flash unable to understand what it needs to do... So, comment out those two lines...

This line, echo $result;, needs to be looped through the array and create the variableName1=value1&variableName2=value2& format... :)

Lastly, please use PHP tag or CODE tag to format the script... and post your question to the right forum... I am moving this thread to the Server-side Scripting forum because it looks like your question is more toward the PHP side rather than the Flash side... :)

killGerald.com
04-26-2004, 07:56 AM
it looks good to me, but why don't you upload the fla so i can take a gander at it
(oh, and did you modify the php file with" echo "&result=".$result "?)
-gerald

rubbersoul
04-26-2004, 08:34 AM
I thank you for your input....however, it's like your speaking Chinnese to me :) Can you look at my code that I've provided and show me (the code) of what I can use to correct the problem. Thanks.

rubbersoul
04-26-2004, 08:36 AM
Yes. I did modify the PHP file. I'm at work right now so I can't do anything about uploading thee .FLA, but I will this evening. Thanks for all your help!

rubbersoul
04-26-2004, 06:20 PM
OK...I've included my sample flash .fla and included here once again my .php code. Hope someone can help.....

My PHP PAGE CALLED STAT.PHP
<?php

//Set up constants
define ('MYSQL_HOST', 'localhost');
define ('MYSQL_USER', 'root');
define ('MYSQL_PASS', '********');
define ('MYSQL_DB', 'stats');
//If we fail to connect
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
die('Failed to connect to host "' . MYSQL_HOST . '".');
}
else
{
echo 'Connected to mysql server ' . MYSQL_HOST . ' as user '
. MYSQL_USER . '<br>';
}
//Tell mysql which database to use
mysql_select_db(MYSQL_DB);
echo 'Database ' . MYSQL_DB . ' selected for use.';

//Select entire table
$result = mysql_query('SELECT * FROM players');

echo "&result=".$result;

?>

and now just take a look at my .fla and you can hopefully make some sence outta my mess. :rolleyes:

freddycodes
04-26-2004, 06:44 PM
This will never work. You cannot return a query result identifier to a flash movie through LoadVars. You need to iterate through the recordset. and create something that flash can read. Or use remoting. Or even use xml or csv. Search the forums for recordset

rubbersoul
04-26-2004, 06:48 PM
OK...now I'm totally confused.... :confused:

rubbersoul
04-26-2004, 10:46 PM
So, I've installed flash remoting......is what I want to do possible.....is it really that hard. For God's sake it's only one dynamic text box......

CyanBlue
04-26-2004, 11:20 PM
Um... None of what I have replied were applied to your PHP file... How come??? You already know I am really bad at PHP, uh??? :D

The bottom line is that you should really make that PHP script working with no error within the web browser and when that's done, that's time to use that data within the Flash and you do need to remember what I said up there in terms of getting rid of unnecessary output and having the variables in varName=value&varName=value pairs...

JerryScript
04-27-2004, 12:43 AM
I just browsed through this thread, and I think you can get this working by adding this line before your echo line, but be warned I did not test it (my db is messy right now).

$result=str_replace("\\n","&",$result);

Hope that helps!

rubbersoul
04-27-2004, 10:08 AM
Ya sorry Cyan....I have just been getting so much mixed responces that I don't know if I'm coming or going. I will try when I get home from work to do as you said and I will add the line that erry suggested also. Then (when it's still not working ;)) I will re-post the .fla and PHP for you guys to look at. Thanks for all the help.....so I geuss the whole 'Flash remoting' thing is unnecessary?

CyanBlue
04-27-2004, 10:12 AM
Flash Remoting will be another way of doing it... It will be extremely useful if you are passing really big data and if you have to create an array with that data in Flash... Flash tends to be real slow when that happens, but if you use Flash Remoting, you can just send PHP array to Flash array... Simple, right??? That's one great reason to use Flash Remoting... :)

rubbersoul
04-27-2004, 10:31 AM
Just as a side note......I just entered my URL from work and put the stats.php at the end of that URL and I get back....

Connected to mysql server localhost as user root
Database stats selected for use.

That's a step in the right direction.....no? Isn't that telling me that it did connect to mysql properly?

I think I will stay away from flash remoting right now....gotta eat this cake slowly..... :)

CyanBlue
04-27-2004, 10:32 AM
Yes... That's correct, but you will need to reformat the output from your PHP script in the manner which Flash understands... :)

rubbersoul
04-27-2004, 11:19 AM
Awesome.....I can feel it getting closer...LOL

freddycodes
04-27-2004, 11:39 AM
Yes its very simple to do this, but like CyanBlue said you need to get your result in a format that flash can read.


Connected to mysql server localhost as user root
Database stats selected for use.


This is not a format that flash can read, well I take that back it is a format that flash can read by not very easily.

Now how many fields do you want to return to the flash movie? By fields, I mean how many things do you want to return.

rubbersoul
04-27-2004, 12:09 PM
Well myslq table is set up with the following fields:
name
games
goals
assists
total
penalty
I want all the data in that whole table to be diaplayed in one dynamic text field. i.e.

name games goals assists total penalty
John Doe 5 5 10 20 3

freddycodes
04-27-2004, 01:36 PM
So will there be more than one entry in the table? Do you want it in a tabular format, formatting text like that is fairly difficult to do in a dynamic textfield?

Or something like
Name: John Doe
Games: 5
Goals: 5
Assists: 10
Total: 20
Penalty: 3
======================

Name: John Smith
Games: 3
Goals: 7
Assists: 10
Total: 20
Penalty: 5
======================

rubbersoul
04-27-2004, 01:45 PM
That would be fine....as long as I can have them ordered by Highest point person. I know I have to do this in mysql query....something like SELECT * from Players WHERE soething DESC.....but as for formating purposes.....I don't really care!

rubbersoul
04-27-2004, 07:06 PM
OK.....Cyan, I commented out what you asked and I added the line that Jerry Script mentioned to add....but everyone keeps mentioning that I have to put my php in a format that flash will read....so how do I do that? What is the code? I've include my php file again with the changes.

freddycodes
04-27-2004, 07:15 PM
As soon as I get home I can show you how, but I can't from here.

CyanBlue
04-27-2004, 08:36 PM
Um... Not sure how accurate it is going to be, but you could try this...(I am sure freddycodes will be coming up with whole lot better script for that... :))<?php
//Set up constants
define ('MYSQL_HOST', 'localhost');
define ('MYSQL_USER', 'root');
define ('MYSQL_PASS', '***');
define ('MYSQL_DB', 'stats');
//If we fail to connect
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
die('Failed to connect to host "' . MYSQL_HOST . '".');
}
mysql_select_db(MYSQL_DB);

//Select entire table
$result = mysql_query('SELECT * FROM players');
$result = str_replace("\\n","&",$result);
$num_record = mysql_num_rows($result);
$num_row = mysql_num_fields($result);

while ($row = mysql_fetch_row($result))
{
for($column_num = 0; $column_num < $num_row; $column_num++)
{
$column_name = mysql_field_name($result, $column_num);
echo "$column_name$i=$row[$column_num]";
}
echo("&");
}
?>

freddycodes
04-27-2004, 09:14 PM
No time to explain now, but here is a sample


var data = [];

lv = new LoadVars();
lv.load("http://localhost:8080/GetData.php");
lv.onData = function(raw)
{
var delim = (raw.indexOf("\r\n") > -1) ? "\r\n" : (raw.indexOf("\r") > -1) ? "\r" : "\n";
var temp = raw.split(delim);
var fields = temp.shift().split("||");
for(var i=0;i<temp.length;i++)
{
var row = temp[i].split("||");
if(row.length != fields.length) continue;
var tmpObj = {};
for(var j=0;j<row.length;j++)
{
tmpObj[fields[j]] = row[j];
}
data.push(tmpObj);
}
displayText();
}

function displayText()
{
var msg = "";
for(var i=0;i<data.length;i++)
{
for(var x in data[i])
{
msg += x + ": " + data[i][x] + "\n";
}
msg += "==========================\n";
}
tbArticles.text = msg;
}




PHP

<?php
//Set up constants
define ('MYSQL_HOST', 'localhost');
define ('MYSQL_USER', 'root');
define ('MYSQL_PASS', '');
define ('MYSQL_DB', 'venues');
//If we fail to connect
if (! mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS) )
{
die('Error"' . MYSQL_HOST . '".');
}
mysql_select_db(MYSQL_DB);

//Select entire table
$result = mysql_query('SELECT * FROM articles');
$num_row = mysql_num_fields($result);
for($j=0;$j<$num_row;$j++)
{
$field = mysql_fetch_field($result, $j);
$fields[] = $field->name;
}

//Print the fields
print implode($fields, "||")."\n";

while($row = mysql_fetch_assoc($result))
{
print implode($row, "||")."\n";
}

?>

rubbersoul
04-27-2004, 09:43 PM
Hey freddy,
Thanks for the attempt but it seems nothing is working for me...I tried your code and at first I got a syntax error....you were missing a }......then after fixing that I get nothing....no errors....no data, and I also can't see in your PHP where you did the &variable =$variable conversion.....so once again :confused:

CyanBlue
04-27-2004, 09:53 PM
Can you dump your MySQL data and post it here???

rubbersoul
04-27-2004, 11:13 PM
OK....I'm really close. I've been trying to look at tutorials and the works....I've managed to connect to my database and get the following using my url http://localhost:8080/stats.php get my stats :) :) :) Here is the result of that URL....

&n=3&name0=Rodney Garett&&games0=0&&goals0=0&&assists0=0&&total0=0&&penalty0=0&&name1=Pierre Parland&&games1=0&&goals1=0&&assists1=0&&total1=0&&penalty1=0&&name2=Stephan Ray&&games2=0&&goals2=0&&assists2=0&&total2=0&&penalty2=0&&

I'm so pleased!!!! I geuss this means my .PHP side is done and working....I've included the file! Now onto the flash part.....I'm using this code supplied to me by you guys

myVars = new LoadVars();
myVars.load("http://localhost:8080/stats.php");
myVars.onLoad = function(success)
{
trace("waiting for data....");
if (success)
{
images = this.name;
trace(images);
}
else
{
trace("no returned data");
}
};

When I run it I get the following:

waiting for data....
undefined

freddycodes
04-27-2004, 11:22 PM
I did no conversion, not sure what isn't working for you. But if you pull up the php script in a browser do you see the data?

rubbersoul
04-27-2004, 11:25 PM
No...for your code I was getting the syntax error....then when I corrected that nothing, just a blank page. With my new PHP code I am getting the data........

freddycodes
04-27-2004, 11:47 PM
OK first so you can see I am not full of sh%t
http://www.irq11.com/~louie/articles.html

Next here is the source .fla and php script.

Remember to change your db settings and query

rubbersoul
04-28-2004, 08:27 AM
Hey Freddy,
Thanks for going above and beyond man! Ya, I got your code to work with what you sent.....looks cool to. Now I only have 2 problems....
1 - My name field is always on the bottom of the list which kinda stinks....i.e.

Goals: 0
Assists: 5
Total: 5
Name: John

========

Is there an easy way to get the name as the first field displayed? And the other is a biggie that I've posted in other thread but will mention here as it's happening with this code that you gave me also....

I can test the .swf and everything looks fine (like yours)....I can throw that .swf into a html document and type in http://localhost:8080 in my browser and everything is still cool.....However, if I type in my URL (say: http://mywebpage.com) My page comes up.....I see your scroll bar...but no data :confused:

CyanBlue
04-28-2004, 08:47 AM
Hey, rubbersoul... Don't ever make freddycodes mad... :D

I think you should go back abit and play with LoadVars() object and how you can retrive and assign data to display in your liking... That has to be the first step you need to take and then you can move on to solve your problem...
(Don't know what's in your code, but it sounds like you have not embedded the font if what you have is similar to freddycodes...)

freddycodes
04-28-2004, 10:14 AM
So is your host machine set to port 8080, if not you should be using a local path to the php script and place the html file and swf and php script in the same folde ron your host. Also can you see the data if you pull the php script on your host server?


As for your other problem, just manually iterate the fields you want instead of using the loop I provided.


function displayText()
{
var msg = "";
for(var i=0;i<data.length;i++)
{
msg += "Name: " + data[i].Name + "\n";
msg += "Goals: " + data[i].Goals + "\n";
msg += "Assists: " + data[i].Assists + "\n";
msg += "Total: " + data[i].Total + "\n";

msg += "==========================\n";
}
tbArticles.text = msg;
}

rubbersoul
04-28-2004, 10:38 AM
Thanks again Freddy.....OK
1- My machine is set to port 8080
2- The .html file and .swf are in the same folder under wwroot (IIS)
3- Yes. I can see the data if I pull the PHP script

freddycodes
04-28-2004, 12:57 PM
No I mean on your host?

rubbersoul
04-28-2004, 02:07 PM
My machine is the host.....I'm using my machine and noip.com as my dynamic DNS provider......Everything is on my machine.....when I put in my URL I'm browsing the pages on my machine.

freddycodes
04-28-2004, 02:39 PM
Weird. could have something to do with the sandbox security in the flash player.

But you say when browsing the php script through the dynamic ip you see the data? But the flash movie can't get to it?

rubbersoul
04-29-2004, 09:30 PM
Cool....everything works now.....the problem was what someone had said about using a realitive path....instead of http:..localhost/fil.php.....just file.php. Thanks for all the great help guys!!!!