XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).
Moderator: General Moderators
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Mon Nov 01, 2004 11:21 am
I received some great help over in the PHP code section for a script that populates files via a list and I'm now trying to merge this script to create a xml file for a photo gallery. I think I'm real close, because there are no errors, but it doesn't list the files in one of the elements. Hopefully someone can see an obvious mistake? This is what I have so far -
Code: Select all
<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder>/</image_folder>
<time>3</time><fade>1</fade><repeat>true</repeat>
<captions>true</captions></settings><images>';
$count = "1";
create_tree(".", '/^thumb_/');
function create_tree($file_dir, $filter = '') {
if ($handle = @opendir($file_dir))
{
$list = array();
while (false !== ($file = @readdir($handle))) {
if ($file != "." && $file != "..") {
if( !empty($filter) && !is_dir($file_dir . '/' . $file) )
if( !preg_match($filter, $file) )
continue;
$list[] = $file;
}
}
foreach($list as $file) {
if( is_dir($file_dir . '/' . $file) ) {
create_tree($file_dir . "/" . $file, $filter);
}
else
$xml .= '<image>';
$xml .= '<file>"" . $file_dir . "/" . $file . "\n"</file>';
$xml .= '<caption></caption>';
$xml .= '</image>';
$count++;
}
closedir($handle);
}
}
$xml .= '</images></slideshow>';
echo $xml;
?>
Nothing happens at all between the <file></file> element. Am I close here or do I need to start from scratch
This is my xml output -
Code: Select all
<?xml version="1.0" encoding="UTF-8" ?>
- <slideshow>
- <settings>
<image_folder>/</image_folder>
<time>3</time>
<fade>1</fade>
<repeat>true</repeat>
<captions>true</captions>
</settings>
<images />
</slideshow>
Thanks for any help!!
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Nov 01, 2004 3:07 pm
look at the highlighting: you opened the string with single quotes but trying to close it with double quotes:
Code: Select all
$xml .= '<file>"" . $file_dir . "/" . $file . "\n"</file>';
should be:
Code: Select all
$xml .= '<file>' . $file_dir . '/' . $file . '</file>' . "\n";
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Mon Nov 01, 2004 3:39 pm
Weirdan,
Thanks for helping again, I truly appreciate it. I fixed that line and I still get the exact same output. For some reason, it's not listing the array in the <file> element. I'm still doing a "trial & error" type thing, but I can't get it. Is the "count" syntax in the right place? I just need it to loop and populate that <file> tag. Im really learning as I go here, so you don't have to give me the exact code. Hints would be great so as to learn on my own.
Thanks again!
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Nov 01, 2004 4:07 pm
you are trying to populate the global $xml variable from within the function. To be able to access it you will need to define it as
global inside your function:
Code: Select all
function create_tree($file_dir, $filter = '') {
global $xml; // <======== here
if ($handle = @opendir($file_dir))
//.................
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Mon Nov 01, 2004 4:12 pm
I'll run with it and get back to you. Thanks again.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Nov 01, 2004 4:14 pm
what is that $count for?
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Mon Nov 01, 2004 4:35 pm
Well, this particular code populates that <file> element tag fine without the filters -
Code: Select all
<?
$xml = '<?xml version="1.0" encoding="UTF-8" ?>';
$xml .= '<slideshow><settings><image_folder>images/</image_folder>
<time>3</time><fade>1</fade><repeat>true</repeat>
<captions>true</captions></settings><images>';
$count = "1";
$style = opendir("images");
$exclude = array("desc","php","html");
while($stylesheet = readdir($style)) {
if (stristr($stylesheet,$exclude[0]) ||stristr($stylesheet,$exclude[1]) ||stristr($stylesheet,$exclude[2]) || strlen($stylesheet)< 3 ) continue;
$xml .= '<image>';
$xml .= '<file>'.$stylesheet.'</file>';
$xml .= '<caption></caption>';
$xml .= '</image>';
$count++;
}
closedir($style);
$xml .= '</images></slideshow>';
echo $xml;
?>
I thought that I could merge the 2 codes (first post) and come up with the same result, no? Maybe I should start over.
Understand that I'm still new to this php stuff, but I'm learning something new every day, hence the Newbie status
Don't give up on me Weirdan.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Nov 01, 2004 4:43 pm
functions are another beasts, they have their own scope.
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Mon Nov 01, 2004 5:28 pm
So, you're giving up on me?
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Mon Nov 01, 2004 5:35 pm
I will not if you would show me your ability to RTFM and STFW
ps: disregard my english - it's just too weird for me
rhosk
Forum Commoner
Posts: 30 Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA
Post
by rhosk » Tue Nov 02, 2004 7:12 am
Weirdan wrote: I will not if you would show me your ability to RTFM and STFW
Nah, no need to, I nailed it. Thanks for your help.