+ Reply to Thread
Results 1 to 6 of 6

Thread: How to change a home page picture on each refresh / reload

  1. #1
    phoenix2010 is offline x10 Sophmore phoenix2010 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    241

    How to change a home page picture on each refresh / reload

    ok guys !
    im after picking your brains for an easy way to create a picture box that displays say a folder full of 20 pics 1 pic at a time and chages the picture in the box on every page refresh. !
    ive seen some different forms of code to impliment such boxes but after trying a few times to get it to work ive given up temporaraly and thought i would ask here as you guys rock when it comes to helping with stuff like this !
    again thanks for your time everyone and look forward to your hints and tips
    Last edited by phoenix2010; 08-13-2010 at 12:46 PM.

  2. #2
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: How to change a home page picture on each refresh / reload

    I know an easy way to do that with PHP.
    PHP Code:
    //path to the image directory
    $directory "images/";
     
    //get all image files with a .jpg extension.
    $images glob("" $directory "*.jpg");

    // get random image index
    $rand_img array_rand($images1);

    // display the image
    echo '<img src="'.$images[$rand_img].'" alt="" />'

  3. #3
    phoenix2010 is offline x10 Sophmore phoenix2010 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    241

    Re: How to change a home page picture on each refresh / reload

    firstly friend thanks for the response as im wasnt sure really how to get this kind of thing to work

    secondly i can see the php code uve posted up but how would i use it ?

    is the txt put into the page with the display as html?

    do i have to have the certain jpg,s i wanna use in 1 folder?

    sorry for being a pain its just im not used php that im aware of anyway ?

  4. #4
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: How to change a home page picture on each refresh / reload

    OK I'll try to explain it as simple as I can and if you want more you'll have to learn PHP. I will only help you make this particular peace of code work.

    PHP code is written inside of HTML. The only thing you'll have to do is rename the extension of your ".html" (or ".htm") file to ".php".

    PHP scripts have to be enclosed in "<?php ... ?>" tags where "..." is the actual PHP code. The above code should look like this:
    PHP Code:
    <?php
    //path to the image directory 
    $directory "http://x10hosting.com/forums/images/"
      
    //get all image files with a .jpg extension. 
    $images glob("" $directory "*.jpg"); 

    // get random image index 
    $rand_img array_rand($images1); 

    // display the image 
    echo '<img src="'.$images[$rand_img].'" alt="" />'
    ?>
    PHP code inside the HTML file will be executed on the server before it returns the result to the browser. This means that your browser will only see the HTML.

    The lats line in the script above (before "?>" closing tag) begins with the "echo" statement. This is the PHP output function (similar to print in other languages) that will inject any text into the HTML.

    Down to the script.
    "$directory" variable contains the path to the directory where you have your pics. This can be any folder on your site.

    $images is an array that contains filenames of all the pics in the specified folder that have ".jpg" extension. If you would like to have more then one extension (like .gif or .png) you just have to add them to the $images variable like so:

    PHP Code:
    $images glob("" $directory "*.jpg") + glob("" $directory "*.gif"); 
    $rand_img gets the random index form the array of pics.

    Finaly we have to inject a random image into our page. Wherever you put your "echo" function in your HTML that is where the the result will be injected. To understand it better take a look at the PHP "Halo World!" example below.

    File: halo_world.php
    PHP Code:
    <html>
    <head>
      <title>PHP Halo World</title>
    </head>

    <body>
      <h1><?php echo "Helo World!"?></h1>
    </body>

    </html>
    As you can see the "echo" function will inject the "Halo World!" text into the HTML wherever you put it. The result page (the one browser gets) would look like this:

    PHP Code:
    <html>
    <
    head>
      <
    title>PHP Halo World</title>
    </
    head>

    <
    body>
      <
    h1>Helo World!</h1>
    </
    body>

    </
    html
    In my script I echo-ed the "img" tag with "src" value containing the random image. You can put the code wherever you want your image to be displayed in your HTML page.

    I hope this was clear enough and that you will be able to use it. Just remember to rename your HTML file into PHP.
    Last edited by farscapeone; 08-15-2010 at 02:41 PM.

  5. #5
    phoenix2010 is offline x10 Sophmore phoenix2010 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    241

    Re: How to change a home page picture on each refresh / reload

    thank you so much for taking the time out and will have a play with this stuff later in the week
    Last edited by phoenix2010; 08-17-2010 at 12:46 AM.

  6. #6
    farscapeone's Avatar
    farscapeone is offline Community Advocate farscapeone is on a distinguished road
    Join Date
    Dec 2008
    Location
    Србија (Serbia)
    Posts
    1,166

    Re: How to change a home page picture on each refresh / reload

    Quote Originally Posted by phoenix2010 View Post
    ... as you guys rock when it comes to helping with stuff like this !
    How could I refuse to help when you put it like that

+ Reply to Thread

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2010, 09:11 PM
  2. change of home page
    By hari22_cs79 in forum Free Hosting
    Replies: 1
    Last Post: 06-08-2010, 11:44 PM
  3. CSS not loading on page refresh / reload
    By ninety in forum Free Hosting
    Replies: 0
    Last Post: 04-16-2009, 08:01 PM
  4. Replies: 7
    Last Post: 05-07-2008, 05:27 AM
  5. Can i change default home page extension?
    By shan1403 in forum Free Hosting
    Replies: 2
    Last Post: 04-30-2008, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers