Random image and html image map query?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Random image and html image map query?

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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;
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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>';
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Thankyou

Post by danielwalters6 »

Thankyou, they both work well.

Dan Walters
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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?
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

No, Sorry...

Post 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?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

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

Post 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.
User avatar
danielwalters6
Forum Commoner
Posts: 31
Joined: Fri May 11, 2007 1:17 pm
Location: Cambridge, England, UK

Thank you

Post 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;
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Thank you

Post 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.
Last edited by superdezign on Wed May 30, 2007 2:32 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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