claschy Posted August 17, 2009 Share Posted August 17, 2009 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 More sharing options...
JonnoTheDev Posted August 17, 2009 Share Posted August 17, 2009 <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 https://forums.phpfreaks.com/topic/170659-solved-random-php-include-problem/#findComment-900165 Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 <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 https://forums.phpfreaks.com/topic/170659-solved-random-php-include-problem/#findComment-900273 Share on other sites More sharing options...
claschy Posted August 18, 2009 Author Share Posted August 18, 2009 Thank you! works great! Link to comment https://forums.phpfreaks.com/topic/170659-solved-random-php-include-problem/#findComment-900748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.