Go Back   CodingForums.com > :: Client side development > Flash & ActionScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2008, 08:17 PM   PM User | #1
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Get images from a folder and render it random into a banner?

Hi guys,

First, I started out with a simple flash-movie where I putted all the pictures I wanted in my banner and rendered it. However, I have found that it isn't exactly what I'm looking for.

I don't know what to call it, so here goes an example:

I have a folder called: header_images. In that folder I put all the images I want to be rendered in my banner, each time random. The image should change to another one from the folder in like some minutes, just like a slideshow.

If I want to change or add an image to the banner slideshow, then I simple put it or delete it from the folder and the script will no longer render it into my banner.

I've already searched google and various websites, but I don't know what that kind of application is called, so any examples or tutorials would be really appriciated. If it even CAN be done?
CaptainB is offline   Reply With Quote
Old 03-26-2008, 08:35 PM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
You will need to use a server side language to read the directory and generate an xml file with the images in a random order. Then you can use actionscript to read the dynamic xml file to get your photos and any other info you need.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Users who have thanked _Aerospace_Eng_ for this post:
CaptainB (04-18-2008)
Old 03-26-2008, 09:35 PM   PM User | #3
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Thank you for your advice, Aerospace.

I did some research which turned into a nice tutorial on how to generate an .xml file with php. However, I'm now stuck - With no further knowledge in xml and actionscript I don't know what to do now.

Here's what I got so far, which should generate my .xml file (Added some comments to make it easier to figure out):
PHP Code:
<?php
/*Inform if the script should invert Windows slashes (’\’ to ‘/’). It’s true if you run the script on Windows and then need to move it to Linux or another Unix platform. */
$invertSlash false;                            

/*The list of directories the script should read and create an XML file for. Inform the path relative to the location of the xmlgen.php*/
$directoryList = array('images/header_images');        

/*The name of the XML file to be generated.*/
$xmlFileName 'header_images.xml';                    

/*The name of the root tag. (Applies to the order of the .xml file tags)*/
$rootTagName 'Images';

/*The name of the root immediate child tags (the directory entries tag). (Applies to the order of the .xml file tags)*/                        
$childTagName 'Image';

/*The tag list of each directory entry. You need to provide a pair of ‘tagName’ => ‘tagValue’ for each desired tag. */
$childTagList = array('name'     => 'images/header_images');
?>
Any clues on where to continue?
CaptainB is offline   Reply With Quote
Old 03-26-2008, 10:44 PM   PM User | #4
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
http://www.kirupa.com/web/xml/XMLwithFlash3.htm

Please do some searching on your own before asking a question.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-26-2008, 11:10 PM   PM User | #5
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
I did and I found what I looked for.

However, I still have problems. I found out how to load the xml file with following code: (http://www.flash-creations.com/notes/dynamic_xml.php)

Quote:
Code:
// import the Delegate class
import mx.utils.Delegate;

// declare a new XML instance
var peoplexml:XML = new XML();

function onXmlLoaded(success:Boolean) {
   if (success) {
      // do whatever you want to do if the XML file was read successfully
      // use peoplexml.firstChild to access the main node
   } else {
      // do whatever you want to do if an XML read error occurred
   }
}

function init() {
   // ignore tabs, returns, and other whitespace between nodes
   peoplexml.ignoreWhite = true;	

   // set the scope of the onLoad function to the main timeline, not peoplexml
   peoplexml.onLoad = Delegate.create(this, onXmlLoaded);
	
   // start loading the file
   peoplexml.load("file.xml");	   
}

init();
Try pasting the code above into a new blank movie, replacing "// do whatever you want to do if an XML read error occurred" with "trace('error reading file');" (without quotes) and you should see your error message, plus a Flash built-in error message, appear in the Output panel when the movie is tested.
But. As I've understood it, the php file I created (code in the previous post) should generate the xml file for me to load. However, when I load that php page, nothing gets generated = I have no xml file to load.

I don't think I've grasped the whole concept, and if I'm right about that, please enlighten me. All this is new to me.
CaptainB is offline   Reply With Quote
Old 03-27-2008, 01:24 AM   PM User | #6
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
You need generate the xml file nodes and parent elements however you also need to pass the correct headers so the browser knows its an xml file. Something like this should work
PHP Code:
<?php

/*
This is a sample file that reads through a directory, filters the jpg 
files and builds an xmllist from it. After looking through this file, you'll 
probably 'get the idea' and'll be able to setup your own directory.
*/


// search for jpg files
$filter ".jpg";
// path to the directory you want to scan
$directory "images/header_images/";


// read through the directory and filter files to an array
$d dir($directory);
if (
$d) { 
    while(
$entry=$d->read()) {  
        
$ps strpos(strtolower($entry), $filter);
        if (!(
$ps === false)) {  
            
$items[] = $entry
        } 
    }
    
$d->close();
    
shuffle($items);
}

// third, the playlist is built in an xspf format
// we'll first add an xml header and the opening tags .. 
header("content-type:text/xml;charset=utf-8");

echo 
"<photolist xmlns='http://xspf.org/ns/0/'>\n";

// .. then we loop through the array ..
for($i=0$i<sizeof($items); $i++) {
    echo 
"<photo>".$directory.$items[$i]."</photo>";
}
 
// .. and last we add the closing tags
echo "</photolist>\n";

?>
Edit the directory if you need to, upload it, navigate to it, if it displays an xml file you are one step closer.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||

Last edited by _Aerospace_Eng_; 03-27-2008 at 01:30 AM..
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-27-2008, 09:50 AM   PM User | #7
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Uhm, it doesn't...:

Quote:
Something like this should work PHP Code:
Warning: shuffle() expects parameter 1 to be array, null given in C:\Documents and Settings\User\Path\To\File\generator.php on line 37

Warning: Cannot modify header information - headers already sent by (output started at C:\Documents and Settings\User\Path\To\File\generator.php:12) in C:\Documents and Settings\User\Path\To\File\generator.php on line 42
CaptainB is offline   Reply With Quote
Old 03-27-2008, 11:43 AM   PM User | #8
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Did you edit the path name? I tested this on my server and it works fine. Thats saying that the array was never populated. You will have to edit the filter as well if the images aren't jpg.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-27-2008, 01:14 PM   PM User | #9
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Yeah, I did edit the file extension and the path, though the path was correct.

It's strange, I tested it on my local server, which did not work and returned the above errors, but when I tested it on my online server it worked.

However, no file is generated.
CaptainB is offline   Reply With Quote
Old 03-27-2008, 03:42 PM   PM User | #10
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Huh? That IS the xml file. Its not meant to generate a .xml file.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-28-2008, 06:02 PM   PM User | #11
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Oh, I thought so as it was within the php tags, but now I really see.
Well, apparently it works then, as it returns no errors but just gives me this: http://behrentzs.com/sites/Hjemmeside/xml.php

Would that be correct? If yes, should I then paste the url into following code:
Code:
// import the Delegate class
import mx.utils.Delegate;

// declare a new XML instance
var peoplexml:XML = new XML();

function onXmlLoaded(success:Boolean) {
   if (success) {
      // do whatever you want to do if the XML file was read successfully
      // use peoplexml.firstChild to access the main node
   } else {
      // do whatever you want to do if an XML read error occurred
   }
}

function init() {
   // ignore tabs, returns, and other whitespace between nodes
   peoplexml.ignoreWhite = true;	

   // set the scope of the onLoad function to the main timeline, not peoplexml
   peoplexml.onLoad = Delegate.create(this, onXmlLoaded);
	
   // start loading the file
   peoplexml.load("xml.php");	   
}

init();
Or what should I do?
CaptainB is offline   Reply With Quote
Old 03-29-2008, 05:38 PM   PM User | #12
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Yes thats accurate but its not reading the photos from the directory meaning the path to the directory is likely incorrect. It should return something like this: http://prod.freehostia.com/test.php
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-29-2008, 06:31 PM   PM User | #13
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
There we go:

http://behrentzs.com/sites/Hjemmeside/xml.php

which results in:
Code:
// import the Delegate class
import mx.utils.Delegate;

// declare a new XML instance
var peoplexml:XML = new XML();

function onXmlLoaded(success:Boolean) {
   if (success) {
      // do whatever you want to do if the XML file was read successfully
      // use peoplexml.firstChild to access the main node
   } else {
      // do whatever you want to do if an XML read error occurred
   }
}

function init() {
   // ignore tabs, returns, and other whitespace between nodes
   peoplexml.ignoreWhite = true;	

   // set the scope of the onLoad function to the main timeline, not peoplexml
   peoplexml.onLoad = Delegate.create(this, onXmlLoaded);
	
   // start loading the file
   peoplexml.load("xml.php");	   
}

init();
Right?

If correct, would I then insert the above code into a .as file and then somehow link to the file in my document or what should I do?

Thank you a lot for the help so far!! Without you I would not even have got started on this.
CaptainB is offline   Reply With Quote
Old 03-30-2008, 03:20 PM   PM User | #14
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
Now you just have load the xml file into your flash document then loop through the nodes in the xml file. You just have to do some research on your own now.
__________________
||||If you are getting paid to do a job, don't ask for help on it!||||
_Aerospace_Eng_ is offline   Reply With Quote
Old 03-30-2008, 06:50 PM   PM User | #15
CaptainB
Regular Coder

 
Join Date: Jun 2007
Posts: 805
Thanks: 123
Thanked 23 Times in 23 Posts
CaptainB is an unknown quantity at this point
Okay, I tried some different things, but all didn't work. When I view the flash document nothing happens.

I don't think I've really got the grasp on this, but I'm trying. This is what I have so far, though it does not work.

As far as I've understood, the bottom part should loop the child notes.
But since I don't really know how to do it, I've gone through pretty many search results now, I still can't do it.

Code:
var xm:XML = new XML();//creating XML object
xm.ignoreWhite = true;//white spaces will be discarded
xm.onLoad = function() {//function will be call when xml file data loaded into xml object 
trace(this)
};
xm.load("xml.php");//loading the xml file data into xml object


Song_Array = new Array;

myXML = new XML;
myXML.ignoreWhite = true;
myXML.onLoad = function(success:Boolean) {
trace("success: "+success);
trace("loaded: "+this.loaded);
trace("firstChild: "+this.firstChild.childNodes.length);
TotalNumOfSongs = this.firstChild.childNodes.length;
TotalNumOfInfoPerSong = this.firstChild.firstChild.childNodes.length;
for (var i=0; i<this.firstChild.childNodes.length; i++) 
{
var _node = this.firstChild.childNodes[i];
Song_Array[i] = new Array();
trace("_node:"+_node.childNodes.length);
for (var j=0; j<_node.childNodes.length; j++) 
{
Song_Array[i].push(_node.childNodes[j].firstChild.nodeValue);
trace("~~~ childNodes: "+_node.childNodes[j].firstChild.nodeValue); 
}
}
//trace("songName, songArtist, songPath:"+ [songName, songArtist, songPath]);

trace("Song_Array.length: "+Song_Array[1]);
};
myXML.load("xml.php");
CaptainB is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:21 AM.


Advertisement
Log in to turn off these ads.