PDA

View Full Version : RE. PHP and Flash the introduction... small question (I think)



unable_to_load
November 26th, 2004, 05:54 PM
You can "echo" any message back to Flash and display it in a dynamic textfield to let the user know the message was sent, just add "&status=" in front of your text,
as in "&status=Your message has been sent" :
LoadVars.onLoad =>assign returned var to the status_txt.text property by setting
status_txt.text=this.status; (where "this" refers to the LoadVars object you're sending the PHP result to, either the same one you used to send your variables, or a separate resultVars = new LoadVars).
Read the above mentioned post and it cleared many things up for me. I'm new to the whole flash scene but have been working with PHP extensively for the last few months.

I still don't understand how to get the php vars to Flash dynamic text. Here is a script I'm trying to display the information from!


<?
include("../conf.php");
include("../functions.php");
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
// generate and execute query
$query = "SELECT id, slug AS title, content, timestamp FROM services ORDER BY timestamp DESC LIMIT 0, 10";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

// if records present
if (mysql_num_rows($result) > 0)
{
// iterate through resultset
// print article titles
while($row = mysql_fetch_array($result))
{
$abrv = substr($row['content'], 0, 125);
echo '
<a href="story.php?id=', $row['id'] ,'">', $row['title'] ,'</a><br />
', $abrv ,'...<br /><br />';
}
}
else
{
echo 'No press releases currently available';
}
// close database connection
mysql_close($connection);
?>


If someone could show me the necessary Action Script I would be light years ahead of the game in understanding this process. Thanks and regards.

eyezberg
November 26th, 2004, 06:47 PM
Don't echo each row, put everything into one var using "." in PHP, and echo this at the end to Flash.
In Flash, use loadVars to call the PHP script, and then textfield.htmltext = nameOfVarYouEcho in the onLoad.. maybe a bit short, but it's late and I gotta sleep :)
will check back ;)

eyezberg
November 26th, 2004, 06:55 PM
http://www.kirupaforum.com/forums/showthread.php?p=681086&posted=1#post681086

unable_to_load
November 29th, 2004, 12:24 PM
Thanks for your help so far but I'm a real n00b and can't figure out what you are talking about :|

Here's what I've tried based on the examples:



while($row = mysql_fetch_array($result))
{
$abrv = substr($row['content'], 0, 125);
$titlelink = <a href="story.php?id=', $row['id'] ,'">', $row['title'] ,'</a>;
$all = $titlelink_$abrv;
}


Flash?


myLoadVars = new LoadVars();
myLoadVars.onLoad = function(success) {
myLoadVars.all;
textdisplay.text = "Display Text:"+myLoadVars.toString()+newline;
};
myLoadVars.load("http://myURL/flashtext.php");


Is this even in the ballbark? Because it doesn't work for me :(

unable_to_load
November 30th, 2004, 10:52 AM
After much toil (and with the fact that someone will no doubt have the same question in the future) I have figured out most of the above scenario!

To transfer php vars (I don't know if this is efficient or not) see below:


// generate and execute query
$query = "SELECT slug, content FROM news ORDER BY timestamp DESC";
$result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$slug = $row['slug'];
$content = $row['content'];
echo "slug=".$slug."content=".$content;
}
}


Pay attention to the bolded var echo.

Now my problem is displaying the information in flash! Here's what I've been playing with (thanks to a code snippet from Macromedia.com):



var c = new LoadVars();
c.onLoad = function() {
textDisplay.text = "returned from php:\n\n";
for (i in this) {
textDisplay.text += i + " = " + this[i] + "\n\n";
}
};
c.load("http://www.mysite.com/mysql.php");


Two problem I have when echoing the php data in flash.

1. The data does not refresh when the page refreshes (it seems to be caching the data received from the php file :()
2. I can't help but think there is a more efficient way to do this! If anyone out there can show me a more efficient way to do the above I'd greatly appreciate it.

Thanks
unable_to_load :D