View Full Version : Help with php, mysql and flash
sunny747
07-06-2007, 09:39 AM
Hello All,
i really need a support here. I would like to make a "shopping mall floor plan" with flash. I have a working database(mysql) which is integrated with PHP. IF the shop is ready for rent, it shows "vacant" otherwise "occupied" on the jpg image.
Now, i want to connect mysql db with flash so that the image will turn into gray(or something) if the shop is "occupied". Or if it is vacant, it will show different color.
can any of you support me with work arounds or tutorials? i would really appreciate......i dont even know how to start..
thanks so much
regards,
sunny
Scuba_Steve
07-06-2007, 04:54 PM
read my post in this same forum - i've got all the AS and PHP written to show you how to do exactly what you want. you need to use the loadvars/sendAndLoad to send a piece of info from your flash app to a php script and then back again. it's really simple and easy to use, no worries.
you basically, say, have a variable that needs to go to php for some sort of processing (a value you need to pass to a function in php for example).
you'd have:
var apples:Number = new Number;
apples = 99;
you'd create two LoadVars objects - one stores the data you wanna send to php, and the other sits empty until the php sends a value(s) back. those values sent back are stored in the 2nd LoadVars object.
var pitcher_lv:LoadVars = new LoadVars();
var catcher_lv:LoadVars = new LoadVars();
then you take whatever value you wanna send to your php and stick a "copy" of it inside your pitcher loadVars object. Note: if you were to trace(pitcher_lv.apples), it would output "99".
pitcher_lv.apples = apples;
now you're ready to fire that info off to your php script using the sendAndLoad method. whereas just using send() would also work, sendAndLoad() means you're sending data and expecting somethign to come back. sendAndLoad() is written as:
sendAndLoad("URL to where we're sending the data", loadvars-object-to-receive-any-data-sent-back, "POST");
so for this example, it would look like:
pitcher_lv.sendAndLoad("http://www.yourdomain.com/your/script.php", catcher_lv, "POST");
now that data goes off to your php script... for our example, our php is just going to take the data flash sends (apples, or more specifically 99) and add it to some predefined data in the php file:
$apples = $_POST['apples']; // this is how we use the data that is
// sent to this php script using sendAndLoad. php receives the data
// as $_POST['pitcher_lv.yourvariablename'], so the first thing we do
// is create a var in php and set it equal to the data we sent to this
// script. if you could "trace" in php, you'd see that trace($apples)
// would output "99".
$oranges = 1; // we're going to add our apples and a new var
// that exists in the php script - "$oranges".
$total = $apples + $oranges; // simple php here adds the two
// fruits and sets the answer as "$total";
echo "&total=" . $total; // here's how we get teh data that php
// calculates (whether its simple math or fetching data from a
// database) and send it back to flash. the easiest way is using
// "name/value pairs" which you've undoubtedly seen before. it's
// when a url contains something like "blah.php?name1=bob&name2
// =fred&name3=george". that's how flash likes to get data, so
// we'll tell our php to output "$total" using that pattern.
// echoing "&total=" . $total just outputs "&total=100" (since 100
// = 99 apples plus 1 orange).
?>
and that's that. now just like when you're loading xml or whatnot, sometimes the data isn't available right away. for that, we use an onLoad function. just like when using xml, when a LoadVars object finishes loading, the "onLoad" function is called automatically.
how do you get your paws on that $total variable now? well, same way you set "pitcher_lv.apples = apples". we're going to tell our onLoad function to trace(catcher_lv.total). any variables you sent back to flash from the php are loaded into your "catcher_lv" LoadVars object. to access them, just use catcher_lv.variablename. piece of cake. :)
catcher_lv.onLoad = function(success:Boolean){
if (success){
trace("You have " + this.total + " apples and oranges.");
};
};
note that the above function is called on the catcher_lv LoadVars object itself, so instead of tracing "catcher_lv.total", we would say "this.total".
Running the Flash app should now output "You have 100 apples and oranges."
You could also have your php print multiple variables by changing the echo statement to:
echo "&total=" . $total . "&oranges=" . $oranges;
then you could do this in Flash:
trace ("I have " + apples + " apples and " + this.oranges + " oranges, for a total of " + this.total + " pieces of fruit.");
Simple as pie. ;)
Scuba_Steve
07-06-2007, 04:57 PM
setting your shop status could be as easy as fetching the "vacant" or "occupied" data from your db and sending the data back to flash as:
&store1=occupied&store2=vacant&store3=vacant;
then depending on in flash what this.store1 is, you can have a function that says:
catcher_lv.onLoad = function (success:Boolean){
if (this.store1 == "vacant"){
// draw a green box at certain x/y coordinates
} else {
// draw a red box at certain x/y coordinates
};
};
sunny747
07-07-2007, 06:36 AM
thanksss sooo mucchh steve......i still haven't tried it yet but i really appreciate you support........no words here how to thank you...
i may ask you more questions once i try your codes.............
have a good weekend...
Scuba_Steve
07-09-2007, 01:19 PM
no sweat. i post on here now and then with questions but most of them go unanswered (i think due sheerly to the amount of posts this subforum gets, stuff get's pushed back quickly), so when i see somethign i know the answer to, i try to provide a clear step by step answer. :)
sunny747
07-11-2007, 12:20 PM
Dear steve, i need some idea. I successfully implemented the store layout with your great support. I haven't had any problem but i'm facing new prob here. Whenever i update the data from my DB it doesn't update on flash from the IE on webserver. Coz IE and flash reloads from the cache and refreshing doesn't seems working also.
Any idea? Do you think i should put a "update" button on flash? ...but how?
thansk again.
LOLFlash
07-11-2007, 01:37 PM
Put anything after url let IE think you sending variables to server:
.sendAndLoad("http://www.yourdomain.com/your/script.php?anything=whatever", catcher_lv, "POST");
Scuba_Steve
07-11-2007, 01:40 PM
LOLFlash,
What exactly does that accomplish?
LOLFlash
07-11-2007, 01:47 PM
Much depends what you loading, but what I know IE doesn’t cash if you sending variables to server
Scuba_Steve
07-11-2007, 02:14 PM
so Flash uses IE to connect to your php scripts? i thought it was independent.
sunny747
07-11-2007, 07:45 PM
well, it's not independent.........i'm using IE.But it is based on intranet.
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.