populate a xml element list - I think

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

populate a xml element list - I think

Post 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!!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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";
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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))
//.................
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post by rhosk »

I'll run with it and get back to you. Thanks again.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

what is that $count for?
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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 »

So, you're giving up on me?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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 ;)
rhosk
Forum Commoner
Posts: 30
Joined: Sun Oct 31, 2004 12:00 pm
Location: Maine, USA

Post 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.
Post Reply