Random image and html image map query?
Moderator: General Moderators
- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
Random image and html image map query?
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
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
I'm assuming you're only question is getting the image and the map to pair up? Just define them together.
- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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;
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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>';- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
Thankyou
Thankyou, they both work well.
Dan Walters
Dan Walters
- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
Will this work if the CASE statements are somewhere else on the page....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; }
How will the PHP Server know what to test the CASE statement against?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
danielwalters6 wrote:Will this work if the CASE statements are somewhere else on the page....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; }
How will the PHP Server know what to test the CASE statement against?
Are you familiar with switch...case statements?
- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
No, Sorry...
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?
I've just basic programming knowledge of IF statements, 'simple' case statements While/For Loops etc, all self taught from VB.
Switch Cases?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
The switch statement works like a series of if statements.
This code:
Does the same as this code:
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.
This code:
Code: Select all
switch($i)
{
case 'hi':
doSomething();
break;
case 'bye':
doSomethingElse();
break;
default:
throwError();
}Code: Select all
if($i == 'hi')
{
doSomething();
}
else if($i == 'bye')
{
doSomethingElse();
}
else
{
throwError();
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.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.
- danielwalters6
- Forum Commoner
- Posts: 31
- Joined: Fri May 11, 2007 1:17 pm
- Location: Cambridge, England, UK
Thank you
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;
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;
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Thank you
The "break" keyword works almost anywhere.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;
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.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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
http://php.net/reserved