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 SkipVought 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
0
0
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.
 
Hi

egims said:
I'm a PHP novist
egims in the JavaScript forum said:
I'm a Javascript novist
Then why not choose something you are experienced with ?

Anyway, you could easily rewrite the SSI solution I posted in thread216-1592064 into PHP. Try to do some work, then ask if you have problem.


Feherke.
 
Hi

Based on further discussion in thread216-1592064 :
PHP:
[teal]<?php[/teal]

[navy]$piece[/navy][teal]=[/teal][b]array[/b][teal]([/teal][green][i]'one'[/i][/green][teal],[/teal][green][i]'two'[/i][/green][teal],[/teal][green][i]'three'[/i][/green][teal],[/teal][green][i]'four'[/i][/green][teal]);[/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]3[/purple][teal];[/teal]

[COLOR=darkgoldenrod]file_put_contents[/color][teal]([/teal][green][i]'topleft.txt'[/i][/green][teal],[/teal][navy]$topleft[/navy][teal]=([/teal][navy]$topleft[/navy][teal]+[/teal][purple]1[/purple][teal])[/teal][navy]%4[/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]Rotate[teal]</[/teal]title[teal]>[/teal]
[teal]</[/teal]head[teal]>[/teal]

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

[teal]<[/teal]div id[teal]=[/teal][green][i]"topleft"[/i][/green][teal]>[/teal]
[teal]<?php[/teal] [COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$piece[/navy][teal][[/teal][navy]$topleft[/navy][teal]].[/teal][green][i]'.htm'[/i][/green][teal]);[/teal] [teal]?>[/teal]
[teal]</[/teal]div[teal]>[/teal]

[teal]<[/teal]div id[teal]=[/teal][green][i]"topright"[/i][/green][teal]>[/teal]
[teal]<?php[/teal] [COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$piece[/navy][teal][[/teal][navy]$topleft[/navy][teal]=([/teal][navy]$topleft[/navy][teal]+[/teal][purple]1[/purple][teal])[/teal][navy]%4[/navy][teal]].[/teal][green][i]'.htm'[/i][/green][teal]);[/teal] [teal]?>[/teal]
[teal]</[/teal]div[teal]>[/teal]

[teal]<[/teal]div id[teal]=[/teal][green][i]"bottomleft"[/i][/green][teal]>[/teal]
[teal]<?php[/teal] [COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$piece[/navy][teal][[/teal][navy]$topleft[/navy][teal]=([/teal][navy]$topleft[/navy][teal]+[/teal][purple]1[/purple][teal])[/teal][navy]%4[/navy][teal]].[/teal][green][i]'.htm'[/i][/green][teal]);[/teal] [teal]?>[/teal]
[teal]</[/teal]div[teal]>[/teal]

[teal]<[/teal]div id[teal]=[/teal][green][i]"bottomright"[/i][/green][teal]>[/teal]
[teal]<?php[/teal] [COLOR=darkgoldenrod]readfile[/color][teal]([/teal][navy]$piece[/navy][teal][[/teal][navy]$topleft[/navy][teal]=([/teal][navy]$topleft[/navy][teal]+[/teal][purple]1[/purple][teal])[/teal][navy]%4[/navy][teal]].[/teal][green][i]'.htm'[/i][/green][teal]);[/teal] [teal]?>[/teal]
[teal]</[/teal]div[teal]>[/teal]

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

[teal]</[/teal]html[teal]>[/teal]

Feherke.
 
Hi Feherke.

THIS IS FANTASTIC! Really!

One question...

Is the above code based on a scenerio where we are using includes named one.htm, two.htm, three.htm, four.htm

You really have no idea of how much I appreciate your help.

Ed
 
Perfect. Thank you again.

Problem solved.
 
feherke,

If I may trouble you one more time. I don't seem to be able to get the code to function. I'm sure it is something I have done wrong, but I don't know what.

I have created the include files: one.htm, two.htm, three.htm, and four.htm

The HTML code is as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<html lang="en">

<head>
<title>Rotating text test</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<link rel="stylesheet" type="text/css" href="ptl.css">
</head>

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">

<!-- Begin Code Rotation Script -->
<?php

$piece=array('one','two','three','four');

if (file_exists('topleft.txt')) $topleft=file_get_contents('topleft.txt');
else $topleft=3;

file_put_contents('topleft.txt',$topleft=($topleft+1)%4);

?>
<!-- End Code Rotation Script -->


<!-- Begin Page Container -->
<table align="center" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<td align="center" valign="middle" width="100%" height="100%">

<!-- Begin Page Header -->
<div class="header">
<table width="954" height="55" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="954" height="40" bgcolor="#ffffff" colspan="3"><img src="images/header.jpg" width="954" height="40"></td>
</tr>
<tr>
<td width="954" height="15" bgcolor="#990033" colspan="3"><img src="images/shim.gif" width="954" height="15"></td>
</tr>
</table>
</div>
<!-- End Page Header -->


<!-- Begin Page Content -->
<table width="962" height="640" border="0" cellpadding="0" cellspacing="0">
<tr>
<!-- Box 1 -->
<div id="topleft">
<?php readfile($piece[$topleft].'.htm'); ?>
</div>
<!-- End Box 1 -->

<td width="2" height="320" bgcolor="#006600"><img src="images/shim.gif" width="2" height="320" border="0"></td>

<!-- Box 2 -->
<div id="topright">
<?php readfile($piece[$topleft=($topleft+1)%4].'.htm'); ?>
</div>
<!-- End Box 2 -->

</tr>
<tr>
<td width="962" height="2" bgcolor="#006600" colspan="3"><img src="images/shim.gif" width="962" height="2" border="0"></td>
</tr>
<tr>

<!-- Box 3 -->
<div id="bottomleft">
<?php readfile($piece[$topleft=($topleft+1)%4].'.htm'); ?>
</div>
<!-- End Box 3 -->

<td width="2" height="320" bgcolor="#006600"><img src="images/shim.gif" width="2" height="320" border="0"></td>

<!-- Box 4 -->
<div id="bottomright">
<?php readfile($piece[$topleft=($topleft+1)%4].'.htm'); ?>
</div>
<!-- End Box 4 -->

</tr>
</table>
<!-- End Page Content -->

</td>
</table>
<!-- End Page Container -->

</body>
</html>


What do the div ID's reference?

Thanks.
 
Hi

egims said:
I don't seem to be able to get the code to function.
And what you get instead ?
[ul]
[li]Nothing ?[/li]
[li]Incorrect behavior ?[/li]
[li]Error message ? ( If yes, please specify. )[/li]
[/ul]
If you mean that the included contents are displayed above your [tt]table[/tt] and not inside it, that is because your invalid markup. The [tt]div[/tt]s can not appear in the [tt]tr[/tt] tags, so move them into the [tt]td[/tt] tags.
In meantime a quick checklist :
[ul]
[li]Are you loading the file through the web server ( [tt]http:[/tt] protocol ) ? ( Sorry, had to ask. )[/li]
[li]Is your web server configured to interpret PHP code in the file you posted above ?[/li]
[li]Is your PHP version 4.3.0 or newer ?[/li]
[li]Has the web server's user permission to write in the directory where your script resides ?[/li]
[/ul]
egims said:
What do the div ID's reference?
The [tt]div[/tt]s are used just as containers. As you use [tt]table[/tt] based layout, you probably not need the [tt]div[/tt]s. ( Note that using [tt]table[/tt]s for layout is deprecated practice. ) The [tt]id[/tt]s are just to label the containers.
egims said:
<link rel="stylesheet" type="text/css" href="ptl.css">

<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
That looks plain stupid. You are using CSS, but you still adjust the appearance with attributes. ( Note that topmargin, leftmargin, marginwidth and marginheight never existed in the HTML standard. )

Feherke.
 
Hello, and thanks.

The page does load, however, minus the includes. There are no error messages. Just a large empty block where the includes would otherwise display.

Origonally I was attempting to load the page in a subdirectory. After that I decided to attempt loading the page in the root directory. However, the result both times were exactly the same. I also tried setting the permissions to 777, but that did not work so I imagine it is not a permission issue.

Yes, the server is configured to run PHP, interpret PHP in the file posted above, and the version installed is 5.2.11

"That looks plain stupid. You are using CSS, but you still adjust the appearance with attributes."

Thanks you for pointing that out...you are correct. I need to improve my coding a bit.
 
feherke,

I have the code working perfectly. As suspected it was an error I was making. I failed to name the file with a PHP extension!!!!!!!!!!!!!!!!

Thank you again for all your help. You are a credit to these forums.

Ed
 
feherke, If you are still monitoring this topic I was wondering if I could ask you one more question about the code you gave me?

Working perfectly, your code allows me to use 4 include files, and then for them to rotate with each site visit, or screen refersh. Now, I wanted to take that one step further...

My thought was to do a simular thing with 12 images (banners) within each of the 4 blocks, which are rotating around the screen. I created the following code, based on your code:

<?php

$grouptwo=array('twoone','twotwo','twothree','twofour','twofive','twosix','twoseven','twoeight','twonine','twoten','twoeleven','twotwelve');
if (file_exists('grouptwotopleft.txt')) $grouptwotopleft=file_get_contents('grouptwotopleft.txt');
else $grouptwotopleft=11;
file_put_contents('grouptwotopleft.txt',$grouptwotopleft=($grouptwotopleft+1)%12);

?>

This above code I place into the HTML page just below the simular code that you wrote for me.

Then, into one of the include files (one of the 4 rotating blocks) I placed this:

<td width="480" height="320" align="center" valign="top"><div class="content">
<table width="480" height="320" border="0" cellpadding="0" cellspacing="0">
<tr>
<?php readfile($grouptwo[$grouptwotopleft].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
</tr>
<tr>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
</tr>
<tr>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
</tr>
<tr>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
<?php readfile($grouptwo[$grouptwotopleft=($grouptwotopleft+1)%12].'.htm'); ?>
</tr>
</table></div></td>

The .htm include files contain the images.

The result:

The first part of the code is working in that refreshes are being logged into "grouptwotopleft.txt", however the include files are NOT being added to the page.

I can't figure out why, but feel confident that you will probably know why. Hope you are still here, and thanks.
 
Hi

Being here is not enough. :-( So far I not understand the new requirement.

First you are talking about "12 images", but later you are still using [tt]readfile()[/tt] on files with .htm extension.

So the first thing to make clear, are those images or text files ?


Feherke.
 
Hi feherke,

Well, yes on both counts. What I did in the later case was refer to .htm files (12 .htm file includes), each of which contain the image code.

I'm sure there is probably a better way, but.....

I am trying to place these includes into the 4 rotating blocks that you already have working for me.
 
Hi

Ok, so there is no image. For PHP the text is just text even if it contains HTML with [tt]img[/tt] tags.

Do you want the 12 includes to be rotated together in all 4... whatevers ? Like this ?

1[sup]st[/sup] request :
[tt]+-------+ +-------+
| A B C | | A B C |
| D E F | | D E F |
| G H I | | G H I |
| J K L | | J K L |
+-------+ +-------+
+-------+ +-------+
| A B C | | A B C |
| D E F | | D E F |
| G H I | | G H I |
| J K L | | J K L |
+-------+ +-------+[/tt]

2[sup]nd[/sup] request :
[tt]+-------+ +-------+
| L A B | | L A B |
| C D E | | C D E |
| F G H | | F G H |
| I J K | | I J K |
+-------+ +-------+
+-------+ +-------+
| L A B | | L A B |
| C D E | | C D E |
| F G H | | F G H |
| I J K | | I J K |
+-------+ +-------+[/tt]

Please make sure you have debugging turned on while testing:
Code:
[url=http://php.net/ini_set/]ini_set[/url]('[url=http://php.net/errorfunc.configuration/#ini.display-errors]display_errors[/url]','on');
[url=http://php.net/error_reporting/]error_reporting[/url]([url=http://php.net/errorfunc.constants/#errorfunc.constants.errorlevels.e-all]E_ALL[/url]);
If any error message appears, please post them.

Feherke.
 
Yes. Your example is correct. The only difference being I have the blocks (4 accross and 3 down) rather than (3 accross and 4 down). But yes.

Thanks!
 
Hi

And the $grouptwotopleft value in your code is the topleft of a single group ? So there would be $grouponetopleft..$groupfourtopleft ? Not really reasonable if they are all rotated synchronized.


Feherke.
 
That was my thought, but I'm sure you are correct. I was just trying to figure it out as I go.

The only need is that the 4 groups of rotating images be kept seperate. In other words they need to rotate within their own groups without venturing into any of the other groups.

If you know a better way (I'm sure you probably do) then I am totally open to your way.

THANKS!
 
Hi

egims said:
If you know a better way
Well, not necessary better, but certainly abit different.

Is EOD ( End Of Day ) on this continent, so I will put something together tomorrow.

Wait ! I think I have an idea about why your code is not working.

My solution used 5 files :
[ul]
[li]1 PHP script file[/li]
[li]4 HTML snippets files[/li]
[/ul]
You modification probably has 17 files :
[ul]
[li]1 PHP script file[/li]
[li]4 HTML snippets files for the groups[/li]
[li]12 HTML snippets files for the image codes[/li]
[/ul]
That will not work because the 4 file at the second level will also do includes so they must be also scripts :
[ul]
[li]1 PHP script file[/li]
[li]4 [highlight]PHP script files[/highlight] for the groups[/li]
[li]12 HTML snippets files for the image codes[/li]
[/ul]
That would mean the first level PHP script has to use [tt]include[/tt] instead of [tt]readfile()[/tt], so the second level scripts to be also interpreted as PHP scripts.


Feherke.
 
Thank you again. I more or less follow what you are saying, but a little over my head. Have a nice evening where you are and I will look forward to seeing what you have in mind tomorrow. Much appreciation.
 
i don't quite understand this new question.
on the assumption that the 12 images within each box remain static but the boxes themselves rotate clockwise, this code should work. it requires the html files with the image tags to be called sequentially html_file_X where X is 1 to 48. obviously this can be changed.

Code:
<?php
session_start();
define ('DEBUG', true);
function getContent($path){
	if (DEBUG) return $path;
	return file_get_contents($path);
}
$visit = empty($_SESSION['visit']) ? 1 : $_SESSION['visit'] + 1;
//calculate starting point (0 is top left)
$sp = ($visit % 4) - 1;

//we know we have 48 html files
//assume they are numbered 1 - 48
$base = 'path/to/my/directory/withhtml/';
$fileNumber = 1;
for ($b=1; $b<=4; $b++){
	$box = ($b + $sp) >= 4  ? ($b + $sp) - 4 : $b + $sp;
	$output[intval($box)] = "<div class=\"box box_$box\">I am box $box<br/>";
	 
	do { 
		$c = getContent($base . 'htmlFile_' . $fileNumber . '.html');
		$output[intval($box)] .= "<span class='boxContent'>$c</span>";
		$fileNumber++;
	} while ( ($fileNumber - 1) % 12 !== 0);
	$output[intval($box)] .= "</div>";
}
echo <<<CSS
<style type="text/css">
			span{display:block;}
			.box{
				border: black thin solid;
			}
			.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;
for($i=0; $i<4; $i++) echo $output[$i];
$_SESSION['visit'] = $visit;
session_write_close();
?>

if i have misunderstood and you also want each of the 12 banners to rotate clockwise at the same time as each of the boxes rotate clockwise, then post back. this is only a little bit more complex to do.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top