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
https://forums.phpfreaks.com/topic/170659-solved-random-php-include-problem/
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>

<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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.