View Full Version : Passing variables from PHP to ActionScript
arctictiger
09-10-2004, 07:09 PM
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 :D
Twister
09-10-2004, 07:48 PM
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. :D
CyanBlue
09-10-2004, 07:54 PM
Quick tutorial can be found here... :)
http://tutorials.flashvacuum.com/article.php?article=1&PHPSESSID=32fbfe0daeec4289c00a44705643e5d5
I'd probably use LoadVars object unless you are exporting your movie for F5...
arctictiger
09-11-2004, 01:10 AM
Ok, this is what I have going on as a test... (test php file is topten.php)
<?php
$testVar = "Success";
echo("Topname=$testVar");
?>
then in the ActionScript I have this...
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
09-11-2004, 01:46 AM
Ok, now I have tried a different ActionScript method using the same PHP as follows... (The dynamic textbox intance name is "top_text")
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 :confused:
ibizconsultants
09-11-2004, 07:30 PM
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 (http://www.ibizconsultants.com)
Dark_Element
09-12-2004, 04:07 AM
Hi ya. I've made something like this for some1 recently. This piece of php code will deffinitely help you:
<?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:
+-------+------+
|player | score |
+-------+------+
| foo | 100 |
+-------+------+
| bar | 250 |
+-------+------+
| var | 90 |
+-------+------+
this will be returned by the PHP code:
player1=bar&score1=250&player2=foo&score2=100&player3=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>
arctictiger
09-12-2004, 01:00 PM
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
09-12-2004, 01:02 PM
D'oh! it just needed an & in front. Thanks guys problem solved :)
davej
09-12-2008, 04:50 PM
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.
acolyte
09-13-2008, 06:30 PM
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.
So much much easier it is with maybee swx or remoteObjects you can transfer your associated array out of the box !!
try google for swxFormat or amfPhpRemoting or Red5
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.