Login   Register  
PHP Classes
elePHPant
Icontem

File: csv.inc

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Srinivasan Ramakrishnan  >  csvtohtml  >  csv.inc  >  Download  
File: csv.inc
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: 2001-02-03 13:18
Size: 5,168 bytes
 

Contents

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

Description:
&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 

Usage:
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.

Ex:

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

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

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

*/

require("errOut.inc");

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

    if(file_exists($csvFilePath)){
        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>";
                            else
                                $htmlOut .= "\t<td>";
                                
                            $htmlOut .= $data[$c];
                            
                            if($row == 1)
                                $htmlOut .= "</th>\n";
                            else
                                $htmlOut .= "</td>\n";
                        }
                            
                        ++$row;
                        $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){
                if(file_exists($htmlFilePath)){
                    $errMsg = "$htmlFilePath already exists. Overwriting file";
                    errOut($errMsg,2);
                }
                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";
                            errOut($errMsg,1);
                    }
                }else{
                    $errMsg = "Output file $htmlFilePath could not be created. Check permissions";
                    errOut($errMsg,1);                  
                }
                fclose($htmlFileHndl);
            }
            
            return $htmlOut;    /* App ends after returning HTML code*/
            
        }else{
            $errMsg = "Unable to open CSV file. Please check the file permissions or out of memory <br><b>" . $csvFilePath . "</b>";
            errOut($errMsg,0);
            return false;       
        }
    }else{
        $errMsg = "Unable to find input CSV file. Please check the file path <br><b>" . $csvFilePath . "</b>";
        errOut($errMsg,0);
        return false;
    }

}
?>