PDA

View Full Version : PHP/MySQL vars to flash for movieclip placement


CVO
01-29-2008, 01:53 PM
Hello again.

I'm not sure how to pull variables from a database back into flash. I've looked it up and it seems I need to use an array but I'm not sure on the syntax etc.

First I need to pull in a variable from flash called "from". "from" is an int variable. PHP then pulls all the values from the database where the "id" is between the variable "from" and "from"+9999. ie: if from is 120001 then I want records 120001 to 130000 selected. I then want to filter these records so that I only have the ones where another field called "taken" is equal to 1.

I have gotten this far with the php<?php

//this pulls the variables from the flash movie when the user hits submit.
$from = $_POST ['from'];

...log in details removed...

//select squares from database which have already been taken by other users
$sql = "SELECT * FROM `takensquares` WHERE `id` BETWEEN $from AND ($from + 9999) AND taken = 1";
$result = mysql_query($sql) or die(mysql_error());
?> First; is the sql query above worded correctly? I'm not sure about the ($from + 9999) bit.

Finally, how do I go about getting these values sent back in to flash? Is it just a case of sending $result back in to flash and do I have to put $result = $POST ['result']; in the php file?

As you can tell I'm no PHP coder! Any help much appreciated :)

CVO
01-29-2008, 08:42 PM
Okay half a day later and (I think) I finally got it!
<?php

//this pulls the variables from the flash movie when the user
//hits submit.
$from = $_POST ['from'];
$to = $POST ['to'];

//connect to the database
$host = "";
$user = "";
$pass = "";
$database = "";

$db_connect = mysql_connect($host, $user, $pass) or die (mysql_error());
$db_select = mysql_select_db($database, $db_connect) or die (mysql_error());

$query = "SELECT * FROM takensquares WHERE id BETWEEN '$from' AND '$to' AND taken = 1";
$result = mysql_query($query) or die(mysql_error());

// count rows of array
$num = mysql_numrows($result);

// loop through results of array and print to screen
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
echo $id,",";
$i++;
}
?>
This outputs the results in the following way: 1,25,198,255,

First of all; is this the way to do it? (comma separted values)

Secondly, if it is the correct way then how do I place movieclips based on the output of the php file. I have the vars going in to flash but any idea on how to make flash make a copy of a movieclip (called takensquares) and have it placed on the stage according to the results?
ie: if php outputs 1,25,198,255, to flash then 4 movieclips would be created and placed at x=1, x=25 etc

CVO
01-30-2008, 01:08 AM
Ok I found out that I need to get the php array to a flash array. Thanks to a great tutorial from kirupa.com (http://www.kirupa.com/developer/mx2004/external_array.htm) I think I have it (albeit 4 1/2 hours later).

Now all I need to do is have the movieclip called "takensquare" placed on stage for each value in the array "mc". So if the array "mc" has 2 values (say 3 and 65) then I want 2 copies of the movieclip "takensquare" placed on the stage (at x=3 and x=65. y can be anything).

Anyone any ideas? Ta.