file2table.php
<?php
//
// This PHP script will take the contents of a character delimited text file
// and create a nice looking table for it.
//
// Rules of the text file:
// 1. First line of text file should the the table headings
// 2. Each column should be separated by the delimiter
// 3. Default colors will be blue and white
// 4. Default tablename ( id ) will be "table_x" where x will be incremented
// if there are multiple function calls.
//
// SYNTAX:
//
// echo file2table( $filename, $tablename, $delimeter, $r_color1, $r_color2 );
//
//
$x=0;
function file2table( $filename, $tablename, $delimeter, $r_color1, $r_color2 )
{
$x++;
$border=0;
$cellspacing=4;
$thColor="aqua";
$align="center";
$rowctr=0; // change to 1 if you do not want table headers
$result = "<div id="table_$x">nt";
$result .= "<table border="$border" cellspacing="$cellspacing" align="$align">ntt";
//$file = "info/contracts_warranties.txt";
$file = "$filename";
$fd = @fopen( $file, "r" );
while( !feof( $fd ) )
{
$rowctr++;
$buffer = fgets( $fd, filesize( $file ) );
$line = explode( $delimeter, rtrim( $buffer ) );
//$arrylgth = count( $line );
if( $rowctr == 1 )
{
$result .= "<tr>nttt";
$arrylgth = count( $line );
for( $colctr = 0; $colctr < $arrylgth; $colctr++ )
{
$result .= "<th bgColor="$thColor" nowrap><font size="+1">$line[$colctr]</font></th>nttt";
}
$result .= "ntt</tr>";
$columns = $arrylgth;
}
else
{
$result .= "<tr>nttt";
$altcolor++;
for( $colctr = 0; $colctr < $columns; $colctr++ )
{
if( $line[$colctr] == "" ) { $line[$colctr] = " "; }
if( $altcolor%2 == 0 )
{
$result .= "<td bgColor="$r_color1">$line[$colctr]</td>nttt";
}
else
{
$result .= "<td bgColor="$r_color2">$line[$colctr]</td>nttt";
}
}
$result .= "ntt</tr>";
}
}
$result .= "nt</table>nt</div>n";
fclose( $fd );
return $result;
}
?>
filename.php - This is the file that calls the above function.
<html>
<head>
<title>AutoTAP results from car</title>
<!-- Include the "file2table.php" function for use -->
<?php require( "file2table.php" ); ?> </head>
<body bgColor="white">
<? echo file2table "../aug21b.csv", "geotable", ",", "lightyellow", "lightgreen" ); ?>
</body>
</html>
|
|