simple php syntax problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jben.net
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:55 am

simple php syntax problems

Post by jben.net »

Hi,

I'm writing a script that loops through a series of directories and adds each filename (all jpgs) into an array (1 per dir), this works fine. I'm having trouble with the second part which loops through the 3 sets of 3 folders and generates an xml file for me to load into Flash (MX, not that that matters). I works fine if I just loop through 1 set of 3 (ie 1 for-loop) but if I add a second for loop (for determining which main directory I'm in) the code breaks ?? I've pasted the lot below and would really appreciate people ideas, hints etc into where I'm going wrong.

My dir structure is (repeated 3 times):

main folder: gallery
sub gallery folder: press
inside press folder: big, preview and thumbs

When I run an echo in the second for loop, instead of being returned the filename(s) of that index in the array, I get d,d,d, p,p,p j, j, j, - very weird. There are 2 images in each folder so why isn't it working ?

Here's my code:

Code: Select all

<?php
	
	$galleries = array("djing","press","justforfun");
	
	$djingBig = array();
	$djingPreview = array();
	$djingThumbs = array();
	
	$pressBig = array();
	$pressPreview = array();
	$pressThumbs = array();
	
	$justforfunBig = array();
	$justforfunPreview = array();
	$justforfunThumbs = array();
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./djing/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./djing/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./djing/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./press/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./press/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./press/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./justforfun/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./justforfun/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./justforfun/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	//echo $bigXML;
	
	$totalGalleries = count($galleries);
	
	//print $galleries&#1111;0];
	
	$theXML = "<" ; 
	$theXML .= "?";
	$theXML .= "xml version='1.0'";
	$theXML .= "?";
	$theXML .= ">\n";
	
	$theXML .= "\t<gallery>\n";
	
	for($i=0; $i<$totalGalleries; $i++)
	&#123;
		$theXML .= "\t<" . $galleries&#1111;$i] . ">\n";
		
		
		$currentBig = $galleries&#1111;$i] . "Big";
		$currentPreview = $galleries&#1111;$i] . "Preview";
		$currentThumbs = $galleries&#1111;$i] . "Thumbs";
		
		//echo (!is_array($currentBig));
		//echo("<br>");
				
		$totalImages = count($currentBig);
		//echo($currentBig . "<br>");
		//echo($totalImages . "<br>");
	
		for($j=0; $j<$totalImages ; $j++)
		&#123;
			
			echo($currentBig&#1111;$j] . "<br>");
			echo($currentPreview&#1111;$j] . "<br>");
			echo($currentThumbs&#1111;$j] . "<br><br>");
			
			$theXML .= "\t\t\t<item>\n";
			$theXML .= "\t\t\t\t<bigImageURL>big/" . $currentBig&#1111;$j] . "</bigImageURL>\n";
			$theXML .= "\t\t\t\t<previewImageURL>preview/" . $currentPreview&#1111;$j] . "</previewImageURL>\n";
			$theXML .= "\t\t\t\t<thumbImageURL>thumbs/" . $currentThumbs&#1111;$j] . "</thumbImageURL>\n";
			$theXML .= "\t\t\t</item>\n";
		&#125;
		
		$theXML .= "\t</" . $galleries&#1111;$i] . ">\n";
	&#125;
	
	$theXML .= "\t</gallery>";
		
		
	//echo($theXML);
?>
Thanks in advance,

Jon
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Here:

Code: Select all

$currentBig = $galleries[$i] . "Big"; 
      $currentPreview = $galleries[$i] . "Preview"; 
      $currentThumbs = $galleries[$i] . "Thumbs";
You change arrays to strings so when you then try and manipulate them as arrays it doesn't work.

Mac
jben.net
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:55 am

Post by jben.net »

thought that might be the problem, how can I get around it ?? In actioscript it doesn't matter so I'm not used to it ;-)

The only way I can think of is to add a conditional into the second for loop but that seems like a daft idea ! Any suggestions ??

Thanks for the speedy response,

Jon
jben.net
Forum Newbie
Posts: 5
Joined: Mon Apr 14, 2003 11:55 am

Post by jben.net »

Ok, got it. Realised I had no need to do it all in 1 for loop so I just did it with 3.

Thanks,

Jon

Code: Select all

<?php
	
	$galleries = array("djing","press","justforfun");
	
	$djingBig = array();
	$djingPreview = array();
	$djingThumbs = array();
	
	$pressBig = array();
	$pressPreview = array();
	$pressThumbs = array();
	
	$justforfunBig = array();
	$justforfunPreview = array();
	$justforfunThumbs = array();
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./djing/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./djing/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./djing/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($djingThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./press/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./press/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./press/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($pressThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	// gets all files in 'big' folder
	if ($bigDir = opendir('./justforfun/big')) &#123;
    	while (false !== ($file = readdir($bigDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunBig, $file);
        	&#125;
    	&#125;
    	closedir($bigDir);
	&#125;
	
	// gets all files in 'preview' folder
	if ($prevDir = opendir('./justforfun/preview')) &#123;
    	while (false !== ($file = readdir($prevDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunPreview, $file);
        	&#125;
    	&#125;
    	closedir($prevDir);
	&#125;
	
	// gets all files in 'thumbs' folder
	if ($thumbsDir = opendir('./justforfun/thumbs')) &#123;
    	while (false !== ($file = readdir($thumbsDir))) &#123;
        	if ($file != "." && $file != ".." && $file != ".DS_Store") &#123;
        		array_push ($justforfunThumbs, $file);
				
        	&#125;
    	&#125;
    	closedir($thumbsDir);
	&#125;
	
	
	$totalGalleries = count($galleries);
	
	
	$theXML = "<" ; 
	$theXML .= "?";
	$theXML .= "xml version='1.0'";
	$theXML .= "?";
	$theXML .= ">\n";
	
	$theXML .= "\t<gallery>\n";
	
	$theXML .= "\t\t<djing>\n";
	
	$totalDjing = count($djingBig);
	
	for($j=0; $j<$totalDjing ; $j++)&#123;
	
		$theXML .= "\t\t\t<image>\n";
		$theXML .= "\t\t\t\t<bigImageURL>big/" . $djingBig&#1111;$j] . "</bigImageURL>\n";
		$theXML .= "\t\t\t\t<previewImageURL>preview/" . $djingPreview&#1111;$j] . "</previewImageURL>\n";
		$theXML .= "\t\t\t\t<thumbImageURL>thumbs/" . $djingThumbs&#1111;$j] . "</thumbImageURL>\n";
		$theXML .= "\t\t\t</image>\n";
	&#125;
	
	$theXML .= "\t\t</djing>\n";
	
	$totalPress = count($pressBig);
	
	$theXML .= "\t\t<press>\n";
	
	for($j=0; $j<$totalPress ; $j++)&#123;
	
		$theXML .= "\t\t\t<image>\n";
		$theXML .= "\t\t\t\t<bigImageURL>big/" . $pressBig&#1111;$j] . "</bigImageURL>\n";
		$theXML .= "\t\t\t\t<previewImageURL>preview/" . $pressPreview&#1111;$j] . "</previewImageURL>\n";
		$theXML .= "\t\t\t\t<thumbImageURL>thumbs/" . $pressThumbs&#1111;$j] . "</thumbImageURL>\n";
		$theXML .= "\t\t\t</image>\n";
	&#125;
	
	$theXML .= "\t\t</press>\n";
	
	$totalJustforfun = count($justforfunBig);
	
	$theXML .= "\t\t<justforfun>\n";
	
	for($j=0; $j<$totalJustforfun ; $j++)&#123;
	
		$theXML .= "\t\t\t<image>\n";
		$theXML .= "\t\t\t\t<bigImageURL>big/" . $justforfunBig&#1111;$j] . "</bigImageURL>\n";
		$theXML .= "\t\t\t\t<previewImageURL>preview/" . $justforfunPreview&#1111;$j] . "</previewImageURL>\n";
		$theXML .= "\t\t\t\t<thumbImageURL>thumbs/" . $justforfunThumbs&#1111;$j] . "</thumbImageURL>\n";
		$theXML .= "\t\t\t</image>\n";
	&#125;
	
	$theXML .= "\t\t</justforfun>\n";
	
	
	$theXML .= "\t</gallery>";
		
		
	echo($theXML);
	
?>
Post Reply