Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop Issues

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
1
16
US
I created a function for presenting images from a database (MySQL) and it is working after a fashion but I can't get the loop to work quite right as I suspect it needs a second loop inside that I am unsure how to do. Any help is much appreciated.

$ImageIDs is an array of integer values; $ImageCaptions is an array of text values and the other values are not arrays. Not all $ImagesIDs have a matching $ImageCaptions value. In other words, not all images have captions and that might be a clue to where it is failing as it is giving only a single image when there should be up to three.

Code:
function ShowImage($ImageIDs, $ImageCaptions, $ImageType, $Alignment, $Thumbnail) {
	$OpenImage = ($Thumbnail == 1) ? "<div class=\"ListThumb\">":"<div class=\"PhotoBox\">";
	$ShowCaption = "<div class=\"ImageCaption\">";
	$FullDomain = "[URL unfurl="true"]http://".$_SERVER[/URL]['HTTP_HOST'];
	$LocalPath = SitePath("Viewers");
	$ShowImage = "<img src=\"".$LocalPath."show_image.php?ID=";

	$count = count($ImageIDs);

	for ($i = 0; $i < $count; $i++) :
		$ImageID = $ImageIDs[$i];
		$ImageCaption = (isset($ImageCaptions[$i])) ? $ImageCaptions[$i]:"";
		$ImageCaption = ($ImageCaption && $Thumbnail == 0) ? $ShowCaption.$ImageCaption."</div>\n":"";
		$AltText = ($Thumbnail == 1 && $ImageCaption) ? " alt=\"".$ImageCaption."\"":"";

		// Overwrites $ImageType for random images
		if ($ImageID == "10000" && $Thumbnail == 0) :
			$ImageType = "10&Rand=1";
		elseif ($ImageID == "10000" && $Thumbnail == 1) :
			$ImageType = "11&Rand=1";
		endif;

		// Get image height and width parameters
		// NOTE: $ImageSize IS PRODUCING AN ARRAY RATHER THAN THE NEEDED VALUES
		$ImageSize = ($ImageID) ? list($attr) = getimagesize($FullDomain.$LocalPath."show_image.php?ID=$ImageID&Type=$ImageType"):"";
		$ImageSize = $ImageSize[3];
			
		if ($Alignment == 1) : // Show images horizontally side-by-side			
			$Images = ($ImageID) ? "$OpenImage$ShowImage$ImageID&Type=$ImageType\" $ImageSize$AltText>":"";
			$Images .= ($ImageCaption) ? "$ImageCaption":"";
			$Images .= "</div>";
		else : // Show images vertically above one another
			$Images = ($ImageID) ? "$OpenImage$ShowImage$ImageID&Type=$ImageType\" $ImageSize$AltText>$ImageCaption</div>\n":"";
		endif;
		return $Images;
	endfor;
}

Thank you.
 
the problem is this line

Code:
return $Images;

which is inside the for loop. so the loop will only ever execute once.

perhaps you wanted the Images to be constructed as an array and returned thus? or did you want it to be a string? If the latter, then beware that your else block here:
Code:
else : // Show images vertically above one another
    $Images = ($ImageID) ? "$OpenImage$ShowImage$ImageID&Type=$ImageType\" $ImageSize$AltText>$ImageCaption</div>\n":"";
endif;
overwrites the value of $Images each time it is invoked.
 
Sorry for the late reply but I'm away until late this week. You're right about the return being mislocated. I had noticed that too but forgot to move it. Anyway, I'll report once it's changed and working. Thank you.
 
Although probably still a bit of a kludge, the function is working more or less properly now after a total rework from the one I posted originally. In case it helps anyone, it is below along with some samples of the output.

Code:
function ShowImage($ImageIDs, $ImageCaptions, $ImageType, $Alignment, $Thumbnail) {
	$FullDomain = "[URL unfurl="true"]http://".$_SERVER[/URL]['HTTP_HOST'];
	$LocalPath = SitePath("Viewers");
	$ShowImage = "<img src=\"".$LocalPath."show_image.php?ID=";

	// Be sure IDs and Captions are an array
	$ImageIDs = (is_array($ImageIDs)) ? $ImageIDs: array($ImageIDs);
	$ImageCaptions = (is_array($ImageCaptions)) ? $ImageCaptions: array($ImageCaptions);

	$Count = count($ImageIDs);
	$LastKey = key(array_slice($ImageIDs, -1, 1, TRUE));
	$Images = ($ImageIDs[0]) ? $OpenImage:"";

	// Provide opening style div per image type
	$OpenImage = ($Thumbnail == 1) ? "<div class=\"ListThumb\">":"<div class=\"PhotoBox\">";

	if ($Thumbnail == 1) :
		$ImageSize = ($ImageIDs[0] && $ImageIDs[0] != "10000") ? list($attr) = getimagesize($FullDomain.$LocalPath."show_image.php?ID=$ImageIDs[0]&Type=$ImageType"):"";
		$ImageSize = (isset($ImageSize)) ? $ImageSize[3]:"";
		$AltText = ($ImageCaptions[0]) ? " alt=\"".trim(strip_tags(str_replace("<br>"," ",$ImageCaptions[0])))."\"":" alt=\"Image\"";
		$Images = "$OpenImage$ShowImage$ImageIDs[0]&Type=$ImageType\" $ImageSize$AltText></div>\n";
	else :
		for ($i = 0; $i < $Count; $i++) :
			$ImageID = $ImageIDs[$i];
			$ImageCaption = (isset($ImageCaptions[$i])) ? $ImageCaptions[$i]:"";
			$ImageCaption = ($ImageCaption) ? "<div class=\"ImageCaption\">$ImageCaption</div>\n":"";
			$AltText = ($ImageCaption) ? " alt=\"".trim(strip_tags(str_replace("<br>"," ",$ImageCaption)))."\"":" alt=\"Image\"";

			// Get image height and width parameters
			$ImageSize = ($ImageID && $ImageID != "10000") ? list($attr) = getimagesize($FullDomain.$LocalPath."show_image.php?ID=$ImageID&Type=$ImageType"):"";
			$ImageSize = (isset($ImageSize)) ? $ImageSize[3]:"";

			// Overrides $ImageType for full-size random images
			$ImageType = ($ImageID == "10000") ? "10&Rand=1":$ImageType;

			if ($Alignment == 1) : // Show multiple images horizontally side-by-side
				$Images .= "$ShowImage$ImageID&Type=$ImageType\" $ImageSize$AltText>";
			else : // Show multiple images vertically above one another
				$Images .= ($i > 0) ? $OpenImage:""; // Open div for all but first image
				$Images .= "$ShowImage$ImageID&Type=$ImageType\" $ImageSize$AltText>";
				$Images .= $ImageCaption;
				$Images .= ($ImageIDs[$i] != $ImageIDs[$LastKey]) ? "</div>":""; // Close div for all but last image
			endif;
		endfor;
		// Provides single caption, if available, for horizontally aligned images
		$Images .= ($Alignment == 1 && $ImageCaptions[0]) ? "<div class=\"ImageCaption\">".$ImageCaptions[0]."</div>\n":"";
	// Provide closing style div
		$Images .= "</div>\n";
	endif;

  return $Images;
}

For horizontally aligned images, it builds HTML like this:

Code:
<div class="PhotoBox"><img src="/internals/viewers/show_image.php?ID=10&Type=4" width="183" height="375" alt="Caption for photo 1">

<img src="/internals/viewers/show_image.php?ID=11&Type=4" width="141" height="375" alt="Image">
<div class="ImageCaption">Caption for photo 1</div>
</div>

For vertically aligned images, it builds HTML like this:

Code:
<div class="PhotoBox"><img src="/internals/viewers/show_image.php?ID=10&Type=4" width="183" height="375" alt="Caption for photo 1">
<div class="ImageCaption">Caption for photo 1</div>
</div>

<div class="PhotoBox"><img src="/internals/viewers/show_image.php?ID=11&Type=4" width="141" height="375" alt="Caption for photo 2">
<div class="ImageCaption">Caption for photo 2</div>
</div>

And for Thumbnail images, which are always single photos and do not have captions, it builds the HTML like this:

Code:
<div class="ListThumb"><img src="/internals/viewers/show_image.php?ID=177&Type=2" width="50" height="44" alt="Thumbnail image)"></div>

Of course, this uses other custom functions for certain paths and for fetching the images from the database but they can also be physical files from the filesystem with some modification to the function.
 
I should add that, although my original post indicated that it would show up to three images, what I meant was that's all, at the most, that I'm giving it. It is actually virtually unlimited to how many it can show within the confines of common sense.
 
Oops! An error as this line:

Code:
$OpenImage = ($Thumbnail == 1) ? "<div class=\"ListThumb\">":"<div class=\"PhotoBox\">";

should be the first line in the function or at least placed somewhere above this one where the variable is being referenced:

Code:
$Images = ($ImageIDs[0]) ? $OpenImage:"";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top