Zend - The PHP Company




Tables

Add Code


Create tables from ASCII files  

Type: application
Added by: gwolanin
Entered: 17/07/2002
Last modified: 07/12/2001
Rating: - (fewer than 3 votes)
Views: 6813
This PHP script will take the contents of a character delimited text file and create a nice looking table for it.


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$fdfilesize$file ) );
        
$line explode$delimeterrtrim$buffer ) );
        
//$arrylgth = count( $line );

        
if( $rowctr == )
        {
            
$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] = "&nbsp;"; }
                if( 
$altcolor%== )
                {
                    
$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>



Usage Example


See the example


Rate This Script





Search



This Category All Categories