<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0065)http://127.1/code/source.php?url=./projects/csv-htmltable/csv.inc -->
<HTML><HEAD><TITLE>Source Display</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR></HEAD>
<BODY>
<TABLE width="100%" align=center bgColor=#6699cc>
<TBODY>
<TR>
<TD align=middle><B>Source listing for <PRE>csv.inc</PRE></B></TD></TR></TBODY></TABLE><CODE><FONT color=#000000><FONT
color=#0000bb><?php <BR></FONT><FONT
color=#ff8000>/****************************************************************************************
<BR>* CSV2HTML - PHP code by Srinivasan Ramakrishnan, cheeni@bigfoot.com *
<BR>* http://www.sriniram.com, 03/FEB/2001 *
<BR>* Free for non-commercial use. For commercial use please contact author. *
<BR>* No warranties of correctness are extended for non-commercial usage. *
<BR>****************************************************************************************/
<BR>/* <BR>csvtohtml <BR>(PHP3 >= 3.0.8, PHP4)
<BR>csvtohtml -- Converts a CSV file to a HTML table
<BR><BR>Description:
<BR>&string [&boolean] &csvtohtml(string $csvFilePath, string $htmlFilePath, int $maxRowLength, string $htmlTableStyle [, string $delimiterString = ","])
<BR><BR>string $csvFilePath - Must point to a valid CSV file path
<BR><BR>string $htmlFilePath - The output HTML file destination path, NULL if output is to be only as return value.
<BR><BR>int $maxRowLength - The max number of characters on any line in the CSV file
<BR><BR>string $htmlTableStyle - The HTML style elements in a table, can be left as a zero length string for default styling as per browser
<BR> Ex: 'align="center" background="bg.gif" border="3"'
<BR><BR>string $delimiterString - By default assumed as , (comma). Denotes the delimiting character between fields
<BR><BR>Usage:
<BR>The function will return a reference to a variable containing the HTML code
<BR>for creating the table containing the CSV records.However an output HTML file
<BR>path may be specified to allow output to an HTML file. The returned reference
<BR>will contain a boolean false if an error occurs.
<BR><BR>If output need not be stored as an HTML file the $htmlFilePath must be set to NULL.
<BR><BR>Ex:
<BR><BR>$output = &csvtohtml("test.csv", NULL, 1000, "border='0'", ";");
<BR><BR>csvtohtml("test.csv", "test.html", 1000, "border='0'", ";");
<BR><BR>$output = &csvtohtml("test.csv", "test.html", 1000, "");
<BR><BR>*/ <BR><BR></FONT><FONT color=#007700>require(</FONT><FONT
color=#dd0000>"errOut.inc"</FONT><FONT color=#007700>);
<BR><BR>function &</FONT><FONT color=#0000bb>csvtohtml</FONT><FONT
color=#007700>(</FONT><FONT color=#0000bb>$csvFilePath</FONT><FONT
color=#007700>, </FONT><FONT color=#0000bb>$htmlFilePath</FONT><FONT
color=#007700>, </FONT><FONT color=#0000bb>$maxRowLength</FONT><FONT
color=#007700>, </FONT><FONT color=#0000bb>$htmlTableStyle</FONT><FONT
color=#007700>, </FONT><FONT
color=#0000bb>$delimiterString </FONT><FONT
color=#007700>= </FONT><FONT color=#dd0000>","</FONT><FONT color=#007700>){
<BR><BR> if(</FONT><FONT
color=#0000bb>file_exists</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$csvFilePath</FONT><FONT color=#007700>)){
<BR> if(</FONT><FONT
color=#0000bb>$csvFileHndl </FONT><FONT color=#007700>= @</FONT><FONT
color=#0000bb>fopen</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$csvFilePath</FONT><FONT color=#007700>,</FONT><FONT
color=#dd0000>"r"</FONT><FONT color=#007700>)){
<BR>
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"<table " </FONT><FONT
color=#007700>. </FONT><FONT
color=#0000bb>$htmlTableStyle </FONT><FONT
color=#007700>. </FONT><FONT color=#dd0000>">"</FONT><FONT
color=#007700>;
<BR>
<BR> </FONT><FONT
color=#0000bb>$row </FONT><FONT color=#007700>= </FONT><FONT
color=#0000bb>1</FONT><FONT color=#007700>;
<BR> while (</FONT><FONT
color=#0000bb>$data </FONT><FONT color=#007700>= @</FONT><FONT
color=#0000bb>fgetcsv </FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$csvFileHndl</FONT><FONT color=#007700>, </FONT><FONT
color=#0000bb>$maxRowLength</FONT><FONT color=#007700>, </FONT><FONT
color=#0000bb>$delimiterString</FONT><FONT color=#007700>)) {
<BR> </FONT><FONT
color=#0000bb>$num </FONT><FONT color=#007700>= </FONT><FONT
color=#0000bb>count </FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$data</FONT><FONT color=#007700>);
<BR>
<BR> </FONT><FONT
color=#ff8000>/* Skip empty lines */
<BR> </FONT><FONT
color=#007700>if(</FONT><FONT color=#0000bb>$data</FONT><FONT
color=#007700>[</FONT><FONT color=#0000bb>0</FONT><FONT
color=#007700>] != </FONT><FONT color=#0000bb>NULL </FONT><FONT
color=#007700>&& </FONT><FONT color=#0000bb>$num </FONT><FONT
color=#007700>> </FONT><FONT color=#0000bb>1</FONT><FONT
color=#007700>){
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"<tr>\n"</FONT><FONT color=#007700>;
<BR> for (</FONT><FONT
color=#0000bb>$c</FONT><FONT color=#007700>=</FONT><FONT
color=#0000bb>0</FONT><FONT color=#007700>; </FONT><FONT
color=#0000bb>$c</FONT><FONT color=#007700><</FONT><FONT
color=#0000bb>$num</FONT><FONT color=#007700>; ++</FONT><FONT
color=#0000bb>$c</FONT><FONT color=#007700>){
<BR> if(</FONT><FONT
color=#0000bb>$row </FONT><FONT color=#007700>== </FONT><FONT
color=#0000bb>1</FONT><FONT color=#007700>)
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"\t<th>"</FONT><FONT color=#007700>;
<BR> else
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"\t<td>"</FONT><FONT color=#007700>;
<BR>
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#0000bb>$data</FONT><FONT color=#007700>[</FONT><FONT
color=#0000bb>$c</FONT><FONT color=#007700>];
<BR>
<BR> if(</FONT><FONT
color=#0000bb>$row </FONT><FONT color=#007700>== </FONT><FONT
color=#0000bb>1</FONT><FONT color=#007700>)
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"</th>\n"</FONT><FONT color=#007700>;
<BR> else
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"</td>\n"</FONT><FONT color=#007700>;
<BR> }
<BR>
<BR> ++</FONT><FONT
color=#0000bb>$row</FONT><FONT color=#007700>;
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"</tr>\n"</FONT><FONT color=#007700>;
<BR> }
<BR> }
<BR> </FONT><FONT
color=#0000bb>$htmlOut </FONT><FONT color=#007700>.= </FONT><FONT
color=#dd0000>"</table>"</FONT><FONT color=#007700>; </FONT><FONT
color=#ff8000>/* The HTML table is now ready*/
<BR> </FONT><FONT
color=#0000bb>fclose</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$csvFileHndl</FONT><FONT
color=#007700>); </FONT><FONT
color=#ff8000>/* Get rid of CSV file*/
<BR>
<BR> /* Output a HTML file if needed */
<BR>
<BR> </FONT><FONT
color=#007700>if(</FONT><FONT color=#0000bb>$htmlFilePath </FONT><FONT
color=#007700>!= </FONT><FONT color=#0000bb>NULL</FONT><FONT
color=#007700>){
<BR> if(</FONT><FONT
color=#0000bb>file_exists</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFilePath</FONT><FONT color=#007700>)){
<BR> </FONT><FONT
color=#0000bb>$errMsg </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"$htmlFilePath already exists. Overwriting file"</FONT><FONT
color=#007700>;
<BR> </FONT><FONT
color=#0000bb>errOut</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$errMsg</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>2</FONT><FONT color=#007700>);
<BR> }
<BR> if(</FONT><FONT
color=#0000bb>$htmlFileHndl </FONT><FONT color=#007700>= @</FONT><FONT
color=#0000bb>fopen</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFilePath</FONT><FONT color=#007700>,</FONT><FONT
color=#dd0000>"w"</FONT><FONT color=#007700>)){
<BR> </FONT><FONT
color=#0000bb>$htmlCode </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"<html><head><title>HTML Table of CSV file $csvFilePath</title></head><body>"</FONT><FONT
color=#007700>;
<BR> </FONT><FONT
color=#0000bb>$endHtmlCode </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"</body></html>"</FONT><FONT color=#007700>;
<BR> if( ! @</FONT><FONT
color=#0000bb>fwrite</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFileHndl</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>$htmlCode</FONT><FONT color=#007700>) ||
<BR> ! @</FONT><FONT
color=#0000bb>fwrite</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFileHndl</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>$htmlOut</FONT><FONT color=#007700>) ||
<BR> ! @</FONT><FONT
color=#0000bb>fwrite</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFileHndl</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>$endHtmlCode</FONT><FONT color=#007700>)){
<BR> </FONT><FONT
color=#0000bb>$errMsg </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"Output file write at $htmlFilePath could not be completed. Check disk space"</FONT><FONT
color=#007700>;
<BR> </FONT><FONT
color=#0000bb>errOut</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$errMsg</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>1</FONT><FONT color=#007700>);
<BR> }
<BR> }else{
<BR> </FONT><FONT
color=#0000bb>$errMsg </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"Output file $htmlFilePath could not be created. Check permissions"</FONT><FONT
color=#007700>;
<BR> </FONT><FONT
color=#0000bb>errOut</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$errMsg</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>1</FONT><FONT
color=#007700>);
<BR> }
<BR> </FONT><FONT
color=#0000bb>fclose</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$htmlFileHndl</FONT><FONT color=#007700>);
<BR> }
<BR>
<BR> return </FONT><FONT
color=#0000bb>$htmlOut</FONT><FONT
color=#007700>; </FONT><FONT
color=#ff8000>/* App ends after returning HTML code*/
<BR>
<BR> </FONT><FONT
color=#007700>}else{
<BR> </FONT><FONT
color=#0000bb>$errMsg </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"Unable to open CSV file. Please check the file permissions or out of memory <br><b>" </FONT><FONT
color=#007700>. </FONT><FONT color=#0000bb>$csvFilePath </FONT><FONT
color=#007700>. </FONT><FONT color=#dd0000>"</b>"</FONT><FONT
color=#007700>;
<BR> </FONT><FONT
color=#0000bb>errOut</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$errMsg</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>0</FONT><FONT color=#007700>);
<BR> return </FONT><FONT
color=#0000bb>false</FONT><FONT
color=#007700>;
<BR> }
<BR> }else{
<BR> </FONT><FONT
color=#0000bb>$errMsg </FONT><FONT color=#007700>= </FONT><FONT
color=#dd0000>"Unable to find input CSV file. Please check the file path <br><b>" </FONT><FONT
color=#007700>. </FONT><FONT color=#0000bb>$csvFilePath </FONT><FONT
color=#007700>. </FONT><FONT color=#dd0000>"</b>"</FONT><FONT
color=#007700>; <BR> </FONT><FONT
color=#0000bb>errOut</FONT><FONT color=#007700>(</FONT><FONT
color=#0000bb>$errMsg</FONT><FONT color=#007700>,</FONT><FONT
color=#0000bb>0</FONT><FONT color=#007700>);
<BR> return </FONT><FONT
color=#0000bb>false</FONT><FONT color=#007700>; <BR> }
<BR><BR>} <BR></FONT><FONT color=#0000bb>?> <BR></FONT></FONT></CODE>
<TABLE width="100%" align=center bgColor=#6699cc>
<TBODY>
<TR>
<TD align=middle><STRONG>Last modified: Saturday
03/February/2001</STRONG></TD></TR></TBODY></TABLE></BODY></HTML>
|