Log in

View Full Version : Random Image with filenames



valkyriesound
08-29-2011, 01:28 AM
Greetings:

I'm trying to get random images to load on a website using the RAND function. The images are in a folder called "images" and named with filenames such as "fuji.jpg"

I have been able to get random jpg names "1.jpg" or "2.jpg" to show up but the images do not show on the website.

I'm having problems converting the filenames into an array which I can then somehow key to numbers (for RAND) and then display the images.

This is homework, so great hints are very appreciated.

Thanks!





<?php
$file = 'includes/title.inc.php';
if (file_exists($file) && is_readable($file)) {
include($file);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Japan Journey
<?php if (isset($title)) {
echo "— {$title}";
}?>
</title>

<link href="assets/journey.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<div id="header">
<h1>Japan Journey </h1>
</div>
<div id="wrapper">
<?php include('includes/menu.inc.php'); ?>
<div id="maincontent">
<h1>A journey through Japan with PHP </h1>
<p>Ut enim ad minim veniam, quis nostrud exercitation consectetur adipisicing elit. Velit esse cillum dolore ullamco laboris nisi in reprehenderit in voluptate. Mollit anim id est laborum. Sunt in culpa duis aute irure dolor excepteur sint occaecat.</p>
<div id="pictureWrapper"><img src="<?php include('includes/randomimage.php');?>" width="350" height="237" class="picBorder" /></div>
<p>Eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Cupidatat non proident, quis nostrud exercitation ut enim ad minim veniam.</p>
<p>Consectetur adipisicing elit, duis aute irure dolor. Lorem ipsum dolor sit amet, ut enim ad minim veniam, consectetur adipisicing elit. Duis aute irure dolor ut aliquip ex ea commodo consequat.</p>
<p>Quis nostrud exercitation eu fugiat nulla pariatur. Ut labore et dolore magna aliqua. Sed do eiusmod tempor incididunt velit esse cillum dolore ullamco laboris nisi.</p>
</div>
<?php include('includes/footer.inc.php'); ?>
</div>
</body>
</html>



Random image PHP


<?php
$my_images = array (

"images/basin.jpg",

"images/fountains.jpg",

"iamges/fuji.jpg",

"kinkakuji.jpg",

"menu.jpg",

"monk.jpg",
"ryoanji.jpg"

);



// Total number of images in the folder

$total = "7";



// Change to the type of files to use eg. .jpg or .gif

$file_type = ".jpg";



// Change to the location of the folder containing the images

$image_folder = "images/";



$start = "1";





$random = rand($start, $total);



$image_name = $random . $file_type;



echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\""


?>

JShor
08-29-2011, 04:00 AM
You know, even though it's not there, every single-dimensional array item has a number as its key. The key starts at zero. Just generate a random number between zero and the count of the images, minus one (since it starts at zero and not one).

Secondly, the rand() function accepts two arguments: [start] and [end]. Oh, also, your image HTML tag is screwed up. That can't be helping anything.



<?php
$my_images = array (

"images/basin.jpg",

"images/fountains.jpg",

"iamges/fuji.jpg",

"kinkakuji.jpg",

"menu.jpg",

"monk.jpg",
"ryoanji.jpg"

);



// Total number of images in the folder

$total = (count($my_images) - 1);

// Change to the type of files to use eg. .jpg or .gif

$file_type = ".jpg";

// Change to the location of the folder containing the images

$image_folder = "images/";

$random = rand(0, $total);

// Here's where we get the image file name by key.


$image_name = $my_images[$random]. $file_type;



echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\" />";


?>