PHP Classes


Recommend this page to a friend!
  Classes of Srinivasan Ramakrishnan  >  csvtohtml  >  >  Download  
Role: ???
Content type: text/plain
Description: The include file containing the core code for csv2html
Class: csvtohtml
Converts a CSV file into a HTML table
Author: By
Last change:
Date: 21 years ago
Size: 5,168 bytes


Class file image Download
*       CSV2HTML - PHP code by Srinivasan Ramakrishnan,              *
*, 03/FEB/2001                                            *
*       Free for non-commercial use. For commercial use please contact author.          *
*       No warranties of correctness are extended for non-commercial usage.             *
(PHP3 >= 3.0.8, PHP4)
csvtohtml -- Converts a CSV file to a HTML table

&string [&boolean] &csvtohtml(string $csvFilePath, string $htmlFilePath, int $maxRowLength, string $htmlTableStyle [, string $delimiterString = ","])

string $csvFilePath     - Must point to a valid CSV file path

string $htmlFilePath    - The output HTML file destination path, NULL if output is to be only as return value.

int $maxRowLength       - The max number of characters on any line in the CSV file

string $htmlTableStyle  - The HTML style elements in a table, can be left as a zero length string for default styling as per browser
                          Ex: 'align="center" background="bg.gif" border="3"'

string $delimiterString - By default assumed as , (comma). Denotes the delimiting character between fields 

The function will return a reference to a variable containing the HTML code 
for creating the table containing the CSV records.However an output HTML file 
path may be specified to allow output to an HTML file. The returned reference 
will contain a boolean false if an error occurs.

If output need not be stored as an HTML file the $htmlFilePath must be set to NULL.


$output = &csvtohtml("test.csv", NULL, 1000, "border='0'", ";");

csvtohtml("test.csv", "test.html", 1000, "border='0'", ";");

$output = &csvtohtml("test.csv", "test.html", 1000, "");



function &csvtohtml($csvFilePath, $htmlFilePath, $maxRowLength, $htmlTableStyle, $delimiterString = ","){

        if($csvFileHndl = @fopen($csvFilePath,"r")){
            $htmlOut = "<table " . $htmlTableStyle . ">";
            $row = 1;
            while ($data = @fgetcsv ($csvFileHndl, $maxRowLength, $delimiterString)) {
                    $num = count ($data);
                    /* Skip empty lines */
                    if($data[0] != NULL && $num > 1){
                        $htmlOut .= "<tr>\n";
                        for ($c=0; $c<$num; ++$c){
                            if($row == 1)
                                $htmlOut .= "\t<th>";
                                $htmlOut .= "\t<td>";
                            $htmlOut .= $data[$c];
                            if($row == 1)
                                $htmlOut .= "</th>\n";
                                $htmlOut .= "</td>\n";
                        $htmlOut .= "</tr>\n";
            $htmlOut .= "</table>"; /* The HTML table is now ready*/
            fclose($csvFileHndl);   /* Get rid of CSV file*/
            /* Output a HTML file if needed */
            if($htmlFilePath != NULL){
                    $errMsg = "$htmlFilePath already exists. Overwriting file";
                if($htmlFileHndl = @fopen($htmlFilePath,"w")){
                    $htmlCode = "<html><head><title>HTML Table of CSV file $csvFilePath</title></head><body>";
                    $endHtmlCode = "</body></html>";
                    if( ! @fwrite($htmlFileHndl,$htmlCode) || 
                        ! @fwrite($htmlFileHndl,$htmlOut) || 
                        ! @fwrite($htmlFileHndl,$endHtmlCode)){
							$errMsg = "Output file write at $htmlFilePath could not be completed. Check disk space";
                    $errMsg = "Output file $htmlFilePath could not be created. Check permissions";
            return $htmlOut;    /* App ends after returning HTML code*/
            $errMsg = "Unable to open CSV file. Please check the file permissions or out of memory <br><b>" . $csvFilePath . "</b>";
            return false;       
        $errMsg = "Unable to find input CSV file. Please check the file path <br><b>" . $csvFilePath . "</b>";
        return false;

For more information send a message to info at phpclasses dot org.