php script that populates photos to a html div

I currently have a picture page with about 250 jpeg Images and I currently have a web page where I manually assigned a picture to a section on the page, however this is very time consuming and would like to create a script that will run through and assign the images to a div. While this normally would be easy my problem is the way they filed the images. On the web page I have a quad drawling, think of looking down a scope and seeing the circle and cross, 4 section and each section needs a image assigned to it. the image file names vary and is causing my headache:

The file format Ex is below. so each quad would have 4 images, the first set of numbers before the underscore is the pict id and the number in between the underscores is which section of the quad the pict goes to. But the pict id vary in length and I’m not sure how to go about this. Any thoughts or recommendations are appreciated
1234_1_01.jpeg
1234_2_03.jpeg
1234_3_02.jpeg
1234_4_01.jpeg
345422_1_01.jpeg
345422_2_02.jpeg and so on

Im new to php and these are some snipits that i have pieced together without any luck
[php]<?php
$medianView = “IN”;
$fileLoc = $_SERVER[“SCRIPT_NAME”];
$sigID = basename($fileLoc,".php").PHP_EOL;

include("…/…/…/includes/header.php");
?>

<?php $dir = '../photos';
$files = scandir($dir);
$images = array();
	foreach($files as $file) {
	  if(fnmatch('*.jpg',$file)) {
		$images[] = $file;
	  }
	}
$xp = implode("_", $images);

$thePics = array();

	$nameParts = explode('_',$xp);
		if(!array_key_exists($nameParts[0],$thePics))$thePics[$nameParts[0]]=array();
			$thePics[$nameParts[0]][$nameParts[1]] = $xp;

?>

[/php]

a revision of the code but I keep getting a blank screen, can any one take a quick look and see if anything stick out as to why im getting no errors or outputs?
[php]<?php
$myPicFolder =‘12321_1_2334,23414_2_12321,1231_2_234’;
$thePics = array();
if (is_dir($myPicFolder)) {
if ($dh = opendir($myPicFolder)) {
while (($file = readdir($dh)) !== false) {
if(!is_dir($myPicFolder . $file)){
list($pictureID,$quadID) = explode(’_’.$file);//Seperates the parts of the name for easier access
//check if the ID is already a key in $thePics
if(!array_key_exists($pictureID,$thePics)){
//Add the new ID if it does not exist with an empty array.
$thePics[$pictureID]=array();
}
// Add a new part to the ID since each picture ID has 4 pictures.
$thePics[$pictureID][$quadID] = $file;
}
}
closedir($dh);
}
}
//Cycle through the pictures…
foreach($thePics as $quad){
echo ‘

’;
foreach($quad as $pictureSRC){
echo ‘’;
}
echo ‘
’;
}
?>[/php]

Please be sure to wrap your php code in the proper tags like so…

[php]<?php

$myPicFolder =‘12321_1_2334,23414_2_12321,1231_2_234’;
$thePics = array();
if (is_dir($myPicFolder)) {
if ($dh = opendir($myPicFolder)) {
while (($file = readdir($dh)) !== false) {
if(!is_dir($myPicFolder . $file)){
list($pictureID,$quadID) = explode(’_’.$file);//Seperates the parts of the name for easier access
//check if the ID is already a key in $thePics
if(!array_key_exists($pictureID,$thePics)){
//Add the new ID if it does not exist with an empty array.
$thePics[$pictureID]=array();
}
// Add a new part to the ID since each picture ID has 4 pictures.
$thePics[$pictureID][$quadID] = $file;
}
}
closedir($dh);
}
}
//Cycle through the pictures…
foreach($thePics as $quad){
echo ‘

’;
foreach($quad as $pictureSRC){
echo ‘’;
}
echo ‘
’;
}
?>[/php]

to do this you can either click the php button above the smiley-face that sticks its tongue out or use

[php] YOUR CODE GOES HERE [/php]

Thank you for that will make sure I do that next time

So progress has been made, pictures are now displayed but I am having a hard time figuring out how to extract a image out of a quad array from a certain picture id. Any ideas would be greatly appreciated. Below is a Ex. of Results I am getting now and the code to get it.
[php] list($pictureID,$quadID)=explode(’_’,$file);//Seperates the parts of the name for easier access
//check if the ID is already a key in $thePics
if(!array_key_exists($pictureID,$thePics)){
//Add the new ID if it does not exist with an empty array.
$thePics[$pictureID]=array();
}
// Add a new part to the ID since each picture ID has 4 pictures.
$thePics[$pictureID][$quadID] = $file;
}
}
closedir($dh);
}
}
//Cycle through the pictures…
foreach($thePics as $quad){
echo ‘

’;
foreach($quad as $pictureSRC){
echo ‘’;
}
echo ‘
’;
}[/php]

Results using var_dump:
array (size=38)
1715 =>
array (size=4)
1 => string ‘1715_1_1.jpg’ (length=12)
2 => string ‘1715_2_1.jpg’ (length=12)
3 => string ‘1715_3_1.jpg’ (length=12)
4 => string ‘1715_4_1.jpg’ (length=12)
1718 =>
array (size=4)
5 => string ‘1718_5_1.jpg’ (length=12)
6 => string ‘1718_6_1.jpg’ (length=12)
7 => string ‘1718_7_1.jpg’ (length=12)
8 => string ‘1718_8_1.jpg’ (length=12)

Sponsor our Newsletter | Privacy Policy | Terms of Service