Jump to content

How to


usadarts

Recommended Posts

I have some code to display a random image at the top of each webpage.  I will be adding more images every so often.  I really to not want to have to modify 50+ pages everytime I do this.

 

I have the following code at the top of each page:

 <script language="JavaScript" type="text/javascript">
var images = [
["<img src='/pictures/banner1.jpg' alt='Banner Image 1'>", 1], 
["<img src='/pictures/banner2.jpg' alt='Banner Image 2'>", 2], 
["<img src='/pictures/banner3.jpg' alt='Banner Image 3'>", 3], 
["<img src='/pictures/banner4.jpg' alt='Banner Image 4'>", 4],
["<img src='/pictures/banner5.jpg' alt='Banner Image 5'>", 5],
["<img src='/pictures/banner6.jpg' alt='Banner Image 6'>", 6],
["<img src='/pictures/banner7.jpg' alt='Banner Image 7'>", 7],
]
;var randomValue = Math.random() * 7;var imageId = -1;
for (i = 0; i < images.length; i++) {
if (randomValue <= images[i][1]) {
imageId = i;
break;
}
}
document.write(images[imageId][0]);
      </script>

 

Is it possible to create a seperate page with just this information and have some kind of code to call this file?  This way, in the future, I would only have to change 1 file instead of 50+

Link to comment
Share on other sites

my own randomizer session based image generator

 

(never gets the same one on 2 loads)

<?php
$header_img = $_SESSION['head_img'];
        $i = 0;
while($header_img === $_SESSION['head_img'] && $i <500){
	$header_img = rand(0,count(glob("images/headers/banner*.jpg"))-1);
               $i++;
}
$_SESSION['head_img'] = $header_img;
?>

 

(I added the word banner to it for you and your pictures folder

 

so to output the img say

echo "<img src=\"banner".$_SESSION['head_img'].".jpg\" alt=\"\" />";

 

and it won't matter if you have 2 or 5000 images in the folder (note it must have 2 images else it can't "rotate" and it will loop through 500 times before dying)

Link to comment
Share on other sites

This works AWESOME.....thank you. ;D

 

Another question....same as the first actually:

I also ran into a time when I had to add a link to the left column of the website and ended up updating 50+ webpages.  I want the left cpimn information all in 1 file and would need to only update that file.  I tried what nethnet stated, but did not work. 

 

Link to comment
Share on other sites

your looking into the basics of CMS which basically for your idea you want to do something like

Original page

<body>
<div class="head">Header Img</div>
<div class="navi>
Navi...
Navi...
Navi...
Navi...
Navi...
Navi...
Navi...
Navi...
</div>

make a second file called navi_include or something like that and put in it

Navi...
Navi...
Navi...
Navi...
Navi...
Navi...
Navi...
Navi...

then replace the original to look like

<body>
<div class="head">Header Img</div>
<div class="navi>
<?php include('navi_include.html');?>
</div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.