Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rotating SSI with PHP

Status
Not open for further replies.

egims

Technical User
Jun 5, 2001
96
US
I just thought I'd ask. I'm a PHP novist at best and can't figure out how to do what I think is a simple thing with PHP.

The best way to describe what I'm trying to do is like this:

Lets say you took a web page and divided it up into 4 equal squares. Then had the HTML content for each square inserted into the page via a server side include.

Here is what I need to do. Each time the page is visited I want the page content for each square (the server side include) to rotate clockwise. That's it.

So the SSI that placed content into the top left now goes into the top right. The content that was in the top right now moves to the bottom right, and so on for all 4 squares.

If anyone can help me with the code to do this I would appreciate it greatly.
 
and just in case I did misunderstand and you want both inner and outer boxes rotating here is some code that will do it. read the comments carefully

Code:
<?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));
?>
 
Hi

jpadie is right, would be better to use a file naming which to include the order numbers. The following code supposes the use of 4 groups ( 0..3 ) each having 12 images ( 0..11 ), so the file names would be file-0-0.htm to file-3-11.htm. This is a single level inclusion.
PHP:
[teal]<?php[/teal]

[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]file_exists[/color][teal]([/teal][green][i]'topleft.txt'[/i][/green][teal]))[/teal] [navy]$topleft[/navy][teal]=[/teal][COLOR=darkgoldenrod]file_get_contents[/color][teal]([/teal][green][i]'topleft.txt'[/i][/green][teal]);[/teal]
[b]else[/b] [navy]$topleft[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal]

[COLOR=darkgoldenrod]file_put_contents[/color][teal]([/teal][green][i]'topleft.txt'[/i][/green][teal],([/teal][navy]$topleft[/navy][teal]+[/teal][purple]1[/purple][teal])[/teal][navy]%12[/navy][teal]);[/teal]

[teal]?>[/teal]
[teal]<![/teal]DOCTYPE HTML PUBLIC [green][i]"-//W3C//DTD HTML 4.01//EN"[/i][/green] [green][i]"[URL unfurl="true"]http://www.w3.org/TR/html4/strict.dtd"[/URL][/i][/green][teal]>[/teal]
[teal]<[/teal]html lang[teal]=[/teal][green][i]"en"[/i][/green][teal]>[/teal]

[teal]<[/teal]head[teal]>[/teal]
[teal]<[/teal]meta http[teal]-[/teal]equiv[teal]=[/teal][green][i]"Content-Type"[/i][/green] content[teal]=[/teal][green][i]"text/html; charset=iso-8859-1"[/i][/green][teal]>[/teal]
[teal]<[/teal]title[teal]>[/teal]Rotating text test[teal]</[/teal]title[teal]>[/teal]
[teal]</[/teal]head[teal]>[/teal]

[teal]<[/teal]body[teal]>[/teal]

[teal]<[/teal]table[teal]>[/teal]

[teal]<?php[/teal]

[navy]$data[/navy][teal]=[/teal][b]array[/b][teal]();[/teal]

[b]for[/b] [teal]([/teal][navy]$group[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal][navy]$group[/navy][teal]<[/teal][purple]4[/purple][teal];[/teal][navy]$group[/navy][teal]++)[/teal]
  [b]for[/b] [teal]([/teal][navy]$image[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal][navy]$image[/navy][teal]<[/teal][purple]12[/purple][teal];[/teal][navy]$image[/navy][teal]++)[/teal]
    [navy]$data[/navy][teal][[/teal][COLOR=darkgoldenrod]floor[/color][teal]([/teal][navy]$group[/navy][teal]/[/teal][purple]2[/purple][teal])*[/teal][purple]3[/purple][teal]+[/teal][COLOR=darkgoldenrod]floor[/color][teal]([/teal][navy]$image[/navy][teal]/[/teal][purple]4[/purple][teal])].=[/teal][green][i]'<td>'[/i][/green][teal].[/teal][COLOR=darkgoldenrod]file_get_contents[/color][teal]([/teal][green][i]'file-'[/i][/green][teal].(([/teal][navy]$group[/navy][teal]+[/teal][navy]$topleft[/navy][teal])[/teal][navy]%4[/navy][teal]).[/teal][green][i]'-'[/i][/green][teal].(([/teal][navy]$image[/navy][teal]+[/teal][navy]$topleft[/navy][teal])[/teal][navy]%12[/navy][teal]).[/teal][green][i]'.htm'[/i][/green][teal]).[/teal][green][i]'</td>'[/i][/green][teal];[/teal]

[b]foreach[/b] [teal]([/teal][navy]$data[/navy] [b]as[/b] [navy]$one[/navy][teal])[/teal] [b]echo[/b] [green][i]"<tr>$one</tr>\n"[/i][/green][teal];[/teal]

[teal]?>[/teal]

[teal]</[/teal]table[teal]>[/teal]

[teal]</[/teal]body[teal]>[/teal]
[teal]</[/teal]html[teal]>[/teal]
Note that I used a [tt]table[/tt] just to make it easier to you to follow the structure, but I do not encourage the use of [tt]table[/tt] based layout.


Feherke.
 
jpadie & feherke,

Thank you both. It appears that there are some excellent solutions. I really appreciate the help.

It is amazing, but in seeking this solution I found 100's of rotating banner programs that all do basically the same thing with minor differences. But absolutely NO scripts that have the ability to rotate blocks of code on a page such as these solutions will do. And I can think of many applications for this solution(rotating blocks of code).

Have a great day my friends, and thank you.

Ed
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top