Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Random Pictures in HTML/PHP
New Posts  All Forums:Forum Nav:

Random Pictures in HTML/PHP

post #1 of 14
Thread Starter 
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?
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
post #2 of 14
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" />
See the little PHP bit in the middle? That is set up so that when it generates a random number (between 1 and 3 in my case), the number becomes the name of my image file. Thus, if you name your images a number you can use the random number generator to select the image for you. Of course you'd want to edit the image properties to whatever size you need (or just remove them if the images are different sizes).

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".
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
post #3 of 14
Thread Starter 
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"/>
And the images would simply be in the images directory?
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
post #4 of 14
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" />
In that case, "2" would have been the output from the random selector. Does that make sense?
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
post #5 of 14
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.
    
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD3H-B3 XFX HD5870 16GB G.Skill RipjawsX 
Hard DriveOptical DriveCoolingOS
60GB OCZ Vertex 3 + 2x TB Seagate LG DVD+RW Stock Intel Windows 7 64bit / OSX Mountain Lion 
MonitorKeyboardPowerCase
Dell ST2210 + 17" IBM Das Ultimate S Antec TruePower 650W Antec P183 
MouseMouse PadAudioAudio
Logitech MX Revolution X-Trac Ripper Objective 2 + ODAC Combo Sennheiser HD650 + Klipsch 2.1 Promedia 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD3H-B3 XFX HD5870 16GB G.Skill RipjawsX 
Hard DriveOptical DriveCoolingOS
60GB OCZ Vertex 3 + 2x TB Seagate LG DVD+RW Stock Intel Windows 7 64bit / OSX Mountain Lion 
MonitorKeyboardPowerCase
Dell ST2210 + 17" IBM Das Ultimate S Antec TruePower 650W Antec P183 
MouseMouse PadAudioAudio
Logitech MX Revolution X-Trac Ripper Objective 2 + ODAC Combo Sennheiser HD650 + Klipsch 2.1 Promedia 
  hide details  
Reply
post #6 of 14
Thread Starter 
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"/>
Or do i have to change some stuff
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
post #7 of 14
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
    
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD3H-B3 XFX HD5870 16GB G.Skill RipjawsX 
Hard DriveOptical DriveCoolingOS
60GB OCZ Vertex 3 + 2x TB Seagate LG DVD+RW Stock Intel Windows 7 64bit / OSX Mountain Lion 
MonitorKeyboardPowerCase
Dell ST2210 + 17" IBM Das Ultimate S Antec TruePower 650W Antec P183 
MouseMouse PadAudioAudio
Logitech MX Revolution X-Trac Ripper Objective 2 + ODAC Combo Sennheiser HD650 + Klipsch 2.1 Promedia 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel 2500k Gigabyte Z68X-UD3H-B3 XFX HD5870 16GB G.Skill RipjawsX 
Hard DriveOptical DriveCoolingOS
60GB OCZ Vertex 3 + 2x TB Seagate LG DVD+RW Stock Intel Windows 7 64bit / OSX Mountain Lion 
MonitorKeyboardPowerCase
Dell ST2210 + 17" IBM Das Ultimate S Antec TruePower 650W Antec P183 
MouseMouse PadAudioAudio
Logitech MX Revolution X-Trac Ripper Objective 2 + ODAC Combo Sennheiser HD650 + Klipsch 2.1 Promedia 
  hide details  
Reply
post #8 of 14
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:
Code:

<img src="http://sethhochberg.com/random/images/<?php echo(rand(1,3)); ?>.jpg" alt="kopi test" />
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
post #9 of 14
Thread Starter 
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!

Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
Kopisaurus
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k @ 5ghz 1.395v Gigabyte P67A-UD3P-B3 ATI HD6950 2GB (flashed --> 6970) 16GB Gskill Sniper 1600mhz 
Hard DriveOptical DriveOSMonitor
500gb WD Black / 2x 1TB WD Black RAID1 LG DVD/RW Super Multi Windows 7 Enterprise x64 ASUS ML239H 23" LED  
KeyboardPowerCaseMouse
Logitech G15 (modded) Corsair TV750W V2 Lian-Li Lancool PC-K62B Logitech G5 
  hide details  
Reply
post #10 of 14
Not a problem at all.

Good luck with your site, keep on codin'!
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
Morpheus Mini
(7 items)
 
  
CPUMotherboardGraphicsRAM
i7-6700T SN970 GTX 960 Crucial CT102464BF160B 
Hard DriveOSAudio
Samsung 850 Evo M.2 Windows 10 Pro MOTU Audio Express 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Web Coding
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Web Coding › Random Pictures in HTML/PHP