<?php
session_start();
/*
* if this is set to true then just the box number will be returned.
* if false then the contents of the referenced file will be returned.
* note that it would be better just to return the <img> tag dynamically formed...
*/
define ('DEBUG', true);
/*
* set this to the absolute path to the directory with the html within
*/
define ("BASE", 'path/to/my/directory/withhtml/');
/*
* if files are not named htmlFile_X.html where x is a number between 1 and 48, then set this value to TRUE
*/
define ("NON_STANDARD_FILE_NAMES", false); //if files are not named
/**
* this function takes the sequential number as an argument
* and either returns a dummied value for testing
* or the contents of the sequentially or dynamically obtained file name depending on the value
* of NON_STANDARD FILE NAMES
*
* @param object $fileNumber
* @return
*/
function getContent($fileNumber){
if (DEBUG) return $fileNumber;
if (NON_STANDARD_FILE_NAMES):
$files = loadFiles();
return file_get_contents($_SESSION['files'][$fileNumber]);
endif;
return file_get_contents(BASE. 'htmlFile_' . $fileNumber . '.html');
}
/**
* this function scans the directory BASE and stores the true file names in a static array
* only used if NON_STANDARD_FILE_NAMES is set to true.
* @return
*/
function loadFiles(){
if (empty($_SESSION['files'])){
$dir = opendir(BASE);
while (false !== ($file = readdir($dir))){
if ($file !== '.' && $file !== '..' && !is_dir(BASE . $file)){
$files[] = BASE . $file;
if (count($files) == 48) break; //limit to 48 files
}
}
$_SESSION['files'] = $files;
}
return $_SESSION['files'];
}
//what visit number are we?
$_SESSION['visit'] = empty($_SESSION['visit']) ? 1 : $_SESSION['visit'] + 1;
$sp = (($_SESSION['visit'] - 1) % 4); //calculate starting point for outer rotation (0 is top left)
$iSP = (($_SESSION['visit'] - 1) % 12); //(0 is top left of the inner rotation)
session_write_close(); // to avoid race conditions when testing locally
$fileNumber = 1;
for ($b=1; $b<=4; $b++){ //start the outer boxes
$box = ($b + $sp) >= 4 ? ($b + $sp) - 4 : $b + $sp;
$output[$box] = "<div class=\"box box_$box\">"; //create holding variables for the outer boxes
$innerBoxes = array();
$iB = 0; //counter
do { //start the inner boxes rotation
$c = getContent($fileNumber);
$bbb = $iB < $iSP ? $iB + 15 : $iB; //push the lower order boxes to the end
$innerBoxes[$bbb] .= "<div class=\"boxContent\">$c</div>";
$fileNumber++;
$iB++;
} while ( ($fileNumber - 1) % 12 !== 0);
ksort($innerBoxes); //reset the array to key ordered ASC
$output[$box] .= implode("\n", $innerBoxes) . "\n</div>";
}
ksort($output); //reset the outer boxes to an ordered array
//all done now
//just output needed
echo <<<CSS
<style type="text/css">
.boxContent{
float:left;
width: 30%;
border: black thin solid;
margin: 1px;
height:auto;
text-align:center;
}
.box{
border: black thin solid;
overflow:hidden;
}
.box_0{
float:left;
width:45%;
}
.box_1{
float:right;
width:45%;
}
.box_2{
clear: both;
float:left;
width:45%;
}
.box_3{
float:right;
width:45%;
}
</style>
CSS;
echo (implode("\n", $output));
?>