I have some code that I use to display random images, which it draws from a list of possibilities. My problem is that I want to be able to echo the name of the randomized image as text below it. If someone could help me figure out how to do it, I would be very much grateful.

This is the code that randomizes the images:

<?php 
echo "<img src=" . $regular[array_rand($regular,1)] . ">\n";
echo "<img src=" . $regular[array_rand($regular,1)] . ">\n";
echo "<img src=" . $regular[array_rand($regular,1)] . ">\n";
?>

The code draws from a file that reads like this:

<?php

$regular = array (
"cards/moon01.gif", 
"cards/moon02.gif", 
"cards/moon03.gif"
);

?>

First you have to save the randomized output to a variable

$filename =  $regular[array_rand($regular,1)];

Then use the variable $filename to echo the file name and create the <img> source path.

<?php 
   $filename =  $regular[array_rand($regular,1)];
   echo $filename." - <img src=" . $filename  . "><br/>";

   $filename =  $regular[array_rand($regular,1)];
   echo $filename." - <img src=" . $filename  . "><br/>";

   $filename =  $regular[array_rand($regular,1)];
   echo $filename." - <img src=" . $filename  . "><br/>";
   ..
   ..
?>

Hope I understood your question correctly

Thanks! It's very close, but maybe a visual example would explain what I'm hoping for a little better.

This page has an example of what I'm hoping to accomplish:
http://indigoplateau.clavis-sama.com/memoryb.php

The image is posted, and then an echo displays the filename later in the same document. Everytime the page is refreshed, the images change and the text displays the names of those randomized files. I have the code for the randomizer in the link, but it can't be applied to my other randomizer because it draws from a different kind of organization. If it helps, this is the code for the linked page:

<?php include('reward.php'); ?>
<?php
echo "<img src=/cards/" . $r1 . $n1 .".gif>\n";
echo "<img src=/cards/" . $r2 . $n2 .".gif>\n";
echo "<img src=/cards/" . $r3 . $n3 .".gif>\n";
echo "<img src=/cards/" . $r4 . $n4 .".gif>\n";
echo "<img src=/cards/" . $s5 . $n5 .".gif>\n";

?>
<?php
echo "$r1$n1, $r2$n2, $r3$n3, $r4$n4, $s5$n5  ";
?>

The one I'm having trouble with also draws from an include from a separate file if it helps to know. This file has $r = the name of a set of images and $n = the available digits, but the code I'm asking about just has a single name ($regular).

Does that better explain what I'm hoping for?

Member Avatar for diafol

I'd randomize the whole array and pick the first 3 or 4 images in the order.

try shuffle().

If you have a multidimensional array, no problem:

$r = array(array('r' => 'me', 'n' => 'you'),array('r' => 'me2', 'n' => 'you2'),array('r' => 'me3', 'n' => 'you3'));
shuffle($r);
print_r($r);

You can then just select the first 2 or 3 images:

for($x=0;$x<2;$x++){
  echo $r[$x]['r'] . " " . $r[$x]['n'];
} 
//should return the first two ($x=0 and $x = 1).

Can you please explain that a little further, ardav? I'm still pretty new to PHP coding, so I'm not exactly sure how to implement trying that out.

What ardav was trying to explain is this.

There is a method in php called shuffle() which randomize the order of the elements in an array (See : http://www.php.net/manual/en/function.shuffle.php)

Lets take your array for an example

<?php 
	$regular = array (
	"cards/moon01.gif",
	"cards/moon02.gif",
	"cards/moon03.gif",
	"cards/moon04.gif",
	"cards/moon05.gif"
	); 
?>

Now the order of the image names are from 1 to 5 (cards/moon01.gif, cards/moon02.gif,...,cards/moon05.gif).

Now call shuffle() like this

<?php
     shuffle($regular);
?>

Now the order of the image names are NOT from 1 to 5. Now array elements are shuffled. It can be in any random order.

Now lets say you want to use 3 images to display in your site ( The site in the link you gave uses only 2). Lets access the array from the beginning

<?php
	echo $regular[0]; // Prints the value in 0th element
	echo $regular[1]; // Prints the value in 1th element
	echo $regular[2]; // Prints the value in 2th element
?>

Since the array is already shuffled, you are getting elements not in the order you initialize the array. Now every time page reloads, the array gets shuffled and randomize the order of the elements.

So now you can display the images as follows

<?php
	echo "<img src=" . $regular[0] . "><br/>";
	echo "<img src=" . $regular[1] . "><br/>";
	echo "<img src=" . $regular[2] . "><br/>";
?>

Hope I explained well :)

commented: Did a great job of explaining a new concept in a nice and easy-to-understand way. +1

Ah, okay! Thanks for the suggestion ardav, and thanks for the great explanation niranga. I've been tinkering around with it for a day and I think this will help things quite nicely. You guys have been great. Thanks so much!

Welcome :)
If you problem got solved mark this thread as Solved.. and a vote up would be nice :)

Consider it done! :D

:) :) :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.