473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Random include() help

57
I have some code that will randomly load an image from an array, but I want to tweak it to be able to randomly choose a php file and "include()" it in my page.

Here's the original code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file_array = array
  3. ('http://www.samplesite.com/images/image1.jpg',
  4. 'http://www.samplesite.com/images/image2.jpg',
  5. 'http://www.samplesite.com/images/image3.jpg',
  6. 'http://www.samplesite.com/images/image4.jpg',
  7. 'http://www.samplesite.com/images/image5.jpg');
  8. $total = count($file_array);
  9. $random = (mt_rand()%$total);
  10. $file = "$file_array[$random]";
  11. print("<img src=$file></a>");
  12. ?>
  13.  
And here's my tweaked version that gave me an error:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $file_array = array
  3. ('http://www.samplesite.com/samplefile1.php',
  4. 'http://www.samplesite.com/samplefile2.php',
  5. 'http://www.samplesite.com/samplefile3.php',
  6. 'http://www.samplesite.com/samplefile4.php',
  7. 'http://www.samplesite.com/samplefile5.php');
  8. $total = count($file_array);
  9. $random = (mt_rand()%$total);
  10. $file = "$file_array[$random]";
  11. include('$file');
  12. ?>
  13.  
I'm pretty new to php, so any help is greatly appreciated. Thanks in advance!
Jun 11 '07 #1
1 1277
Purple
404 Expert 256MB
Hi

[PHP]<?php
$file_array = array
('http://www.samplesite.com/samplefile1.php',
'http://www.samplesite.com/samplefile2.php',
'http://www.samplesite.com/samplefile3.php',
'http://www.samplesite.com/samplefile4.php',
'http://www.samplesite.com/samplefile5.php');
$total = count($file_array);
$random = (mt_rand()%$total);
$file = $file_array[$random];
include($file);
?>[/PHP]

try that..

Purple
Jun 11 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Acacia | last post by:
How would you generate a random number in C++?
1
by: John C | last post by:
Hi, I am trying to include the generation of random numbers in my c++ class. However I don't quite know how to incorporate it. To start with, I managed to get random numbers going via the...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
15
by: John Cassidy | last post by:
This has been driving me crazy. I've done basic C in school, but my education is mainly based on object oriented design theory where Java is our tool. For some reason, while helping a friend with a...
9
by: L33VaNcL33F | last post by:
I like to make a javascript that generate random number from the range number within : (10016486 and 99999985). the number always + 22423 that begin from 10016486 and end at 99999985 Example...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
1
by: ligong.yang | last post by:
Hi all, I got tortured by a very weird problem when I was using k. wilder's random generator class in my program. PS: wilder's generator class can be found at...
12
by: Juha Nieminen | last post by:
I'm trying to use a random number generator and rand() is just not up to the task (RAND_MAX is way too small). I searched in the help system and found the System::Random class which seems to be...
3
by: Army1987 | last post by:
Is there anything wrong with this program? It seems to behave strangely if I give stdin EOF when asked for the character set... /* BEGIN pwdgen.c */ #include <stdio.h> #include "random.h"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.