home>php>addrotate


The following script is used at http://guide105.com to include the "Today's Travel Feature" features into the home page one per day.

<script language="php">
	
	# Let's assign some variables
	
	# Include files directory
	$directory = "file_directory";
	
	# Today's date in format yymmdd
	$today = date(ymd);
	
	# Rotation file
	$rotating_file = "rotate.dat";	
	
	# File List
	$list_file = "list.cfg";


	# Read rotation data file into array
	$schedule = file("$directory/$rotating_file");
	
	
	# We assign the values we read to the appropriate variable and trim out the line feed 
	$ID = trim($schedule[0]);
	$date = trim($schedule[1]);
	$file = trim($schedule[2]);
	
	if($date == $today)
	{
		include ("$directory/$file");	
	}
	
	else
	{
		# Read list of files available
		$list = file("$directory/$list_file");
		
		
		# If we reached the last include file we go back to the beginning, else we increase 1
		if($ID == (count($list)-1))
		{
			$ID = 0;
		}
		else
		{
			$ID++;
		}
		
		# Let's assign the new file to include to its variable, after a line feed trim		
		$file = trim($list[$ID]);
		
		# We write the new rotation file with today's data
		$fp = fopen("$directory/$rotating_file", "w");
		fwrite($fp,"$ID\n$today\n$file\n");
		fclose($fp);
		
		# And now we include the new file for the day
		include("$directory/$file");

	}
	
   
</script>    


This HTML file contains the script for a simple control panel used 
to add/delete include files from the list file. 

<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<title>Guide 105 Today's Feature Control Panel</title>
</head>

<body>


<h2 align="center"><font face="Arial">Guide 105 Home Page Features Editing Panel</font></h2>
<h3 align="center"><font color="#008080" face="Arial">Feature Files available on
the server</font></h3>

<p align="center">
<script language="php">

# Directory to open, in this case the current one
$handle=opendir('.'); 

# Let's assign some HTML variables
$this_file = "name_of_this_file";
$cfg_file = "name_of_the_list_file";
$href= "<A HREF=\""; 
$hsuf = "\">"; 
$het = "</A>"; 
$hrefurl = "<A HREF=\"";

# The name of this script and its GET parameter name to add the file to the list
$runurl = "<A HREF=\"$this_file?filename="; 
$tabletag= "<TABLE width=456>"; 

if ($handle) 
{ 

	# Start the Table and the table headings 
	echo $tabletag. "<TR><TD><B><U>View Feature Files</U>"; 
	echo "</B></TD><TD><B><U>Add To List</U></B></TD>"; 
	echo "<TD><B><U>Last Modified</U></B></TD><TR>"; 

	while ($file=readdir($handle))
	{ 
		# List only the .inc files present in the directory
		if ((strstr($file, ".inc"))) 
		{ 
			echo "<TR><TD>"; 
			echo $href. "$file".$hsuf.$file.$het. "</TD>"; 
			echo "<TD>".$runurl.$file.$hsuf.$file.$het; 
			echo "</TD>"; 

			# Get the file date and time
			$incfiletime=filectime($file); 
			$datemod = date( "h:i A m/d/Y", $incfiletime); 

			echo "<TD>".$datemod. "</TD>"; 
			echo "</TR>"; 
		} 
	}
 
	echo "</TABLE>"; 
	closedir($handle); 
} 


# If we cliked on one of the files to add it to the list, let's save the results
if($filename)
{
	$fp = fopen("$cfg_file", "a");
	fwrite($fp,"\n$filename");
	fclose($fp);

}


# If we modified the list manually in the form box, let's save the results
if($filedata)
{
	# Let's eliminate the form carriage characters
	$filedata = ereg_replace("\r","",$filedata);
	
	# Let's write to file the new configuration
	$fp = fopen("$cfg_file", "w");
	fwrite($fp,"$filedata");
	fclose($fp);
}

</script>

<p align="center">


<font color="#008080">To modify the features
rotating on the home page,<br>
change the list below and click &quot;Save&quot;.</font></p>

<form method="POST" action="<?php echo "$this_file"; ?>">
  <div align="center">
    <center>
    <table border="0">
      <tr>
        <td width="100%"><textarea rows="10" name="filedata" cols="34"><?php readfile("$cfg_file"); ?></textarea></td>
      </tr>
      <tr>
        <td width="100%"><font size="2" color="#008080">Remember:
          DELETE any blank line at the end.<br>
          </font><input type="submit" value="Save"></td>
      </tr>
    </table>
    </center>
  </div>
</form>

</body>
</html>