Help - Random images

This is a discussion on Help - Random images within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, I have a web page which is a simple html table whish contains a picture in one of the ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-21-2004
Tim Dixon
 
Posts: n/a
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


Reply With Quote
  #2 (permalink)  
Old 01-21-2004
php
 
Posts: n/a
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 www.offa.org for this in action

Regards

Jeff P.



"Tim Dixon" <ng@keymt.com> wrote in message
news:400e77f2$0$243$fa0fcedb@lovejoy.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
>
>



Reply With Quote
  #3 (permalink)  
Old 01-21-2004
Tom Thackrey
 
Posts: n/a
Default Re: Help - Random images


On 21-Jan-2004, "Tim Dixon" <ng@keymt.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
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:22 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0