Sidebar Script
Source Code
<?php
// uncomment the filename line to run this as a seperate file and
// change this filename to the file you want checked in the same directory as this file resides
$file_name = "menu_data.txt";
// Error Supress the fopen
$handle = @fopen($file_name, "r");
// Check for successful fopen
if (!$handle) {
// error message and exit if no file handle available
echo "File Handle Not Available For Use"; exit;
}
// On successful fopen, read the file one line at a time into an array
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
// loop to stuff the output buffer with data for output
if ($data[2] == "header") {
$link = '<div class="sidebar"><dl><dt class="' . $data[1] .'" >' . $data[0] . '</dt>';
}
// or else check to see if the div is ending
elseif ($data[2] == "footer") {
$link = '</dl></div>';
}
// or there must be link information
else
$link = "\t" . '<dd class="' . $data[1] . '"> <a href="' . $data[2] . '" title="' . $data[3] . '" alt="' . $data[4] . '">' . $data[0] . '</a></dd>';
echo "\n\r" . $link;
}
//close the open file prior to ending
// uncomment as required
fclose($handle);
?>
How it works
This first section is fairly mundane. It simply opens the file named "menu_data.txt" and gets a handle to the file. If the handle is not available, it kills the process after writing an error message.
<?php
// uncomment the filename line to run this as a seperate file and
// change this filename to the file you want checked in the same directory as this file resides
$file_name = "menu_data.txt";
// Error Supress the fopen
$handle = @fopen($file_name, "r");
// Check for successful fopen
if (!$handle) {
// error message and exit if no file handle available
echo "File Handle Not Available For Use"; exit;
}
This next section reads a line of the file until the end-of-file condition is met.
// On successful fopen, read the file one line at a time into an array
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
The data which was read from the file now is checked to see if it is a Header data line. If so, a variable named "$link" is created from the data. The data will be output as an html line shortly.
// loop to stuff the output buffer with data for output
if ($data[2] == "header") {
$link = '<div class="sidebar"><dl><dt class="' . $data[1] .'" >' . $data[0] .
'</dt>';
}
The data which was read from the file now is checked to see if it is a Footer data line. If so, a variable named "$link" is created from the data. The data will be output as an html line shortly.
// or else check to see if the div is ending
elseif ($data[2] == "footer") {
$link = '</dl></div>';
}
Okay, since a data line is neither a Header nor a Footer, it must contain data for a link. The information is formatted for output and stored into the variable. Notice that the Logic is structured such that only one type of an output line is created by use of the If_Elseif_Else function. Now the variable which contains the html code is echo'd to the Browser, file closed and we are done here.
// or there must be link information else $link = "\t" . '<dd class="' . $data[1] . '"> <a href="' . $data[2] . '" title="' . $data[3] . '" alt="' . $data[4] . '">' . $data[0] . '</a>'; echo "\n\r" . $link; } //close the open file prior to ending // uncomment as required fclose($handle); ?>
Previous page: The Template Script Next page: The Content Array Script