04-25-2004, 08:31 PM
|
#1
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
How to send PHP data to Flash?
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 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:
PHP Code:
loadVariables("http://localhost/script.php", "this", "GET");
I test in flash and nothing.....I know I'm missing stuff....can any1 help?
Last edited by CyanBlue; 04-26-2004 at 08:54 AM.
|
|
|
04-25-2004, 09:45 PM
|
#2
|
|
Olympic Dad
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
|
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
Code:
//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.
|
|
|
04-25-2004, 10:04 PM
|
#3
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
OK...I tried your code.....I get the 'data loading' part....yipeee!!!! At least I'm a bit closer. As for your question #2
Quote:
|
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
|
|
|
04-25-2004, 11:41 PM
|
#4
|
|
Olympic Dad
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
|
Hey rubbersoul,
nevermind question number two.
Give this a try, it should get your code working:
(within the onLoad function):
Code:
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.
|
|
|
04-26-2004, 06:47 AM
|
#5
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
Nope....not working. Let me show you what I used
PHP Code:
//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!
Last edited by CyanBlue; 10-11-2004 at 03:43 PM.
Reason: PHP tag is applied
|
|
|
04-26-2004, 07:06 AM
|
#6
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
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...
|
|
|
04-26-2004, 08:34 AM
|
#7
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
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.
|
|
|
04-26-2004, 07:56 AM
|
#8
|
|
Olympic Dad
Join Date: Apr 2004
Location: Greenpoint, Brooklyn, NY
Posts: 113
|
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
|
|
|
04-26-2004, 08:36 AM
|
#9
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
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!
|
|
|
04-26-2004, 06:20 PM
|
#10
|
|
Registered User
Join Date: Apr 2004
Posts: 28
|
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 Code:
<?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.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Hybrid Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 05:10 AM.
///
|
|