Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 09-10-2004, 07:09 PM   #1
arctictiger
Registered User
 
Join Date: Sep 2004
Posts: 5
Exclamation Passing variables from PHP to ActionScript

Ok I'm new to PHP but I have relative experience with ActionScript.

I've been making a game which at the end sends variables to a PHP file and the PHP file sends the variables to a MySQL database, I have this working perfectly fine and dandy.

However I require the game to display the top 10 scores at the start of the game, I can get the top 10 fine using PHP on its own but when it comes to sending the top10 (or any variable for that matter) to ActionScript I can't seem to get it to work.

I have searched in vein to find tutorials to help me out and I have searched this forum and still I'm no more knowledged than 3 days ago.

Please could someone post a simple PHP script that will pass a single variable to ActionScript and also the ActionScript needed to recieve that variable so I can see how it should be done.

Thanks
arctictiger is offline   Reply With Quote
Old 09-10-2004, 07:48 PM   #2
Twister
Registered User
 
Join Date: Feb 2002
Location: Minneapolis
Posts: 77
Send a message via AIM to Twister Send a message via Yahoo to Twister
Default

Ok you need to make a page for flash to pull in the variable. on this page you'd output your variable like this &name=thomas The source code for that page would be something like echo ("&name=thomas");

Then you use loadVariableNum to load that page. loadVariableNum("variablepage.php",0)

Two things to keep in mind
1) it will load variables into the root level of your file, most likely
2) I wrote that off the top of my head so make sure to check the code.
Twister is offline   Reply With Quote
Old 09-10-2004, 07:54 PM   #3
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Quick tutorial can be found here...

http://tutorials.flashvacuum.com/art...0a44705643e5d5

I'd probably use LoadVars object unless you are exporting your movie for F5...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 09-11-2004, 01:10 AM   #4
arctictiger
Registered User
 
Join Date: Sep 2004
Posts: 5
Default

Ok, this is what I have going on as a test... (test php file is topten.php)

PHP Code:
<?php
    $testVar 
"Success";
    
    echo(
"Topname=$testVar"); 
?>
then in the ActionScript I have this...

Code:
loadVariablesNum("topten.php", 0);
and my Dynamic Text box is assigned the variable _root.Topname

so this should display "Success" in my text box, is this right?
arctictiger is offline   Reply With Quote
Old 09-11-2004, 01:46 AM   #5
arctictiger
Registered User
 
Join Date: Sep 2004
Posts: 5
Default

Ok, now I have tried a different ActionScript method using the same PHP as follows... (The dynamic textbox intance name is "top_text")

Code:
var top_list = new LoadVars();
top_list.onLoad = function(success){
	if (success){
		top_text.text = top_list.Topname;
	}
}
top_list.load("topten.php");
And this doens't work either! Help Please
arctictiger is offline   Reply With Quote
Old 09-11-2004, 07:30 PM   #6
ibizconsultants
Registered User
 
Join Date: Aug 2004
Posts: 24
Default

This should help

PHP Code

<?
$first="this";
$second = "that";

echo "myfirst=$first&mysecond=$second";
exit;
?>


Flash Actionscript:

var lvSend = new LoadVars();
var lvReceive = new LoadVars();
lvSend.SendAndLoad("www.domain.com/script.php",lvReceive,"POST");

lvReceive.onLoad = function(bSuccess) {
if(bSuccess == true) {
trace(this.myfirst);
trace(this.mysecond);
}
}

Hope this helps

www.ibizconsultants.com
ibizconsultants is offline   Reply With Quote
Old 09-12-2004, 04:07 AM   #7
Dark_Element
Lolen
 
Dark_Element's Avatar
 
Join Date: Aug 2004
Location: Australia->Gold Coast
Posts: 413
Default If

Hi ya. I've made something like this for some1 recently. This piece of php code will deffinitely help you:

PHP Code:
<?php
//just replace the 'host', 'user', 'pass', and 'db' with stuff you have
$connect = @mysql_connect('host','user','pass');
$dbselect = @mysql_select_db('db',$connect);
$query = @mysql_query('SELECT player, score FROM scores ORDER BY score DESC LIMIT 10');
if (!
$connect or !$dbselect or !$query) {
  exit(
'failed=true');
}
$num 1;
$rows mysql_num_rows($query);
while (
$info mysql_fetch_assoc($query)) {
  
$key array_keys($info);
  echo(
$key[0].''.$num.'='.$info['player'].''.($num <= $rows '&' '').''.$key[1].''.$num.'='.$info['score'].($num $rows '&' ''));
  
$num++;
}
mysql_free_result($query);
mysql_close($connect);
?>
PS: that code assumes you have a table in your database called "scores" and in it there should be columns "player" and "scores".

note: it will return variables in this layout:
"player"a increasing number"="name of player"&score"a increasing value equivlent to the one before"="score value

so: if there is a table in MySQL like this:
Code:
 
+-------+------+
|player | score |
+-------+------+
| foo   | 100  |
+-------+------+
| bar   | 250  |
+-------+------+
| var   | 90   |
+-------+------+
this will be returned by the PHP code:
player1=bar&score1=250&player2=foo&score2=100&play er3=var&score3=90

Hope that helps

J.W_(Dark_Element)

<C-Stamp> if you need more help you are welcomed to PM me </C-Stamp>

Last edited by Dark_Element; 09-12-2004 at 04:11 AM. Reason: Makin it look better
Dark_Element is offline   Reply With Quote
Old 09-12-2004, 01:00 PM   #8
arctictiger
Registered User
 
Join Date: Sep 2004
Posts: 5
Default

Sniper I aren't having any trouble with MySQL but thatnks anyway.

ibizconsultants I have tried your method and this works fine for the second variable only, it seems for some reason I can only recieve the second variable. bloody odd.
arctictiger is offline   Reply With Quote
Old 09-12-2004, 01:02 PM   #9
arctictiger
Registered User
 
Join Date: Sep 2004
Posts: 5
Default

D'oh! it just needed an & in front. Thanks guys problem solved
arctictiger is offline   Reply With Quote
Old 09-12-2008, 04:50 PM   #10
davej
Senior Member
 
Join Date: Dec 2005
Posts: 263
Default

if you start passing larger and more coplex data you may thing about having you php out put it as a xml doc. then in flash you load the xml. It is much easier for complex data then the above method.
davej is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:20 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.