Randomizing php includes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
M3lvin
Forum Newbie
Posts: 6
Joined: Fri Jul 11, 2008 3:59 pm

Randomizing php includes

Post by M3lvin »

Hey all.

OK, I'm trying to figure out the simplest way to facilitate the random selection and loading of a set of php include files. In other words, with each new page load or refresh a different php include file gets called and loaded in the same HTML container.

I've attempted the following: using the ubiquitous rotate.php image rotation script as an include file (after configuring it to select only php files) so that it in turn randomly selects a standard php include file from among several within a given folder, which then does its thing. Something like this:

Code: Select all

<?php include('rotate.php'); ?> -----> 'random_include_file.php' ----->  <random html content>
as opposed to this:

Code: Select all

<?php include('specific_include_file.php'); ?> ----->  <specific html content>
which would obviously ouput the same content every time.

It kinda works but it's replete with header errors (Warning: Cannot modify header information - headers already sent by....) and sometimes code from rotate.php itself gets loaded, I've tried several different combinations but cannot seem to get rid of the errors.

So I'd like to know if what I'm attempting is even possible or am I just wasting my time. I realize I can easily accomplish what I want by using rotate.php with html files and iframes but I'd much rather prefer to use php includes if possible, it makes for neater code.

Thanks. :)
koen.h
Forum Contributor
Posts: 268
Joined: Sat May 03, 2008 8:43 am

Re: Randomizing php includes

Post by koen.h »

This image rotation scripts sets a header which creates the error. You won't need anything starting from 'if ($img!=null) {
'.
M3lvin
Forum Newbie
Posts: 6
Joined: Fri Jul 11, 2008 3:59 pm

Re: Randomizing php includes

Post by M3lvin »

OK, I took that section out and now nothing shows up, there's a blank spot in the source code.

It's starting to look more and more like this isn't going to happen, I don't think the include function can work with an intermediary step. True?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Randomizing php includes

Post by Benjamin »

This question was already answered recently. I recommended placing the values into an array and using the array_rand function.

http://us2.php.net/manual/en/function.array-rand.php
M3lvin
Forum Newbie
Posts: 6
Joined: Fri Jul 11, 2008 3:59 pm

Re: Randomizing php includes

Post by M3lvin »

Thanks, but that doesn't seem to work with include files, keep getting parse errors.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Randomizing php includes

Post by Eran »

It has nothing to do with include. What astions suggested is something like this:

Code: Select all

 
$files = array(
    0 => 'file1.php',
    1 => 'file2.php',
    2 => 'file3.php'
);
$index = array_rand($files);
include($files[$index]);
 
 
M3lvin
Forum Newbie
Posts: 6
Joined: Fri Jul 11, 2008 3:59 pm

Re: Randomizing php includes

Post by M3lvin »

Well, that certainly looks promising, do I insert it into the page or save to file and call it with an include function? (sorry, still learning :oops:)
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Randomizing php includes

Post by Eran »

Put in the file, replacing the previous include statement. If you want more specific help, show us the code you are trying to work with.
M3lvin
Forum Newbie
Posts: 6
Joined: Fri Jul 11, 2008 3:59 pm

Re: Randomizing php includes

Post by M3lvin »

OK, got things working, thank you so much! :drunk:
Post Reply