Page 1 of 1

populate a xml element list - I think

Posted: Mon Nov 01, 2004 11:21 am
by rhosk
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

&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
- &lt;slideshow&gt;
- &lt;settings&gt;
  &lt;image_folder&gt;/&lt;/image_folder&gt; 
  &lt;time&gt;3&lt;/time&gt; 
  &lt;fade&gt;1&lt;/fade&gt; 
  &lt;repeat&gt;true&lt;/repeat&gt; 
  &lt;captions&gt;true&lt;/captions&gt; 
  &lt;/settings&gt;
  &lt;images /&gt; 
  &lt;/slideshow&gt;
Thanks for any help!!

Posted: Mon Nov 01, 2004 3:07 pm
by Weirdan
look at the highlighting: you opened the string with single quotes but trying to close it with double quotes:

Code: Select all

$xml .= '&lt;file&gt;"" . $file_dir . "/" . $file . "\n"&lt;/file&gt;';
should be:

Code: Select all

$xml .= '&lt;file&gt;' . $file_dir . '/' . $file . '&lt;/file&gt;' . "\n";

Posted: Mon Nov 01, 2004 3:39 pm
by rhosk
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!

Posted: Mon Nov 01, 2004 4:07 pm
by Weirdan
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; // &lt;======== here
    if ($handle = @opendir($file_dir))
//.................

Posted: Mon Nov 01, 2004 4:12 pm
by rhosk
I'll run with it and get back to you. Thanks again.

Posted: Mon Nov 01, 2004 4:14 pm
by Weirdan
what is that $count for?

Posted: Mon Nov 01, 2004 4:35 pm
by rhosk
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 :cry: Don't give up on me Weirdan.

Posted: Mon Nov 01, 2004 4:43 pm
by Weirdan
functions are another beasts, they have their own scope.

Posted: Mon Nov 01, 2004 5:28 pm
by rhosk
So, you're giving up on me?

Posted: Mon Nov 01, 2004 5:35 pm
by Weirdan
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 ;)

Posted: Tue Nov 02, 2004 7:12 am
by rhosk
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.