Re: Random include???
| From: | Justin French | Date: | Fri, 20 Dec 2002 00:42:04 +0000 |
| Subject: | Re: Random include??? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-128927@lists.php.net to get a copy of this message | ||
Hi,
untested code adapted from a random image thing I have... this works by
checking the files in a directory and picking one at random.
<?
function getRandomFile($start_dir)
{
chdir($start_dir);
$dir = opendir('.');
while (($myfile = readdir($dir)) !==false)
{
if ($myfile != '.' && $myfile != '..' &&
is_file("$myfile") &&
$myfile != 'resource.frk')
{
$files[] = $myfile;
}
}
closedir($dir);
chdir('../');
srand ((float) microtime() * 10000000);
$file = array_rand($files);
return $files[$file];
}
$randomfile = getRandomFile('images_header');
?>
<TD WIDTH=77 HEIGHT=435 ROWSPAN=2 align="left">
<?include("include_directory/{$randomfile}");?>
</TD>
The other option is to keep all your include file paths in an array:
<?
$files = array(
'folio_1.php3',
'folio_2.php3',
'folio_3.php3',
'folio_4.php3',
'folio_5.php3'
)
$i = array_rand($files);
$randomfile = "include_directory/".$files[$i];
?>
<TD WIDTH=77 HEIGHT=435 ROWSPAN=2 align="left">
<? if(file_exists($randomfile))
{ include($randomfile); }
else
{ echo "file not found"; }
?>
</TD>
The difference between the two options is *obviously* a few more lines of
code for the first, BUT it lets you update the "random files list" just with
FTPing to the rand files directory, whereas option 2 requires you to FTP the
files AND update the $files array
Justin
on 20/12/02 1:45 AM, Benjamin Trépanier (mailing@cyclonemedias.com) wrote:
> Hi, I'm a newbie in php so sorry for that question!
>
> I have a table in a html dcc and I want to include a file in a cell, but
> that file must be random so people load a different page each time they
> refresh... The basic syntax I use for the include is below now, I want to
> know how to generate the random.
>
> Ex. My file are
>
> folio_1.php3
> folio_2.php3
> folio_3.php3
> folio_4.php3
> folio_5.php3
>
>
>
> <TD WIDTH=77 HEIGHT=435 ROWSPAN=2 align="left"><?
> include("folio.php3");
> ?></TD>
>
>
> Thanks!
> Ben
>