?
Solved

Randomly rotate 5 different PHP header files.

Posted on 2005-03-11
8
Medium Priority
?
234 Views
Last Modified: 2010-04-17
I need to rotate 5 different PHP header files that are include files for a header of a website.
0
Comment
Question by:ct8270
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13521167
you want to include any of the 5 header file randomly ?
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13521273
<?
$folderPath = '/path/to/header_files';
if ($handle = opendir($folderPath)) {
   /* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) {
       $fileArray[] = $file;
   }
   closedir($handle);
}

shuffle($fileArray);

include "$fileArray[0]";

?>
0
 
LVL 4

Accepted Solution

by:
markdoc earned 2000 total points
ID: 13522648
Idbkutty's answer is best for any number of header files in a directory whose names are unknown to you, the programmer.  But a faster way would be:

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

$elements = count($arrHeaders);
include($includespath.$arrHeaders[rand(0, $elements - 1)]);

?>

That is, if you know the include files names' at hand.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 32

Expert Comment

by:ldbkutty
ID: 13522661
yes, thats more simple :) But there is shuffle() function that can be used rather than using count() and rand() functions.

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

shuffle($arrHeaders);
include($includespath . $arrHeaders[0]);

?>
0
 
LVL 4

Expert Comment

by:markdoc
ID: 13522726
Idbkutty, I used the rand() function because shuffle(), as its name suggests, should use *more* processing time than rand(), because rand() only gives you a random integer and not mess around with an array's elements. Although good for a small number of elements like 5, shuffle() would be much, much slower when dealing with larger arrays in general. ;)

Anyway, a *dangerous* optimization to my former post would be:

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

$elements = 5;   // Since you've already hard-coded the filenames why not hardcode this too?
include($includespath.$arrHeaders[rand(0, $elements - 1)]);

?>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13522819
yes, you are a keen programmer :)
0
 
LVL 4

Expert Comment

by:markdoc
ID: 13522946
Thanx Idbkutty, but your 54 K+ points makes you the more prolific one. ;)
0
 

Author Comment

by:ct8270
ID: 13536871
Worked perfectly! Thanks for all your help.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question