Professional Web Applications Themes

Help - Random images - PHP Development

Hi, I have a web page which is a simple html table whish contains a picture in one of the cells. I want this to be one of 13 random pictures (1.jpg, 2.jpg....13.jpg) At the moment i have this line in my table.php file: <td colspan="2" rowspan="2" valign="top" bgcolor="#999999"><img src=" <? $random = mt_rand(1, 13); print"images/random/$random.jpg"; ?> " width="231" height="284" border="0" alt=""></td> .....everything works fine if I click refresh, but I want it to work everytime I go to this page. ie. first time on page i get say 4.jpg displayed, click refresh and say 8.jpg is displayed (working) - ...

  1. #1

    Default Help - Random images

    Hi,

    I have a web page which is a simple html table whish contains a picture in
    one of the cells. I want this to be one of 13 random pictures (1.jpg,
    2.jpg....13.jpg)

    At the moment i have this line in my table.php file:

    <td colspan="2" rowspan="2" valign="top" bgcolor="#999999"><img src="
    <?
    $random = mt_rand(1, 13);
    print"images/random/$random.jpg";
    ?>
    " width="231" height="284" border="0" alt=""></td>


    .....everything works fine if I click refresh, but I want it to work
    everytime I go to this page. ie. first time on page i get say 4.jpg
    displayed, click refresh and say 8.jpg is displayed (working) - if i click a
    link to say the homepage, then click a link from there back to 'table.php'
    then the same image is displayed. It is as if it is now cached.

    How do I force it to show a new pic every time?


    Thanks

    Tim


    Tim Dixon Guest

  2. #2

    Default Re: Help - Random images

    Use two scripts.

    In the first refer to the picture with a link like this

    <td colspan="2" rowspan="2" valign="top" bgcolor="#999999">
    <img src="randompic.php" width="231" height="284" border="0" alt="">
    </td>

    then create a script named randompic.php

    <?php
    $dir = "./images/random";
    $possible = array();
    if (is_dir($dir))
    {
    if ($dh = opendir($dir))
    {
    while (($file = readdir($dh)) !== false)
    if (stristr($file,".jpg")!= false) $possible[] = $file;
    closedir($dh);
    }
    }
    $x = rand(0, count($possible)-1);
    $file = $possible[$x];
    header("Content-Type: image/jpeg");
    readfile($dir . $file);
    ?>

    this will return a random image without causing the browser to cache it

    see [url]www.offa.org[/url] for this in action

    Regards

    Jeff P.



    "Tim Dixon" <ngkeymt.com> wrote in message
    news:400e77f2$0$243$fa0fcedblovejoy.zen.co.uk...
    > Hi,
    >
    > I have a web page which is a simple html table whish contains a picture in
    > one of the cells. I want this to be one of 13 random pictures (1.jpg,
    > 2.jpg....13.jpg)
    >
    > At the moment i have this line in my table.php file:
    >
    > <td colspan="2" rowspan="2" valign="top" bgcolor="#999999"><img src="
    > <?
    > $random = mt_rand(1, 13);
    > print"images/random/$random.jpg";
    > ?>
    > " width="231" height="284" border="0" alt=""></td>
    >
    >
    > ....everything works fine if I click refresh, but I want it to work
    > everytime I go to this page. ie. first time on page i get say 4.jpg
    > displayed, click refresh and say 8.jpg is displayed (working) - if i click
    a
    > link to say the homepage, then click a link from there back to 'table.php'
    > then the same image is displayed. It is as if it is now cached.
    >
    > How do I force it to show a new pic every time?
    >
    >
    > Thanks
    >
    > Tim
    >
    >

    php Guest

  3. #3

    Default Re: Help - Random images


    On 21-Jan-2004, "Tim Dixon" <ngkeymt.com> wrote:
    > I have a web page which is a simple html table whish contains a picture in
    > one of the cells. I want this to be one of 13 random pictures (1.jpg,
    > 2.jpg....13.jpg)
    >
    > At the moment i have this line in my table.php file:
    >
    > <td colspan="2" rowspan="2" valign="top" bgcolor="#999999"><img src="
    > <?
    > $random = mt_rand(1, 13);
    > print"images/random/$random.jpg";
    > ?>
    > " width="231" height="284" border="0" alt=""></td>
    >
    >
    > ....everything works fine if I click refresh, but I want it to work
    > everytime I go to this page. ie. first time on page i get say 4.jpg
    > displayed, click refresh and say 8.jpg is displayed (working) - if i click
    > a
    > link to say the homepage, then click a link from there back to 'table.php'
    > then the same image is displayed. It is as if it is now cached.
    >
    > How do I force it to show a new pic every time?
    If you are using PHP prior to version 4.2.0 you need to seed the mt_rand so
    that it doesn't generate the same sequence each time. Add

    function make_seed() {
    list($usec, $sec) = explode(' ', microtime());
    return (float) $sec + ((float) $usec * 100000);
    }
    mt_srand(make_seed());

    before the line that sets $random


    --
    Tom Thackrey
    [url]www.creative-light.com[/url]
    tom (at) creative (dash) light (dot) com
    do NOT send email to [email]jamesbutlerwillglen.net[/email] (it's reserved for spammers)
    Tom Thackrey Guest

Similar Threads

  1. Extension: Dynamic Images/Advance Random Images
    By Babri1 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 2
    Last Post: August 10th, 08:56 PM
  2. Random Images
    By sokaman webforumsuser@macromedia.com in forum Macromedia Dreamweaver
    Replies: 10
    Last Post: February 23rd, 12:34 PM
  3. Advanced Random Images
    By SuzyGol in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 0
    Last Post: February 10th, 04:01 PM
  4. Random Images on Mac?
    By wecklish in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 1
    Last Post: May 10th, 04:43 AM
  5. random images lingo
    By rozza1 webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 3
    Last Post: August 14th, 07:47 AM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139