www.webdeveloper.com
Results 1 to 4 of 4

Thread: Please help with my JavaScript random image from folder

  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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.36734 seconds
  • Memory Usage 2,899KB
  • Queries Executed 15 (?)
More Information
Template Usage (34):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_code
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (4)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (4)postbit
  • (4)postbit_onlinestatus
  • (4)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates