View Full Version : Random links based on directory contents?
kingnimrod
04-01-2004, 12:01 PM
Here's what I'm trying to do:
Within my main flash movie, I want to be able to load new swfs that will replace the default content. Each swf will have several buttons, none of which will be hard-coded to link to anything specific - I want each button to randomly call an swf from the numerous swf files in the web directory. I need each swf to have a unique descriptive name (not numbered). I will periodically add swf files to the directory, so there will be a larger pool of random swf's to pull in.
How do I create an array from the directory contents and get the buttons to randomly choose from this array?
I have just started delving into php a bit, but I'm getting a bit of a headache trying to figure out how to make all of this work. If anyone feels up to giving me a step by step through it, I'd greatly appreciate it.
thanks
kingnimrod
04-01-2004, 12:07 PM
Here's what I'm trying to do:
Within my main flash movie, I want to be able to load new swfs that will replace the default content. Each swf will have several buttons, none of which will be hard-coded to link to anything specific - I want each button to randomly call an swf from the numerous swf files in the web directory. I need each swf to have a unique descriptive name (not numbered). I will periodically add swf files to the directory, so there will be a larger pool of random swf's to pull in.
How do I create an array from the directory contents and get the buttons to randomly choose from this array?
I have just started delving into php a bit, but I'm getting a bit of a headache trying to figure out how to make all of this work. If anyone feels up to giving me a step by step through it, I'd greatly appreciate it.
thanks
you_rock
04-01-2004, 03:26 PM
My recommendation is to create a file naming system ending in a three digit number.
filename001.swf
filename002.swf
Then add the filename as a variable in an array object.
For the numbers, create a variable that throws a number to something like 1000, and if (success) fails then subtract by 1 until the boolean is true.
Next, use random to append the file name to convert toString.
Parse the two together.
Load.
Flash can not read from a windows directory. HTML has a file object that can however. But since you really only need to create a random number, it is not necessary to go through the trouble.
kingnimrod
04-01-2004, 05:54 PM
I figured out the php code necessary to generate an array from a directory and present it as a string that can be used by flash (i think)
// begin code
<?php
$Mystring ="&swfarray="
$dir =opendir ('.' );Â*Â*Â*
while (( $file =readdir ($dir )) !== false ) {
Â*Â*Â*Â*Â*if( substr ($file , - 3,3) == "swf" ){
Â*Â*Â*Â*Â*Â*Â*Â*Â* $Mystring .= $file .","
Â*Â*Â*Â* }
}Â*Â*
echo $Mystring ;
?>
// end
This way I can give my swf's descriptive names and still have the flash function call them in randomly
now I am just working on the actionscript side of things....
kingnimrod
04-01-2004, 05:55 PM
oh - ignore those silly ***
killGerald.com
04-01-2004, 07:46 PM
Thanks so much for posting that PHP code! I've wanted to do something similar for a long time now but didn't think PHP could read directories. Just a note though - i kept getting errors using your script until I put in a couple of semi-colons.
<?php
$Mystring ="&txtarray=";
if($handle = opendir('.')){
while (($file = readdir($handle)) !== false ) {
if( substr ($file , - 3,3) == "txt" ){
$Mystring .= $file .",";
}
}
echo $Mystring ;
}
?php>
Thanks again!
-Gerald
http://www.killGerald.com
kingnimrod
04-01-2004, 10:35 PM
yeah - for some reason pasting the code here stripped those out too - sorry I didn't catch that.
Once I figure out how to get everything working, I will put together a little tutorial and put the link on the forum
kingnimrod
04-01-2004, 10:38 PM
oh- one question - I see you added an if statement - is that necessary for it to work right?
killGerald.com
04-02-2004, 01:02 AM
Hi,
Yr right - the if statement was totally superfluous (an artifact of my debugging attempts)...
The final php code (for my purposes) looks like:
<?php
$Mystring ="&txtArray=";
$dir =opendir ('.' );
while (( $file =readdir ($dir )) !== false ) {
if( substr ($file , - 3,3) == "txt" ){
$Mystring .= $file .",";
}
}
echo $Mystring ;
?>
i named it files.php and put it into a subdirectory named "dreams" (I'm building a dream log).
in that directory, i placed some files (junk ones with junk data) to read from. you can check it all out here:
http://www.killgerald.com/dreams/
and here's the fla I made:
http://www.killgerald.com/dreams/testDiary.fla
and here's the swf:
http://www.killgerald.com/dreams/testDiary.swf
sorry about the scrollbar code in the "inDreams" scene - the relevant info is labeled "//:::::::::::::::::::: NEXT/PREVIOUS DREAM BUTTONS :::::::"
(lines 20-36).
I'm pretty much brand-spankin' new to flash and especially actionscript, so critiques are very welcome...
-gerald.
oh yeah - i got tripped up a little because the php file creates an element at the end of the array that's empty ("") - it's easy to just pop() off though...
McGiver
04-04-2004, 06:52 PM
This will list all files in the directory of the php file, this will be the php file and the swf.
change "$dir=".";" to anthing else i.e. "$dir="swfs";" for another directory.
the php file was called "list.php" in my example
<?
$dir=".";
$cdir=opendir ($dir);
echo "files=";
while ($file = readdir ($cdir)) {
$ftype=is_file("$dir/$file");
if ($ftype) {
echo "$file";
echo "|";
}
}
closedir($cdir);
?>
mylv = new LoadVars();
mylv.load("list.php");
mylv.onLoad=function(success){
createTextField("textf", 10, 10, 10, 400, 400);
textf.multiline=true
filearr = mylv.files.split("|");
filearr.pop()
//filearr is contains now every file inside the directory you scanned
//select a random file from the array with filearr[random(filearr.length)]
textf.text = filearr.length+" files in this directory. files are: "+filearr;
};
Glen Charles Rowell
06-08-2006, 02:02 PM
Who killed Gerald? I know the post is from 2004 but I wanted to see the working fla :p
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.