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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

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

Post 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:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if($randimg == $screen)
//.......
Daisy Cutter
Forum Commoner
Posts: 75
Joined: Sun Aug 01, 2004 9:51 am

Post by Daisy Cutter »

thank you very much.

I realized I also needed to change
$randimg to
$randimg[$random_number]
sel
Forum Newbie
Posts: 1
Joined: Tue Aug 03, 2004 6:19 pm
Location: Sell It & Pc Headquarters
Contact:

Post by sel »

try to be carefull at the operators you use, = it is not equal with ==
== -> equals
= -> is asigned
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

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

Post 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.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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.
Post Reply