www.webdeveloper.com
Results 1 to 4 of 4

Thread: Please help with my JavaScript random image from folder

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    6

    Question Please help with my JavaScript random image from folder

    Hi, i am working on a website and it is basically will revolve around pictures. So far i am still working on the site and at the moment i have about 400 pictures and counting i wish to put them all up in different parts of the site. Ok so i search online and here and found that many people use javascript codes like this
    myimages[1]="image1.png"
    myimages[2]="image2.png"
    myimages[3]="image3.png"
    and so on

    But since i have many pictures i am not sure if doing it this way will make my home page very crowded so i decided to look for javascript that display random images everytime the page is refreshed. I found some now i found one and it works great the problem is that when i try and display three images using t he command <img src="http://mysite.com/RandIm/randim.php"/> all three display the same and i want the three to not repeat the same image. For example. lets say i put image 1, 2 , 3, 4 , and 5 and put the code three times when i preview my page this is how it looks.

    1 1 1 * refresh* 3 3 3 instead of being for example 1 5 3 *refresh* 2 1 4.

    I looked at the read me text cant figured it out i worked on it for a 2 weeks am a bit frustrated. If anyone can help me with this i would really appreciate it or if anyone can suggest anything that would be easy for me that would be even better. Here is the code i have at the moment.

    PHP Code:
    <?php
    /*******************************************************************************
    *  Title: Random image script (RandIm)
    *  Version: 1.0 @ January 3, 2009
    *  Author: Klemen Stirn
    *  Website: http://www.phpjunkyard.com
    ********************************************************************************
    *  COPYRIGHT NOTICE
    *  Copyright 2009 Klemen Stirn. All Rights Reserved.
    *
    *  This script may be used and modified free of charge by anyone
    *  AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
    *  By using this code you agree to indemnify Klemen Stirn from any
    *  liability that might arise from it's use.
    *
    *  If you are using this script you are required to place a link
    *  to PHPJunkyard on your website. You will find some link suggestions here:
    *  http://www.phpjunkyard.com/link2us.php
    *
    *  Selling the code for this program, in part or full, without prior
    *  written consent is expressly forbidden.
    *
    *  Obtain permission before redistributing this software over the Internet
    *  or in any other medium. In all cases copyright and header must remain
    *  intact. This Copyright is in full effect in any country that has
    *  International Trade Agreements with the United States of America or
    *  with the European Union.
    *******************************************************************************/

    /*******************************************************************************
    *  SETTINGS
    *
    *  See readme.htm file for further instructions!
    *******************************************************************************/

    /* The default folder with images */
    $settings['img_folder'] = 'images/';

    /* File types (extensions) to display */
    $settings['img_ext'] = array('.jpg','.gif','.png');

    /*
       How to display the images?
       0 = print just the image path (for includes), like: images/test.jpg
       1 = redirect to the image, when using: <img src="randim.php" />
    */
    $settings['display_type'] = 1;

    /* Allow on-the-fly settings override? 0 = NO, 1 = YES */
    $settings['allow_otf'] = 1;

    /*******************************************************************************
    *  DO NOT EDIT BELOW...
    *
    *  ...or at least make a backup before you do!
    *******************************************************************************/

    /* Override type? */
    if ($settings['allow_otf'] && isset($_GET['type']))
    {
        
    $type intval($_GET['type']);
    }
    else
    {
        
    $type $settings['display_type'];
    }

    /* Override images folder? */
    if ($settings['allow_otf'] && isset($_GET['folder']))
    {
        
    $folder htmlspecialchars(trim($_GET['folder']));
        if (!
    is_dir($folder))
        {
            
    $folder $settings['img_folder'];
        }
    }
    else
    {
        
    $folder $settings['img_folder'];
    }

    /* Make sure images fodler ends with an '/' */
    if (substr($folder,-1) != '/')
    {
        
    $folder.='/';
    }

    /* Get a list of all the image files */
    $flist = array();
    foreach(
    $settings['img_ext'] as $ext)
    {
        
    $tmp glob($folder.'*'.$ext);
        if (
    is_array($tmp))
        {
            
    $flist array_merge($flist,$tmp);
        }
    }

    /* If we have any images choose a random one, otherwise select the "noimg.gif" image */
    if (count($flist))
    {
        
    $src $flist[array_rand($flist)];
    }
    else
    {
        
    $src 'noimg.gif';
    }

    /* Output the image according to the selected type */
    if ($type)
    {
        
    header('Location:'.$src);
        exit();
    }
    else
    {
        echo 
    $src;
    }
    ?>
    this is the code i use to call the image

    PHP Code:
    <img src="http://cometprint.com/RandIm/randim.php"/> 
    thank you in advance.

  2. #2
    Join Date
    Aug 2010
    Posts
    6
    Okay i have been experimenting with this but doesnt seem to work at all. Maybe someone here can help me.

    I saved this as picklink.js

    Code:
    <script language="JavaScript"><!--
    
    //Javascript Created by Computerhope http://www.computerhope.com
    
    //store the quotations in arrays
    
    images = new Array(4);
    
    images[0] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner.gif' alt='Visit Computer Hope'></a>";
    
    images[1] = "<a href = 'http://www.computerhope.com/history/index.htm'><img src='http://www.computerhope.com/banners/banner2.gif' alt='Computer History'></a>";
    
    images[2] = "<a href = 'http://www.computerhope.com/index.htm'><img src='http://www.computerhope.com/banners/banner3.gif' alt='Visit Computer Hope'></a>";
    
    images[3] = "<a href = 'http://www.computerhope.com/newslet.htm'><img src='http://www.computerhope.com/banners/banner4.gif' alt='Computer Hope Newsletter'></a>";
    
    index = Math.floor(Math.random() * images.length);
    
    document.write(images[index]);
    
    //done
    
    // --></script>
    and then on the main page i am trying to call the javascript with this:

    Code:
    <script type="text/javascript" src="piclink.js"></script>
    Doesnt seem to be working any help please.

    thank you in advance

  3. #3
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    At least 98% of internet users' DNA is identical to that of chimpanzees

  4. #4
    Join Date
    Aug 2010
    Posts
    6
    Thank you so very very much is exactly what i needed. BY any chance is there a way to add links to each image that appears?

    Thank you in advance

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles