Current location: Hot Scripts Forums » Programming Languages » PHP » Random PHP include


Random PHP include

Reply
  #1 (permalink)  
Old 08-11-04, 07:02 PM
brduran brduran is offline
New Member
 
Join Date: Aug 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Random PHP include

Random PHP include

I've had a look at all the scripts under 'random', 'random php', and 'random php include', and none of them offer you the versatility of picking up random documents and include them 'php includes'. What can we do guys?, I know not much about 'php writing', but I would love to get my hands on something like it.

thank you!
Reply With Quote
  #2 (permalink)  
Old 08-11-04, 09:12 PM
kvnband kvnband is offline
Wannabe Coder
 
Join Date: Jun 2003
Posts: 242
Thanks: 0
Thanked 0 Times in 0 Posts
Easiest way would probably be to make an array of all of the filenames

$array[1] = 'filename.txt';
$array[2] = 'filename2.html';

Then do a count on the array

$num = count($array);

Then pick a random number between 0 and $num

$random = rand(0, $num);

Then include the filename listed at $array[$random]

include "$array[$random]";

I'm not great, but that should get you started (until infinity comes along with a better solution

Kevin
Reply With Quote
  #3 (permalink)  
Old 08-12-04, 04:57 PM
Acecool's Avatar
Acecool Acecool is offline
Aspiring Coder
 
Join Date: Nov 2003
Posts: 506
Thanks: 0
Thanked 0 Times in 0 Posts
if you do that, then use
$array = array();
$array[] 'fileone.txt';
$array[] 'filetwo.txt';

and so on, no need to put anything inside the blockquotes.

then count the array, and rand from 0 to the number..
__________________
Check Acecoolco.com for PHP Tutorials, and other tuts
If you plan on contacting me, please read this: Legal Terms & Conditions
Reply With Quote
  #4 (permalink)  
Old 08-13-04, 08:46 PM
bugalyzer bugalyzer is offline
Newbie Coder
 
Join Date: Apr 2004
Posts: 86
Thanks: 0
Thanked 0 Times in 0 Posts
script

why not make it practical .. given a directory 'some/dir' - heres how to include a random file that is in that directory -- this way theres no need to ever modify the script when you add new files:

$i=0;
$myDirectory = dir("some/dir");
while($file=$myDirectory->read())
{
$array[$i]=$file;
$i++;
}
$myDirecotry->close();

$num = count($array);
$random = rand(0, $num);

include "$array[$random]";

-----------------------
http://buildacom.com
Reply With Quote
  #5 (permalink)  
Old 08-16-04, 06:34 AM
MrKafaaski's Avatar
MrKafaaski MrKafaaski is offline
Newbie Coder
 
Join Date: Jul 2004
Location: Hurum, Norway
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php

srand
();
$files = array("file1.html""file2.txt""file3.php""file4.asp");
$rand array_rand($files);
include (
$files[$rand]);
?>
..Also works on win-servers.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Include CGI Script in PHP ivan PHP 3 12-22-10 08:14 PM
Php Include nitefire PHP 4 07-23-04 12:43 PM
PHP include on vBulletin Matrix28 PHP 2 02-25-04 12:29 AM
Include asp-generated html in php rancor PHP 2 08-05-03 03:03 AM
Random Table PHP dougg PHP 5 07-20-03 12:41 PM


All times are GMT -5. The time now is 04:19 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.