| Coding Forum Problems with your code? Let's hear about it. |
02-26-2004, 10:04 AM
|
#1 (permalink)
|
|
Inactive
Join Date: 02-11-04
Location: Corpus Christi, TX
Posts: 901
Latest Blog: None
|
Random Image Loader
I have approxametly 10 images that I would like the browser to randomely pick from each time the page is loaded. Is there a simple script out there that would allow this to happen?
Michael
|
|
|
02-26-2004, 10:07 AM
|
#2 (permalink)
|
|
Inactive
Join Date: 10-13-03
Location: Finland
Posts: 640
Latest Blog: None
|
I belive that you're looking for an image rotator..
hotscripts.com is full of them..
|
|
|
02-26-2004, 10:49 AM
|
#3 (permalink)
|
|
Possible Terrorist
Join Date: 10-13-03
Location: Tuscaloosa, AL or Atlanta
Posts: 4,904
|
[code:1:613fe2053d]
<?php
$location = "http://www.mysite.com/images/";
$files = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$file = array_rand($files, 1)[0];
$ending = ".jpg";
$img = $location . $file . $ending;
echo "<img src="" . $img . "">";
?>[/code:1:613fe2053d]
quick and easy 
__________________
Kyle Varga
"m3lt/theSpear"
student, web designer/coder, future IT consultant
Experience: PHP/MySQL, Java, C++, MS-SQL
|
|
|
02-26-2004, 10:55 AM
|
#4 (permalink)
|
|
Inactive
Join Date: 10-12-03
Location: Cranberry Township
Posts: 275
|
Quote:
|
Originally Posted by theSpear
[code:1:d7404d5b3c]
<?php
$location = "http://www.mysite.com/images/";
$files = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$file = array_rand($files, 1)[0];
$ending = ".jpg";
$img = $location . $file . $ending;
echo "<img src="" . $img . "">";
?>[/code:1:d7404d5b3c]
|
Or as a function.
[code:1:d7404d5b3c]
<?php
function random_image()
{
$location = "http://www.mysite.com/images/";
$files = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$file = array_rand($files, 1)[0];
$ending = ".jpg";
$img = $location . $file . $ending;
return $img;
}
?>
[/code:1:d7404d5b3c]
|
|
|
02-26-2004, 12:31 PM
|
#5 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
[code:1:b893dd3e4e]echo "<img src="" . $img . "">"; [/code:1:b893dd3e4e]
Why not change that to [code:1:b893dd3e4e]echo "<img src="$img">"; [/code:1:b893dd3e4e]
Or will that cause trouble?
|
|
|
02-26-2004, 12:46 PM
|
#6 (permalink)
|
|
Possible Terrorist
Join Date: 10-13-03
Location: Tuscaloosa, AL or Atlanta
Posts: 4,904
|
it works... but tis not as easy to see errors.. just a programming habit of mine.. because I have to use it when i am doing arrays & stuff.. it keeps things clear..
this text is displayed... this text is a variable etc.
__________________
Kyle Varga
"m3lt/theSpear"
student, web designer/coder, future IT consultant
Experience: PHP/MySQL, Java, C++, MS-SQL
|
|
|
02-26-2004, 12:46 PM
|
#7 (permalink)
|
|
Inactive
Join Date: 02-11-04
Location: Corpus Christi, TX
Posts: 901
Latest Blog: None
|
Quote:
|
Originally Posted by theSpear
[code:1:3c3e229642]
<?php
$location = "http://www.mysite.com/images/";
$files = array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
$file = array_rand($files, 1)[0];
$ending = ".jpg";
$img = $location . $file . $ending;
echo "<img src="" . $img . "">";
?>[/code:1:3c3e229642]
quick and easy 
|
would that code go at the top of my coding or where the image needs to be placed? ... and is any of this code need to be edited besides the location of my images?
|
|
|
02-26-2004, 12:49 PM
|
#8 (permalink)
|
|
Possible Terrorist
Join Date: 10-13-03
Location: Tuscaloosa, AL or Atlanta
Posts: 4,904
|
that would go where it needs to be placed.. if you want more than 1 random image then you would use the function that niceguyeddie posted.. then just [code:1:7aa6573cbe]<?php echo "<img src="" . random_image() . "">"; ?>[/code:1:7aa6573cbe] where you would want the image.. the actual function code be anywhere inside <?php ?> tags at the end of the file.. wherever you want it..
__________________
Kyle Varga
"m3lt/theSpear"
student, web designer/coder, future IT consultant
Experience: PHP/MySQL, Java, C++, MS-SQL
|
|
|
02-26-2004, 12:52 PM
|
#9 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
anyway, if you wan't it to happen on the browser, as requested, then a JavaScript way...
Put this in you document first (in the <head> section idealy):
[code:1:7ddbe4fb49]
<script>
var url_before = "/images/image_";
var url_after = ".gif";
var min = 1;
var max = 10;
function printRndImg() {
document.write('<img src="' + url_before + ( min + Math.round(Math.random()*(max-min)) ) + url_after + '" boorder="0">')
}
</script>
[/code:1:7ddbe4fb49]
and set the variables up with you values, then put this anywhere you want an image:
[code:1:7ddbe4fb49]<script>printRndImg();</script>[/code:1:7ddbe4fb49]
|
|
|
02-26-2004, 01:12 PM
|
#10 (permalink)
|
|
Inactive
Join Date: 02-11-04
Location: Corpus Christi, TX
Posts: 901
Latest Blog: None
|
Quote:
|
Originally Posted by LazyJim
anyway, if you wan't it to happen on the browser, as requested, then a JavaScript way...
Put this in you document first (in the <head> section idealy):
[code:1:d6195335d3]
<script>
var url_before = "/images/image_";
var url_after = ".gif";
var min = 1;
var max = 10;
function printRndImg() {
document.write('<img src="' + url_before + ( min + Math.round(Math.random()*(max-min)) ) + url_after + '" boorder="0">')
}
</script>
[/code:1:d6195335d3]
and set the variables up with you values, then put this anywhere you want an image:
[code:1:d6195335d3]<script>printRndImg();</script>[/code:1:d6195335d3]
|
i like this script alot better... much cleaner... but for some reason it's giving me a red X error... i've got my paths set right... any help?
|
|
|
02-26-2004, 01:24 PM
|
#11 (permalink)
|
|
Inactive
Join Date: 02-11-04
Location: Corpus Christi, TX
Posts: 901
Latest Blog: None
|
nvm... i had my images named incorrectly... thanks everyone for you help!!
|
|
|
02-26-2004, 01:48 PM
|
#12 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
no probs 
|
|
|
03-02-2004, 09:59 AM
|
#13 (permalink)
|
|
Inactive
Join Date: 10-17-03
Posts: 146
|
Is there some kind of way to make the random images and links. I mean for example I have 5 banners and I want to link them to 5 different sites. Is it possible to do this?
|
|
|
03-02-2004, 10:33 AM
|
#14 (permalink)
|
|
Inactive
Join Date: 01-12-04
Posts: 999
Latest Blog: None
|
Yes the code example I posted does exactly that. You can even set the alt tags.
Just include the file in your html page.
|
|
|
03-02-2004, 03:50 PM
|
#15 (permalink)
|
|
Inactive
Join Date: 10-17-03
Posts: 146
|
Quote:
|
Originally Posted by LazyJim
anyway, if you wan't it to happen on the browser, as requested, then a JavaScript way...
Put this in you document first (in the <head> section idealy):
[code:1:4975e6cfc9]
<script>
var url_before = "/images/image_";
var url_after = ".gif";
var min = 1;
var max = 10;
function printRndImg() {
document.write('<img src="' + url_before + ( min + Math.round(Math.random()*(max-min)) ) + url_after + '" boorder="0">')
}
</script>
[/code:1:4975e6cfc9]
and set the variables up with you values, then put this anywhere you want an image:
[code:1:4975e6cfc9]<script>printRndImg();</script>[/code:1:4975e6cfc9]
|
OK!
I've got this script to work. How can I make that each picture has it's own link?
|
|
|
03-03-2004, 01:30 PM
|
#16 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
insert the <script>printRndImg();</script> into a link:
[code:1:048911ba22]<a href="somelink.html"><script>printRndImg();</script></a>[/code:1:048911ba22]
|
|
|
03-03-2004, 02:14 PM
|
#17 (permalink)
|
|
Inactive
Join Date: 10-17-03
Posts: 146
|
Quote:
|
Originally Posted by LazyJim
insert the <script>printRndImg();</script> into a link:
[code:1:cc387717b2]<a href="somelink.html"><script>printRndImg();</script></a>[/code:1:cc387717b2]
|
Well, I know that. I do not need this. I need different link for different pictures. So each random picture will have it's own link.
|
|
|
03-03-2004, 03:34 PM
|
#18 (permalink)
|
|
Moderator
Join Date: 10-13-03
Location: UK
Posts: 2,819
Latest Blog: None
|
oh right, so you never wanted a random image thingy at all ( link to your post).
A banner rotation script, or in you case a random banner script, is a little different.
You should use PHP...
in your HTML (but the file needs a .php extension):
[code:1:51c6e88e8a]
<?php
$banner_min = 1;
$banner_max = 4;
$random_banner_number = rand($banner_min, $banner_max);
echo include("banner_$random_banner_number.php");
?>
[/code:1:51c6e88e8a]
and then create 4 files called " banner_X.php" where X is a number from 1 to 4. You can change those numbers to any number, but make sure you also change the $banner_min and $banner_max in the script above. These 4 files I said have a .php extension, but they only need HTML in them, (they can have PHP if you want), or even just text, they just need to be the code for the banner, for example:
[code:1:51c6e88e8a]<a href="link1.html" target="_blank"><img src="image_1.gif"></a>[/code:1:51c6e88e8a]
|
|
|
03-03-2004, 06:22 PM
|
#19 (permalink)
|
|
Inactive
Join Date: 10-17-03
Posts: 146
|
Quote:
|
Originally Posted by LazyJim
oh right, so you never wanted a random image thingy at all ( link to your post).
A banner rotation script, or in you case a random banner script, is a little different.
You should use PHP...
in your HTML (but the file needs a .php extension):
[code:1:05a94f25fc]
<?php
$banner_min = 1;
$banner_max = 4;
$random_banner_number = rand($banner_min, $banner_max);
echo include("banner_$random_banner_number.php");
?>
[/code:1:05a94f25fc]
and then create 4 files called " banner_X.php" where X is a number from 1 to 4. You can change those numbers to any number, but make sure you also change the $banner_min and $banner_max in the script above. These 4 files I said have a .php extension, but they only need HTML in them, (they can have PHP if you want), or even just text, they just need to be the code for the banner, for example:
[code:1:05a94f25fc]<a href="link1.html" target="_blank"><img src="image_1.gif"></a>[/code:1:05a94f25fc]
|
Well, at first I needed the Image Rotator. But then I also thought about banner rotator and thought I could do this with that script.
Ok. Let me try this script you just gave me. Next question, If I want to place this banner to some other page, how can I include them then? Like this:[code:1:05a94f25fc]<img src="page.php"></img>[/code:1:05a94f25fc]
|
|
|
|