Hey everyone, posted in actionscript forum but got no reply so here goes.
Im making a flash map system, where users can log in and move a Movie Clip around the map. After they have moved,they wclick save and it updates.
I've tried using an old tutorial using php and mySQL , but it didnt work.
Im wondering if someone can help me out.
ps. using the tutorial http://underheavyfire.110mb.com/Ex1_SaveMovie.swf
as you can see, it doesnt save or let me create a name
SilverVenom
01-25-2007, 05:07 AM
Hey, it's your lucky day. :)
<?php
/*
Peter Hanneman, 2007
*/
$host = "localhost";
$user = "";
$password = "";
$database = "";
$connection = mysql_connect($host, $user, $password) or die(mysql_error());
@mysql_select_db($database, $connection) or die(mysql_error());
$query = "SELECT * FROM map WHERE user LIKE '$user'";
if ($mode == "setup") {
mysql_query("DROP TABLE map");
mysql_query("CREATE TABLE map(
user TEXT NOT NULL,
xPos TEXT NOT NULL,
yPos TEXT NOT NULL") or die(mysql_error());
} elseif ($mode == "get") {
echo '<?xml version="1.0" encoding="ISO-8859-1"?> <positions> ';
$result = mysql_query($query, $connection) or die(mysql_error());
$info = mysql_fetch_assoc($result);
echo '<positon xPos="'.$info['xPos'].'" yPos="'.$info['yPos'].'" />';
echo '</positions>';
} elseif ($mode == "put") {
$result = mysql_query($query, $connection) or die(mysql_error());
$info = mysql_fetch_assoc($result);
if ($info != "") {
mysql_query("UPDATE map SET user = '$user', xPos = '$xPos', yPos = '$yPos' WHERE user = '$user'");
} else {
mysql_query("INSERT INTO map (user, xPos, yPos) VALUES('$user', '$xPos', '$yPos')");
}
} else {
echo("No paramter was specified. Try using: get, put or setup.");
}
mysql_close();
?>
Copy that code into a file and name it "script.php". Be sure to fill in your database connection setting which are specified at the top of that script, then upload it to the same directory as your flash movie will be. There is no need to make any tables, the script will do that automatically. It just needs a database and a user which has permissions to that database to work. Then use the fallowing actionscript to communicate with the script:
var xml:XML = new XML();
var xPos:Array = new Array();
var yPos:Array = new Array();
var put:LoadVars = new LoadVars();
xml.ignoreWhite = true;
xml.onLoad = function() {
var nodes:Array = this.firstChild.childNodes;
for(var i=0;i<nodes.length;i++) {
xPos.push(nodes[i].attributes.xPos);
yPos.push(nodes[i].attributes.yPos);
}
}
var user = "";
xml.load("script.php?mode=get&user="+user);
myMC._x = xPos[0];
myMC._y = yPos[0];
submit = function () {
put.send("script.php?mode=put&user="+user+"&xPos="+myMC._x+"&yPos="+myMC._y);
}
You need to specify a user in the above code. A user always has to be specified.
Now as soon as you have uploaded the script, run it on the website once with the parameter:
/script.php?mode=setup
That will cause the script to clear, create and populate a table called "map" in your mySQL database. BE WARNED! Running this causes anything that was previously in the table called "map" to be lost. You can use this function to reset it if you ever need to.
I haven't tested any of this code, I just wrote it of the top of my head, so double check for errors. I'm sure you can figure out the rest. Cheers, - Peter.
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.