PDA

View Full Version : PHP variables contain junk when passed into Flash


pixilliontom
03-03-2008, 03:18 PM
Hi,

I've spent a couple of hours searching for an already existing answer for this problem on these forums but found no joy so here goes...

I'm loading some variables from PHP into Flash using loadVars. When I use a textfile (instead of php) it works fine. When I view the php page in a web browser it also works fine. The output of the PHP looks like this:

&myvar1=40&myvar2=40.34,67.23&

However when I get the php into Flash using loadvars the data is pretty messed up. All the decimal points have been replaced with '%2E'. The php file is on my local machine on a webserver and I've given flash the whole locations (ie http://33.33.33.33/blah/blah.php). I'm using a G5 Mac.

Is this an encoding issue or something? At the moment my php file is very very small...just connects to the db and echos the results to screen...nothing about encoding it in it. Anyone got any ideas? Any help would be greatly appreciated.

Cheers,

Tom

The Little Guy
03-04-2008, 02:19 AM
try:

urldecode('&myvar1=40&myvar2=40.34,67.23&');

pixilliontom
03-04-2008, 09:39 AM
Little Guy - No joy, still has junk in it, but thanks anyway. It looks fine on the php page, it's just messed up when it's in Flash...

jsebrech
03-04-2008, 12:15 PM
Post your PHP code (at least the parts where you actually output the text).

pixilliontom
03-04-2008, 12:24 PM
<?php
$connect = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $connect);
$result = mysql_query("SELECT imagefile, imagepos, imagesize, mask FROM data");
$nRows = mysql_num_rows($result);
$cant = 0;
while($row=mysql_fetch_array($result))
{
$str = "&imagefile$cant=$row[imagefile]&imagepos$cant=$row[imagepos]&imagesize$cant=$row[imagesize]&mask$cant=$row[mask]&";
urldecode($str);
echo urldecode($str);
$cant++;
}
?>




I've tried it without the urldecode and the result is the same...it looks fine when viewed in a webpage but crazy in Flash...

jsebrech
03-04-2008, 04:43 PM
First of all, the urldecode call is unnecessary.
Secondly, you should never use variables inside of a string, as this is bad practice and risks introducing bugs. Instead reformat your string to use concatenation with the dot operator:
"&imagefile".$cant."=".$row["imagefile"]."&imagepos".$cant."=".$row["imagepos"]...

I don't know whether that second thing is the cause of your problems, but it is definitely something you should fix.

The Little Guy
03-04-2008, 05:05 PM
Im not sure if this is the problem, but I thought that I read somewhere that you need to use print for actionscript to work properly... I may be wrong, but at least you could give it a try, and see if it works.

pixilliontom
03-05-2008, 09:15 AM
Thanks jsebrech - I've not really used PHP on any major projects before so it's good to know some of the best practices as it looks like I'll be using it a whole lot more soon.

Just tried using print instead of echo and still no joy within Flash...seeing as when I read from a textfile everything works fine this has to be a PHP issue. Maybe my php.ini file needs something in it about encoding?

jsebrech
03-11-2008, 09:26 AM
The default encoding flash assumes is UTF-8, so you need to be outputting UTF-8 from PHP. However, PHP5 is just a byte processor, it doesn't understand about encodings (despite the php.ini setting, which just sends an encoding header but doesn't add any intelligence encoding-wise). What you need to do is change your database settings so the database returns UTF-8 encoded text, and then forward this straight to flash.

http://actionscript.org/forums/showpost.php3?p=695722&postcount=7

PHP 6 is going to fix all this by making the string type intelligent, so you can output the encodings you need (by default this will be UTF-8, so that will work out well for the flash community).

pixilliontom
03-11-2008, 04:40 PM
I've tried not using a database at all and still no luck. If Flash is expecting UTF-8 then surely if my PHP settings were correct then the text would display fine (assuming it hasn't come from a differently encoded database). With that in mind is it JUST the PHP.ini file that sets the encoding?

jsebrech
03-12-2008, 11:14 AM
If you type character data straight into your PHP file, you need to make sure your text editor is in UTF-8 mode. Notepad can save as UTF-8 (unicode), but it adds a BOM (byte order marker) at the start of the file, which PHP doesn't like all that much. PSPad can be configured to save as UTF-8 without a BOM.

The php.ini setting has NO influence on what character set is output by PHP. It only adds a HTTP header that says which character set your PHP code is outputting. It is still your responsibility to make sure your PHP scripts output the correct character set. I'm afraid PHP doesn't play all that nice with unicode, but it is possible to work with unicode if you are careful and understand the subject.

On the topic of character sets, this is a good article: http://www.joelonsoftware.com/articles/Unicode.html

matbury
03-12-2008, 12:25 PM
If you want to pass variables into a SWF, I'd recommend using swfobject (http://blog.deconcept.com/swfobject/).

You can easily and clearly pass any number of variables with it, it handles all the Unicode and URL encoding for you, and it's also the best way to embed your SWF movies into a web page.

If you're using PHP in a dynamic site, you can write the SWFObject embed code onto the PHP page, since it's a client-side script. Don't use relative paths in PHP sites, they sometimes behave strangely!

Here's some example code to get you started. Check out the documentation on the link for info on how exactly to pass your variables into Flash.

<body>

<script type="text/javascript" src="http://yourserver.com/swfobject.js"></script>

<div id="content">
To view the Flash content, please enable JavaScript in your browser.
</div>

<script type="text/javascript">
var so = new SWFObject("http://yourserver.com/yourswffile.swf", "yourswffile", "900", "480", "9", "#ffffff");
so.addParam("wmode", "transparent");
so.addVariable("variable", "variablename");
so.write("content");
</script>

</body>

pixilliontom
03-12-2008, 01:13 PM
Thank Matbury - Yeah SWFObject is a goodun, but I can't really use it in this case. If and when this bloody PHP works I'll be getting data from a database based on an ID so I have to use PHP for this one.

For testing purposes I'm just using a PHP file that does nothing but print a variable to the screen ala:

<?php
print "&imagefile=45.345,3453.768";
?>


Eventually it will be far more complex and use a database BUT for now I just want Flash to be able to pick up that 43.34.. and not turn it into a garbled mess. I've tried creating a PHP file from scratch in a UTF-8 encoded text file as above and I'm reading the file in Flash using:


myData = new LoadVars();

myData.onLoad = function(success:Boolean)
{
if(success)
{
textbox.text = this;

}
else trace("Error loading data");
}
myData.load("http://84.9.143.46/flashUpload/select.php");



I'm using arial as the font, the characters are embedded but this is what I get in my textbox:

=45%2E345%2C3453%2E768%0A%0A&onLoad=%5Btype%20Function%5D

Am I doing something glaringly wrong here or is this abnormal? I'm starting to think there is something wrong with my setup. Either that or I'm just much dumber than you're average bear...

matbury
03-12-2008, 10:34 PM
Ok, here's an example of how you can make it work with a dynamic PHP database driven site:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<?php

// Get your database access constants

global $DB_ACCESS; // or whatever your global DB access object is
require_once('yourDBaccessFile');

// Connect to DB
mysql_connect($DB_ACCESS->hostname, $DB_ACCESS->username, $DB_ACCESS->password);
@mysql_select_db($DB_ACCESS->databasename) or die( "Unable to select database");

// create and send query to DB
$sql = 'SELECT... etc.'; // Obviously, create your query to get the results you want
$result = mysql_query($sql);

// Get the variables from the query result
// This example assumes that your query returned 2 rows
// You can adjust this as you like. If you're passing a lot of variables, consider
// using an array and a loop
$variable = mysql_result($result,0,'variablename');
$othervariable = mysql_result($result,1,'othervariablename');

// Create SWFObject embed code string to print on the user's page
$swfobject = '</head><body><script type="text/javascript" src="http://yourserver.com/swfobject.js"></script>
<div id="content">
To view the Flash content, please enable JavaScript in your browser.
</div>
<script type="text/javascript">
var so = new SWFObject("http://yourserver.com/yourswffile.swf", "yourswffile", "900", "480", "9", "#ffffff");
so.addParam("wmode", "transparent");
so.addVariable("'.$variable.'", "variablename");
so.addVariable("'.$othervariable.'", "othervariablename");
so.write("content");
</script>';

// print it
echo $swfobject;

</body>
</html>

pixilliontom
03-13-2008, 10:55 AM
You sir, are a genius. I have no idea why the other method (which most tutorials on the internet say to use) doesn't work, but this works like a charm. A thousand thank yous.

jsebrech
03-13-2008, 11:38 AM
Your problem has nothing to do with unicode after all, it is this line:
textbox.text = this;

You are assigning an object to a string, so flash does a "toString" on the object, which gives you a code representation. I think instead it should be:
textbox.text = this.imagefile;

matbury
03-13-2008, 04:16 PM
You're welcome pixilliontom. Glad I could help.

PHP can write any client-side language to the browser so it's well worth bearing in mind.

The other ways of doing this kind of thing are well worth knowing too. SWFObject isn't always appropriate to use - in some rare cases users might have Javascript turned off or blocked in their browsers, and some users don't even have it installed! I've had a few cases of this happening with clients' users.

I'd recommend perservering with your original method and making it work too. Looks like jsebrech has found the solution for you.

Good luck!