|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Web Coding | |
Random Pictures in HTML/PHP
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
4 gigs of hert
Join Date: Jan 2006
Location: Hamilton Ontario
Posts: 7,270
Rep: 459
![]() ![]() ![]() ![]() ![]() Unique Rep: 303
Trader Rating: 17
|
Say I have 4 or 5 pictures I'd like to have randomly loaded when someone accesses a website. Is there any way to make it so that everytime the site is loaded a different picture is loaded as well?
__________________
|
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
iRun
![]() |
On my site I use this code:
Code:
<img src="http://site/images/<?php echo(rand(1,3)); ?>.jpg" width="600" height="100" alt="logo" /> I doubt this is the politically correct way, but it gets the job done. I'm still teaching myself PHP so I don't know if it's "proper".
__________________
... wherever I may roam; where I lay my head is home ... Electricity Explained --- Got a G15? Use ATI Tool? Check this out! --- Compilation of Free Security Utilities --- Rule #1: Obey All Rules --- E2160 @ 3400mhz, Orthos Stable
Last edited by Chipp : 04-10-08 at 08:08 PM. |
|||||||||||||
|
|
|
|
#3 (permalink) | |||||||||||||
|
4 gigs of hert
Join Date: Jan 2006
Location: Hamilton Ontario
Posts: 7,270
Rep: 459
![]() ![]() ![]() ![]() ![]() Unique Rep: 303
Trader Rating: 17
|
Thanks alot Chip, time for some experimenting!
Lets say my images are named kopi kops kopo They are all different sizes, and I'd like to alternate between them. Would the code be Code:
<img src="http://site/images/<?php echo(rand(1,3)); ?>.jpg"/>
__________________
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||||||||||||
|
iRun
![]() |
No, that would not work.
The "echo" command inserts the number generated by the math function (rand) as the file name. Thus, your files would have to be names 1.jpg, 2.jpg, 3.jpg, etc, etc. Basically, once the php script is finished executing the resulting HTML would look like this: Code:
<img src="http://site/images/2.jpg" />
__________________
... wherever I may roam; where I lay my head is home ... Electricity Explained --- Got a G15? Use ATI Tool? Check this out! --- Compilation of Free Security Utilities --- Rule #1: Obey All Rules --- E2160 @ 3400mhz, Orthos Stable
|
|||||||||||||
|
|
|
|
#5 (permalink) | |||||||||||||
|
Audiophile
|
Kopi, Chipp means renaming the pictures with numbers as names (1,2, & 3 for example) so with that PHP code chosing a random number within say 1 - 3 and having 3 jpgs all renamed to 1.jpg, 2.jpg, and 3.jpg; the random script would correspondingly load a random picture.
So you would want to rename the pictures: 1.jpg or 4.jpg.. when the PHP picks for example "4", 4.jpg would load.
__________________
-=-
Zune > iPod Currently Playing: HL2 + GRiD + WiC Ultimate Wallpaper Site Antec 900 Cable/sound ModObama? Gag me with a fork.
|
|||||||||||||
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
4 gigs of hert
Join Date: Jan 2006
Location: Hamilton Ontario
Posts: 7,270
Rep: 459
![]() ![]() ![]() ![]() ![]() Unique Rep: 303
Trader Rating: 17
|
Oh okay, I'm with ya.
1.jpg 2.jpg 3.jpg They are all different sizes, and I'd like to alternate between them. Would the code be Code:
<img src="http://site/images/<?php echo(rand(1,3)); ?>.jpg"/>
__________________
|
|||||||||||||
|
|
|
|
#7 (permalink) | |||||||||||||
|
Audiophile
|
I would say yes, but i dont do HTML.. i was just making sure you understood what he was saying
![]() Can you test it? Good Luck ~Higgins
__________________
-=-
Zune > iPod Currently Playing: HL2 + GRiD + WiC Ultimate Wallpaper Site Antec 900 Cable/sound ModObama? Gag me with a fork.
|
|||||||||||||
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
iRun
![]() |
Well, you might get some better results by changing the path to the image files to wherever your keeping your images, etc.
![]() I threw up a simple test page for you - I think you can see the directory structure and how the PHP actually works when you look at the source code in your browser (as the browser will display just the HTML after the number has been put into the filename). http://sethhochberg.com/random/index.php The exact code I used for that page is this: PHP Code:
__________________
... wherever I may roam; where I lay my head is home ... Electricity Explained --- Got a G15? Use ATI Tool? Check this out! --- Compilation of Free Security Utilities --- Rule #1: Obey All Rules --- E2160 @ 3400mhz, Orthos Stable
|
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
4 gigs of hert
Join Date: Jan 2006
Location: Hamilton Ontario
Posts: 7,270
Rep: 459
![]() ![]() ![]() ![]() ![]() Unique Rep: 303
Trader Rating: 17
|
Chipp, you're my hero.
I understand now, thanks alot to the both of you and thanks for throwing that test up for me Chipp! ![]()
__________________
|
|||||||||||||
|
|
|
|
#10 (permalink) | |||||||||||||
|
iRun
![]() |
Not a problem at all.
![]() Good luck with your site, keep on codin'!
__________________
... wherever I may roam; where I lay my head is home ... Electricity Explained --- Got a G15? Use ATI Tool? Check this out! --- Compilation of Free Security Utilities --- Rule #1: Obey All Rules --- E2160 @ 3400mhz, Orthos Stable
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|