Page 1 of 1

making nonrandom text correspond to a random image -- easy?

Posted: Sun Aug 01, 2004 9:51 am
by Daisy Cutter
hello everyone, I've been learning PHP for 2 days now and have hit my first big rut.

I have a script on my page that looks like this:

Code: Select all

<img alt="random image." style="align:center;" src="
<?php
$screen='screen.png';
$eraser='eraser.jpg';
  $randimg&#1111;] = $eraser;
  $randimg&#1111;] = $screen;

  srand ((double) microtime() * 1000000);
  $random_number = rand(0,count($randimg)-1);

  echo ($randimg&#1111;$random_number]);

?>" />
what I want to do is have text underneath the picture that corresponds with the picture.

so
if randimg = $screen
echo "check out my screenshot";
elseif randimg = $eraser
echo "its an eraser";
else
echo "the image failed to load";

but every time it just says "check out my screenshot"
this is probably very easy to do. :oops:

Posted: Sun Aug 01, 2004 10:23 am
by feyd

Code: Select all

if($randimg == $screen)
//.......

Posted: Sun Aug 01, 2004 11:59 am
by Daisy Cutter
thank you very much.

I realized I also needed to change
$randimg to
$randimg[$random_number]

Posted: Tue Aug 03, 2004 6:19 pm
by sel
try to be carefull at the operators you use, = it is not equal with ==
== -> equals
= -> is asigned

Re: making nonrandom text correspond to a random image -- ea

Posted: Tue Aug 03, 2004 7:16 pm
by hawleyjr
Daisy Cutter wrote:I've been learning PHP for 2 days now and have hit my first big rut.
When integrating PHP and HTML it is often easier to write your code as follows:

Code: Select all

<?php
$screen='screen.png'; 
$eraser='eraser.jpg'; 
  $randimg[] = $eraser; 
  $randimg[] = $screen; 

  srand ((double) microtime() * 1000000); 
  $random_number = rand(0,count($randimg)-1); 

  $random_number =  ($randimg[$random_number]); 


?>

Code: Select all

&lt;img alt="random image." style="align:center;" src="&lt;?php echo $random_number;?&gt;"/&gt;
What you’re doing is fine, it will work as needed; this is just a suggestion.

Posted: Wed Aug 04, 2004 3:45 am
by Grim...
A little easier, maybe - define two arrays, one with your images in, and one with the text in. Index them both (so, for example, the forth piece of text in the text array goes with the forth image) then use the same random number on each of them.
If you're going to have lots of photo's, this is definaly the way to go.