Jump to content

[SOLVED] random php include problem


claschy

Recommended Posts

Hi

 

Thanks in advance for any help with this.

 

Basicly, I've created 6 .php include files that I want to add to my website. I want to call 3 of these boxes into a space and randomise them, so that they will change whenever the user refreshes the page/revisits.

 

So to include one random box, I used;

<div class="banner">
<?php $pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php'); $result = rand(0, count($pages) - 1); include ($pages[$result]);?>
</div>

 

Therefore the question is, how do get 3 of the boxes included.. without duplicates, so all 3 are different on the load? ie. 2 of 'seconds.php' won't show together in the 3.

 

I tried:

<div class="banner">
<?php $pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php'); $result = rand(0, count($pages) - 1); include_once($pages[$result]);?>
<?php $pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php'); $result = rand(0, count($pages) - 1); include_once($pages[$result]);?>
<?php $pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php'); $result = rand(0, count($pages) - 1); include_once($pages[$result]);?>
</div>

 

and this works great.. sometimes.. but it doesn't always include 3, sometimes 1 or 2..

 

Any advice? I'd be very grateful!

 

Thanks

 

Claire

Link to comment
Share on other sites

<div class="banner">
<?php 
$pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php');
$keys  = array_rand($pages,3);
foreach($keys as $key) {
include($pages[$key]);
}
?>
</div>

Link to comment
Share on other sites

<div class="banner">
<?php 
$pages = array('includes/day.php', 'includes/seconds.php', 'includes/workshop.php', 'includes/road.php', 'includes/power.php', 'includes/outsourced.php');
$keys  = array_rand($pages,3);
foreach($keys as $key) {
include($pages[$key]);
}
?>
</div>

 

Nice. I won't be using it for this particular purpose, but good to remember.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.