Page 1 of 1

Random image and html image map query?

Posted: Tue May 29, 2007 10:01 am
by danielwalters6
I'm just thinking aloud, your opinions or suggestions would be appreciated;

I have a website, which is to display a different image each time it's refreshed or accessed. A random selection from about 4 different images (I've not decided exactly how many just yet...

I have managed to get hold of some javascript which does this.
However I need an image map so that different parts of the image are clickable, with different destinations...

If i used javascript to change the image's scr, then the "name" of it would remain the same... therefore using the same image map, and becoming useless to me...

Could I (this is the thinking aloud)

Set a PHP variable to a random value between (say) 1 and 4
Then perform a case later on in my html page?

For example;
---

variable=randomnumberbetween1and4

Bits of html header....
IF variable = 1 THEN
echo "ImageMapForImage1"
END IF
IF Variable = 2 THEN
echo "ImageMapForImage2"
END IF
IF Variable = 3 THEN
echo "ImageMapForImage3"
END IF
IF Variable = 4 THEN
echo "ImageMapForImage4"
END IF
End Of Html Header

Bits of html

IF Variable = 1 THEN
echo "Image1"
EndIF

End So Forth....

There must be an easier way of doing this ie, ECHO "ImageMapForImage"&Variable or something like that.

Any suggetions are always appreciated, code examples are good too - if you hadn't guessed, I'm a newbie to php.

Thankyou in advance

Dan Walters

Posted: Tue May 29, 2007 10:14 am
by superdezign
Yes, that could be done very easily. You'd make a switch statement for a random variable which each different outcome case, each with it's own HTML for it's image and that image's map paired up together.

I'm assuming you're only question is getting the image and the map to pair up? Just define them together.

Posted: Tue May 29, 2007 10:28 am
by danielwalters6
Brilliant, it's nice to know I'm not going crazy, could you elaborate on "defining them together" ?


TIA

Dan Walters
A v. uncompetent php newbie

Posted: Tue May 29, 2007 10:46 am
by superdezign

Code: Select all

switch(rand(1,4))
{
    case 1:
        echo '<img src="image1.jpg" usemap="map1" />';
        echo '<map name="map1"><area /></map>';
        break;
    case 2:
        echo '<img src="image2.jpg" usemap="map2" />';
        echo '<map name="map2"><area /></map>';
        break;
}

Posted: Tue May 29, 2007 12:06 pm
by John Cartwright
Condensed to

Code: Select all

$rand = rand(1,4);
echo '<img src="image'.$rand.'.jpg" usemap="map'.$rand.'" />';
echo '<map name="map'.$rand.'"><area /></map>';

Thankyou

Posted: Wed May 30, 2007 10:13 am
by danielwalters6
Thankyou, they both work well.

Dan Walters

Posted: Wed May 30, 2007 12:18 pm
by danielwalters6
superdezign wrote:

Code: Select all

switch(rand(1,4))
{
    case 1:
        echo '<img src="image1.jpg" usemap="map1" />';
        echo '<map name="map1"><area /></map>';
        break;
    case 2:
        echo '<img src="image2.jpg" usemap="map2" />';
        echo '<map name="map2"><area /></map>';
        break;
}
Will this work if the CASE statements are somewhere else on the page....

How will the PHP Server know what to test the CASE statement against?

Posted: Wed May 30, 2007 1:53 pm
by superdezign
danielwalters6 wrote:
superdezign wrote:

Code: Select all

switch(rand(1,4))
{
    case 1:
        echo '<img src="image1.jpg" usemap="map1" />';
        echo '<map name="map1"><area /></map>';
        break;
    case 2:
        echo '<img src="image2.jpg" usemap="map2" />';
        echo '<map name="map2"><area /></map>';
        break;
}
Will this work if the CASE statements are somewhere else on the page....

How will the PHP Server know what to test the CASE statement against?

Are you familiar with switch...case statements?

No, Sorry...

Posted: Wed May 30, 2007 1:55 pm
by danielwalters6
No I'm not, sorry

I've just basic programming knowledge of IF statements, 'simple' case statements While/For Loops etc, all self taught from VB.

Switch Cases?

Posted: Wed May 30, 2007 2:05 pm
by superdezign
The switch statement works like a series of if statements.

This code:

Code: Select all

switch($i)
{
    case 'hi':
        doSomething();
        break;
    case 'bye':
        doSomethingElse();
        break;
    default:
        throwError();
}
Does the same as this code:

Code: Select all

if($i == 'hi')
{
    doSomething();
}
else if($i == 'bye')
{
    doSomethingElse();
}
else
{
    throwError();
}
The advantage of switch...case statements as opposed to if...else statements is that it looks more organized when the list gets long, and you can easily make different cases overlap or end with the "break" keyword. The "break" keyword tells the switch statement to end. However, if you don't break and allow it to continue, if any of the other cases are met, it will perform the actions of those cases as well.

Posted: Wed May 30, 2007 2:18 pm
by feyd
superdezign wrote:The advantage of switch...case statements as opposed to if...else statements is that it looks more organized when the list gets long, and you can easily make different cases overlap or end with the "break" keyword. The "break" keyword tells the switch statement to end. However, if you don't break and allow it to continue, if any of the other cases are met, it will perform the actions of those cases as well.
Not quite true. It will continue execution of code until it encounters a break statement or the end of the switch, whichever comes first. The cases don't matter once a match is found.

Thank you

Posted: Wed May 30, 2007 2:21 pm
by danielwalters6
Brilliant, good examples too.

I have used a case statement before in Visual Basic, but never heard of switch-case statements.

BREAK is a good idea, is a case statement it's only use?
can you use it within a loop?

or would you use an indicator;

Re: Thank you

Posted: Wed May 30, 2007 2:31 pm
by superdezign
danielwalters6 wrote:Brilliant, good examples too.

I have used a case statement before in Visual Basic, but never heard of switch-case statements.

BREAK is a good idea, is a case statement it's only use?
can you use it within a loop?

or would you use an indicator;
The "break" keyword works almost anywhere.
I believe that, in PHP, if you are using in a place where break wouldn't work (i.e., you need to use return), it let's you know.

Posted: Wed May 30, 2007 2:31 pm
by feyd
Unless you understand that it will fall-through, use a break. Breaks are not limited to just cases. They can be used in any of the loop constructs as well. For added interest, look up continue as well.

http://php.net/reserved