Jump to content

array/loop/config help


cinek
 Share

Recommended Posts

I'm trying to create a site, where I list some main images - 5 per page, I scan the folder for more images, if there is more then 1 image, a see more link shows up next to the main image. The configuration file for this looks something like this:idNo;param1;param2;param3;param4;param5what I want to do, is add another option at the end, so that I'm able to specify more then one image to be shown on the main page, so if I add another field to the config text file, and put something like 1,2,3 - that means that images named 1.jpg, 2.jpg & 3.jpg should also be listed on the main page in the following format:

<ul><li><img src="1.jpg" alt="asdf"..... /></li><li><img src="2.jpg" alt="asdf"..... /></li><li><img src="3.jpg" alt="asdf"..... /></li></ul>

I managed to get it to display just 1 main image from a folder, but I have no idea how to get more then one to show. If you wondering why I need this - I basically want to use a jquery image slider plugin later - this requires the above formatI know I need to add another variable below: $linkName = $contents[$index][5]; - something like $sliderImg = $contents[$index][6]; - but I have no idea where to go from therethis is my data.php file:

<?php// Get file contents		$file = "config.txt";	$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);	$contents = array();	foreach($lines as $line) {		$contents[] = explode(';', $line);	}		while($index < $end)	{		if (isset($contents[$index])) {				$no = $contents[$index][0];		$directoryName = $contents[$index][1];		$agencyLink = $contents[$index][2];		$agency = $contents[$index][3];		$siteLink = $contents[$index][4];		$linkName = $contents[$index][5];				//check if there's more then 1 image in the directory		$files = array();		$dir = opendir("projects/$directoryName");				// if dir doesn't exist; break		if(!$dir)		{ 			echo "Problem opening dir";		 	break;		}				$n = 0;		while(($file = readdir($dir)) !== false) {			if($file !== '.' && $file !== '..' && !is_dir($file)) {		   $files[] = $file;		   $n++;		 }		}			$id = $_GET['id'];			if(!isset($id))			{				// echo data back to the site				echo '<div class="content">';				echo "\n";				echo '<img id="'.$directoryName.'-0" src="projects/'.$directoryName.'/'.$directoryName.'-0.jpg" alt="'.$directoryName.'" />';				echo "\n";				echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a>';				echo 'WWW: <a href="'.$siteLink.'"><span class="'.$linkName.'">'.$linkName.'</span></a></div>';								// if there's > 1 image display see more				if($n > 1)				{					 echo '<a href="?page='.$page.'&id='.$no.'"><img class="more" src="images/see_more.jpg" alt="see more" /></a>';				}								echo '<img class="line" src="images/linia.gif" alt="linia" />';				//echo '</div>';				echo '</div>';				closedir($dir);			} else {								$i = 0;								// don't display empty divs				if($id === $no)				{					echo '<div class="subContent">';					while ($id == $no && $i < $n){						echo '<img id="'.$directoryName.'-'.$i.'" src="projects/'.$directoryName.'/'.$directoryName.'-'.$i.'.jpg" alt="'.$directoryName.'" />';						$i++;					}					if($id === $no)					{						 echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a></div>';					}					echo '</div>';					}			}		}		$index++;		echo "\n";	}?>

Link to comment
Share on other sites

Well, instead of using .txt file you could done it in PHP with var.

$files = "1.jpg;2.jpg"; // If using this you will need explode

or

$files = array( "1.jpg", "2.jpg", "3.jpg" ); // I suggest this then.

Then

foreach ($files as $file_name) {  echo '<img src="' . $file_name . '" />';}

Link to comment
Share on other sites

thanks, but the problem is, I'm showing images from multiple projects on the site, so this info cannot be hard coded. I need to be able to change/add it to any project
right, well Haris S was showing you the basic concept of displaying images, which you seemed to be having trouble with too. One way to make this dynamic is to store folder directory paths in a DB, and depending on how the user navigates to this particular slideshow page, (say by choosing a drop down of categories), you could have PHP serach the database for that particular category, return the path to it, and then you could use PHP's directory methods to read all the files from that path, filter them so that only images are included, and put them in an array, or whichever data structure suits your needs.
Link to comment
Share on other sites

I have added another field to the cofiguration file, I'm trying to explode it - which works as it's displaying the array. I have also added the foreach loop which Haris_S suggested. But when I view the code, it's coming up as Array - not the value from the file - and none of the images are displayed ;/here's what the file looks like now

<?php// Get file contents $file = "config.txt"; $lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $contents = array(); foreach($lines as $line) { $contents[] = explode(';', $line); } while($index < $end) { if (isset($contents[$index])) { $no = $contents[$index][0]; $directoryName = $contents[$index][1]; $agencyLink = $contents[$index][2]; $agency = $contents[$index][3]; $siteLink = $contents[$index][4]; $linkName = $contents[$index][5]; $slideImg = $contents[$index][6]; //check if there's more then 1 image in the directory $files = array(); $dir = opendir("projects/$directoryName"); // if dir doesn't exist; break if(!$dir) { echo "Problem opening dir"; break; } $slideSettings[] = explode(" ", $slideImg); $n = 0; while(($file = readdir($dir)) !== false) { if($file !== '.' && $file !== '..' && !is_dir($file)) { $files[] = $file; $n++; } } $id = $_GET['id']; if(!isset($id)) { // echo data back to the site echo '<div class="content">'; echo "\n"; echo '<ul>'; echo "\n"; foreach ($slideSettings as $slide){ echo '<li><img id="'.$directoryName.'-0" src="projects/'.$directoryName.'/'.$directoryName.'-'.$slideSettings.'.jpg" alt="'.$directoryName.'" /></li>'; break; } echo "\n"; echo '</ul>'; echo "\n"; echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a>'; echo 'WWW: <a href="'.$siteLink.'"><span class="'.$linkName.'">'.$linkName.'</span></a></div>'; // if there's > 1 image display see more if($n > 1) { echo '<a href="?page='.$page.'&id='.$no.'"><img class="more" src="images/see_more.jpg" alt="see more" /></a>'; } echo '<img class="line" src="images/linia.gif" alt="linia" />'; //echo '</div>'; echo '</div>'; closedir($dir); } else { $i = 0; // don't display empty divs if($id === $no) { echo '<div class="subContent">'; while ($id == $no && $i < $n){ echo '<img id="'.$directoryName.'-'.$i.'" src="projects/'.$directoryName.'/'.$directoryName.'-'.$i.'.jpg" alt="'.$directoryName.'" />'; $i++; } if($id === $no) { echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a></div>'; } echo '</div>'; } } } $index++; echo "\n"; }?>
any ideas? Edited by cinek
Link to comment
Share on other sites

what does the processed page's html source look like?

Link to comment
Share on other sites

nvm that - I managed to get the accual values to show up - now I face another problem. The loop doesn't seem to be doing its job. It seems to be increasing with every echo. So if I want to echo 3 images - image 1,2,3 from the first project, I can't - it's showing only one image. It's showing 2 images from project 2 - even tho I only want it to display one. Showing 3 images from project 3 - even tho I only want to display one - etc etc etcany ideas what's wrong?this is what I have now

<?php// Get file contents		$file = "config.txt";	$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);	$contents = array();	foreach($lines as $line) {		$contents[] = explode(';', $line);	}		while($index < $end)	{		if (isset($contents[$index])) {				// declare vars for file fields		$no = $contents[$index][0];		$directoryName = $contents[$index][1];		$agencyLink = $contents[$index][2];		$agency = $contents[$index][3];		$siteLink = $contents[$index][4];		$linkName = $contents[$index][5];		$slideImg = $contents[$index][6];				//check if there's more then 1 image in the directory		$files = array();		$dir = opendir("projects/$directoryName");				// if dir doesn't exist; break		if(!$dir)		{ 			echo "Problem opening dir";		 	break;		}				// explode slider image config field		$slideSettings[] = explode(" ", $slideImg);				//scan for multiple files in project folders		$n = 0;		while(($file = readdir($dir)) !== false) {			if($file !== '.' && $file !== '..' && !is_dir($file)) {		   $files[] = $file;		   $n++;		 }		}			$id = $_GET['id'];			if(!isset($id))			{				// echo data back to the site - prepare for slider plugin				echo '<div class="content">';				echo "\n";				echo '<ul>';				echo "\n";				foreach ($slideSettings as $slide => $value){					echo '<li><img id="'.$directoryName.'-'.$slide.'" src="projects/'.$directoryName.'/'.$directoryName.'-'.$slide.'.jpg" alt="'.$directoryName.'-'.$slide.'" /></li>';					//break;				}				echo "\n";				echo '</ul>';				echo "\n";				echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a>';				echo 'WWW: <a href="'.$siteLink.'"><span class="'.$linkName.'">'.$linkName.'</span></a></div>';								// if there's > 1 image display see more				if($n > 1)				{					 echo '<a href="?page='.$page.'&id='.$no.'"><img class="more" src="images/see_more.jpg" alt="see more" /></a>';				}								echo '<img class="line" src="images/linia.gif" alt="linia" />';				//echo '</div>';				echo '</div>';				closedir($dir);			} else {								$i = 0;								// don't display empty divs				if($id === $no)				{					echo '<div class="subContent">';					while ($id == $no && $i < $n){						echo '<img id="'.$directoryName.'-'.$i.'" src="projects/'.$directoryName.'/'.$directoryName.'-'.$i.'.jpg" alt="'.$directoryName.'" />';						$i++;					}					if($id === $no)					{						 echo '<div class="projectInfo">Agency: <a href="'.$agencyLink.'"><span>'.$agency.'</span></a></div>';					}					echo '</div>';					}			}		}		$index++;		echo "\n";	}?>

edit: the data file, from which it should read looks like this for the first project -

25;param1;param2;param3;param4;param5; 0 1 2
the 0 1 2 - is exploded, and should return images 0, 1 & 2 for one of the projects Edited by cinek
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...