Flash & MySQL

Hi,

I’m pulling data from a MySQL database into Flash (MX) through PHP.

In MySQL, I have a table in the database “max_voetsjoeba” called “news” in which there are three fields. A dummy value, “inhoud” and “datum”.

This is the code for the PHP file (thanks Jubba ;)):


<?
$l = mysql_connect("localhost", "max_voetsjoeba","--hidden--");
mysql_select_db("max_voetsjoeba");

$q = "SELECT * FROM news";
$r = mysql_query($q, $l);

if($r){
     $nrows = mysql_num_rows($r);

     $output="";

     for ($i=0; $i < $nrows; $i++){
         $row = mysql_fetch_array($r);

         $output .= "&i=".$i;
         $output .= "&datum".$i."=".$row['datum'];
         $output .= "&inhoud".$i."=".$row['inhoud'];
     }
     print $output;
}
else
{
     print mysql_error($l);
}



mysql_close($l);
?>

This works fine, you can see the output of this PHP file here.

In Flash, I have:

[AS]
onClipEvent (load) {
lv = new loadVars();
lv.onLoad = function(success) {
if (success) {
//this.i is the variable, i is a textfield
i = this.i;
inhoud = this.inhoud
img.loadMovie(“http://users.pandora.be/voetsjoeba/eclipse.jpg”);
} else {
trace(“failed to load script”);
}
};
lv.load(“http://voetsjoeba.asianillusion.com/news.php”);
}
[/AS]

(thanks Ahmed ;))
I can see the image file, but I can’t see the text (inhoud). I took a look at the PHP file and changed “this.inhoud” to “this.inhoud0” (see the PHP file’s output). This worked, but I wanted it to change automatically, so I tried “inhoud = this.inhoud+this.i” (i is 0, see PHP file’s output). This doesn’t work, instead of this.inhoud0 it becomes 0. Why ?

And how do I make the textfield scroll AND have it render as HTML ?

try this:

lv = new loadVars();
        lv.onLoad = function(success) {
                if (success) {
                        //this.i is the variable, i is a textfield
                        i = this.i;
                        inhoud.html = 1
                        inhoud.htmlText = this["inhoud"+i]
                        img.loadMovie("http://users.pandora.be/voetsjoeba/eclipse.jpg");
                } else {
                        trace("failed to load script");
                }
        };
        lv.load("http://voetsjoeba.asianillusion.com/news.php");

and make sure you have a textfield with an instance name of ‘inhoud’… to make it scroll, add the scrollbar component onto the textfield :slight_smile:

It works ! Thanks ahmed ! =) If you don’t mind, I’ve got some additional questions :beam:

See this – There’s much space between the lines. How can I fix this ?
[EDIT]Nevermind … =)[/EDIT]

How can I make it so that, when I add a row to the MySQL database, Flash automatically places the content of the new row above all previous text ?

try this :slight_smile:

onClipEvent (load) {
	inhoud.condenseWhite = true;
	lv = new loadVars();
	lv.onLoad = function(success) {
		if (success) {
			i = this.i;
			inhoud.html = 1;
			for (l=i; l>=0; l--) {
				inhoud.htmlText += this["inhoud"+l];
			}
			img.loadMovie("http://voetsjoeba.asianillusion.com/ca2small.jpg");
		} else {
			trace("failed to load script");
		}
	};
	lv.load("http://voetsjoeba.asianillusion.com/news.php");
}

[AS]for(i=0; i <= 1000; i++){
_root.genius.ahmed.thanks += 1;
}
[/AS]
:beam:

haha :stuck_out_tongue:

Could you give me a hint on making a news thingie like the following…

In my frontpage I have a box in which I print 7 most recent news headlines from mySQL with PHP, sorted by date and limited to 7.

Ok. That works fine. What I would like to do is that when for example the first headline is clicked, I get the first news item opened. If the fourth headline is clicked tha fourth item is opened and so on.

That shouldn’t be too hard, but I can not get it working with Flash.

Anyone?

One advice:

in situations like this:
lv.load(“http://voetsjoeba.asianillusion.com/news.php”);

if u want change to this:
lv.load("./news.php");

Imagine if u have to change the domain … u will have to change and if u have with ./ no problem exist :wink:

villek,

when user click in a item, u have to send a variable to php script and in script u have to verify what item the user have selected and change the SQL instruction with ORDER BY and send again to flash the new result

So how would the code go? “ORDER BY newsID”, or something like that?

The MySql table has newsID(auto increment), author, posted, title and body -rows.

underneath is the code in use (from phpforflash, slightly modified):



$link = @mysql_connect($dbHost, $dbUser, $dbPass);

if (!$link)
{
    print "&newsText=" . urlencode("Could not connect to server");
    exit;
}


if (!@mysql_select_db($dbName))
{

    print "&newsText=" . urlencode("Could not select $dbName database");
    exit;
}

$query = "SELECT * FROM MyNews ORDER BY posted DESC LIMIT 7";


$result = @mysql_query($query);


if ($result && @mysql_num_rows($result) > 0)
{

    $newsText = "";
    $otsikot = "";
    $pvm = "";


    while($row = mysql_fetch_array($result))
    {
	
				
        $posted = strftime("%a %d.%m.%y %H:%M", $row['posted']);

        
        $otsikot .= stripslashes($row['title']);
        $otsikot .= '<br><br>';
				
        $newsText .= stripslashes($row['title']);
        $newsText .= '</b></font><br>';

        $pvm .= $posted . " " . $row['author'];

        $newsText .= stripslashes($row['body']) . '<br><br>';
    }

print "&otsikot=" . urlencode($otsikot);

print "&pvm=" . urlencode($pvm);

print "&newsText=" . urlencode($newsText);


}
else
{

print "&otsikot=" . urlencode("No headlines, Bo!");

print "&newsText=" . urlencode("No news, Bo!");
}


mysql_close($link);

?>



I’m not quite sure how to tell flash that some exact textfield in a movieclip is supposed to print out the newsitem defined earlier.

http://www.macromedia.com has the sort of thing I’m talking about on their front page. Every headline is a link and from a certain link, a certain newsitem is displayed. I just want the item to be displayed in flash.

Thank for the quick reply Drunken !

ok, I sort of got working. It works fine but not dynamically enough.

I fatched the max value of the newsID cell and in order to get for example the second latest news item i just decrement 1 from the max value and print my item out like that.

It sucks 'cause I have to code all the items separately and use separate php files or the “switch” -method or something. I’m really poor with php and Flash:(

Man, if someone out there has an idea on how to do this, pls share your thoughts on a bit more precise level…

Thanks in advance, these forums are really good and I greatly appreciate everyone here. Let’s keep up the work!