1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need help with basic PHP scripting.

Discussion in 'PHP & Perl' started by Zoekban, Dec 8, 2014.

  1. Zoekban

    Zoekban Newbie

    Joined:
    Apr 23, 2013
    Messages:
    18
    Likes Received:
    0
    I've made recently a script for showing random pictures, The thing is that i want the script to show Random picture every specific time , like in 5 minutes, in 15 and in 30,
    How can it be done, And what do i need to add to my script to improve the script to be working by random timing?

    The script is being used in WordPress with the "allow php in post" plugin.

    The script is -


    $total = "2";
    $file_type = ".jpg";
    $image_folder = "(images source)";
    $start = "1";


    $random = mt_rand($start, $total);


    $image_name = $random . $file_type;


    echo "<img src=\"$image_folder/$image_name\" alt=\"$image_name\ />";


    Thanks from ahead, And waiting for a reply and help.
    Thank you.
     
  2. MrBlue

    MrBlue Senior Member

    Joined:
    Dec 18, 2009
    Messages:
    950
    Likes Received:
    662
    Occupation:
    Web/Bot Developer
    You'd be better served by using jQuery for something like this:
    Code:
    <img id="thisImg" alt="img" src="img0.png"/>
    
    
    <script type="text/javascript">
        $(function(){
    
            var imgArray=new Array();
            imgArray[0]="img1.png";
            imgArray[1]="img2.png";
            imgArray[2]="img3.png";
            imgArray[3]="img0.png";
    
            rand = Math.round(Math.random()*(300000-3600000))+3600000; // generate new time (between 5min and 1hour)
    
            window.setInterval(function(){
                randImg = imgArray[Math.floor(Math.random()*imgArray.length)];
                $('#thisImg').attr('src',randImg);
            },rand);        
        });
    </script>
    
     
    • Thanks Thanks x 1
    Last edited: Dec 9, 2014
  3. Zoekban

    Zoekban Newbie

    Joined:
    Apr 23, 2013
    Messages:
    18
    Likes Received:
    0
    And how I can arrange this script with the one I have right now?
    I'm sorry for my questions, But I'ts all new for me, And i will be grateful for all the help.
     
  4. pr250

    pr250 Junior Member

    Joined:
    Apr 7, 2010
    Messages:
    108
    Likes Received:
    23
    Can you explain a little more?

    Do you want to just include this in one post, and have the image constantly change, or do you want that, every 5 minutes, your script posts a new blog post with a new image?
     
  5. Zoekban

    Zoekban Newbie

    Joined:
    Apr 23, 2013
    Messages:
    18
    Likes Received:
    0
    The first script, Changes the image randomly every time you refresh the page.
    I want to make that, To be changed to randomly image, But not on every refresh, But every couple of minutes, for example every 5 minutes
     
  6. Zoekban

    Zoekban Newbie

    Joined:
    Apr 23, 2013
    Messages:
    18
    Likes Received:
    0
    Anybody with answer?