usadarts Posted February 10, 2008 Share Posted February 10, 2008 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+ Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/ Share on other sites More sharing options...
nethnet Posted February 10, 2008 Share Posted February 10, 2008 You could make that into a JS function and put it into a .js file that you include in every page in the HTML head, or you could just leave it like that in a file and use include() to show it on every page. Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/#findComment-462933 Share on other sites More sharing options...
cooldude832 Posted February 10, 2008 Share Posted February 10, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/#findComment-462936 Share on other sites More sharing options...
usadarts Posted February 10, 2008 Author Share Posted February 10, 2008 This works AWESOME.....thank you. 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. Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/#findComment-462950 Share on other sites More sharing options...
cooldude832 Posted February 10, 2008 Share Posted February 10, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/#findComment-462960 Share on other sites More sharing options...
usadarts Posted February 10, 2008 Author Share Posted February 10, 2008 Excellent again.....thank you kindly. David Quote Link to comment https://forums.phpfreaks.com/topic/90289-how-to/#findComment-462964 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.