Jump to content

[SOLVED] Nested Loops


IronWarrior

Recommended Posts

Hello All,

 

Just a quick question I am using this code:

 

	$directoryName = "images/".$trimmedRandomVehicleName;
		$handle=opendir("$directoryName");
		while (($file = readdir($handle))!==false)
		{
			if ($file != "." && $file !="..")
			{
				?>
				<img src=<? echo "images/".$trimmedRandomVehicleName."/".$file?>>
				<?
			}
		}
		closedir($handle);
	?>

 

This gets all of the images from a directory and displays them, But i need to add a second while loop so it only displays one of those images, and if possible just a random image from that directory...

 

Can anyone help me with this?

Link to comment
https://forums.phpfreaks.com/topic/117541-solved-nested-loops/
Share on other sites

like this?

 

<?php
		$directoryName = "images/".$trimmedRandomVehicleName;
		$handle=opendir("$directoryName");
		$files = array();
		while (($file = readdir($handle))!==false)
		{
			if ($file != "." && $file !="..")
			{
				$files[] = $file;
			}
		}
		closedir($handle);
		$file = $files[array_rand($files);
				?>
				<img src="<? echo $directoryName."/".$file?>">

Link to comment
https://forums.phpfreaks.com/topic/117541-solved-nested-loops/#findComment-604563
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.