PDA

View Full Version : loading variable text files with Flash and PHP


sebnewyork
05-14-2005, 04:52 PM
Hi all

I am making a dynamic slide show with flash and a PHP script. The PHP script gets the list of images in a folder, sends the total number of images and their path to flash, and then flash loads the images one after another. I've got this working perfectly.
The problem is I want to do exactly the same thing, but with text files instead of images.
And for some reason, I can't get it to work. I just don't understand well enough the logic behind loading text files.

here's my script to load the images, and it works:

var loadImages:LoadVars = new LoadVars();
loadImages.onLoad = function() {
if (this.totalImages != undefined) {//totalImages is the total number of images sent by the PHP script
imagesToLoad = new Array();
for (var i = 0; i < this.totalImages; i++) {
imagesToLoad[i] = this["img"+i];
}
_root.stage0.createEmptyMovieClip("loader0", 0);
_root.stage0.loader0.createEmptyMovieClip("holder0", 0);
_root.stage0.loader0.holder0.loadMovie(imagesToLoa d[0]);
_root.stage0.loader0.onEnterFrame = function() {
trace(this.holder0._width);
if (this.holder0._width != 0) {
delete this.onEnterFrame;
_root.stage0.gotoAndPlay(2);
}
}
}
}
var imagesToLoad:Array;
loadImages.load(to_php+"?ck="+new Date().getTime()+"&path="+urlpath);

So I tried to adapt that for loading text files, and i can't get it to work:
var loadTexts:LoadVars = new LoadVars();
loadTexts.onLoad = function() {
if (this.totalImages != undefined) {//I am using the same PHP script to list the text files than the one I use to list the image files, so here totalImages in ths total number of text files...
textsToLoad = new Array();
for (var i = 0; i < this.totalImages; i++) {
textsToLoad[i] = this["txt"+i];
}
_root.stage0.txt_MC.text = textsToLoad[0];//'text_MC' is my dynamic text field, inside the MC 'stage0'
_root.stage0.gotoAndPlay(2);
}
}
var textsToLoad:Array;
loadTexts.load(to_php+"?ck="+new Date().getTime()+"&path="+urlpath);
Could you help me adapt the first script to the second so that it loads texts instead of images?

Navarone
05-14-2005, 06:31 PM
Are you loading the images and text at the same time? It looks like you are using the same movie clip to load the text files as you are the images? You also are putting the holder0 on level 0. I think in order for this to work you need to create a seperate movieclip for the text and specify a different level 5 or 10, anything really just so it's above 0. See if creating a seperate movie clip for images and one for text makes any difference.

lelales
05-14-2005, 08:11 PM
Here is a PHP file:
<?

/* MySQL details */
$dbHost = "localhost";
$dbUser = "";
$dbPass = "";
$dbName = "";
$table = "info";

/* Attempt connection to MySQL server */
$link = @mysql_connect($dbHost, $dbUser, $dbPass);

/* If connection wasn't successful... */
if (!$link) {

/* Return error information to Flash and quit! */
print "&list=" . urlencode("<b>Error:</b> Could not conenct to database") . "&";
exit;
}

/* Attempt to select our database */
/* If not able to select... */
if (!@mysql_select_db($dbName)) {

/* Return error information to Flash and quit! */
print "&list=" . urlencode("<b>Error:</b> Could not find $dbName database") . "&";
exit;
}

/* Fetch current time from server */
$currentTime = time();



/* Build SQL query to fetch all entries from the table */
$query = "SELECT * FROM $table";

/* Execute query */
$result = mysql_query($query);

/* If there was a problem with the query... */
if (!$result || @mysql_num_rows($result) < 1) {

/* Return error information to Flash and quit! */
print "&list=" . urlencode("No entries in table $table") . "&";
exit;
}

/* Reset variable to hold output */
$list = "";

/* For each table entry returned... */
while($row = mysql_fetch_array($result)) {

/* Create human readable date from timestamp */
$entryDate = strftime("%A %d/%m/%y", $row['entryDate']);

/* Add details to output variable */
$list .= $row['text'] . "<br>";
}

/* Output data in required format */
print "&info=" . urlencode($list) . "&";

/* Close link to MySQL */
mysql_close($link);

?>


Here is the Flash code:
LoadVarsText3 = new LoadVars();
LoadVarsText3.load("info.php");
//assign a function which fires when the data is loaded:
LoadVarsText3.onLoad = function(success) {
if (success) {
trace("done loading");
//Now that we know the data is loaded,
//set the text content of the Text Field
//with the instance name "scroller" equal to the
//contents of the variable
_root.text.htmlText = this.info;
} else {
trace("not loaded");
}

You must already have a textfield in place with an instance name of "text".

Also you need a database with a table name of "info" with your text inside.