You're here: Snippet Directory » PHP (130)
Language:

csv2table

Language: English
Programming Language: PHP
Published by: drbutler [not registered]
Last Update: 5/15/2006
Views: 1329


Description

Converts delimited string into HTML table. Arguements include delimiter, num of colums, header row on/off and style.

Code

1 <?php 2 Function csv2table ($celldata, $delim, $numcols, $headerrow, $tablestyle) { 3 // $celldata delimited data 4 // $delim delimiter 5 // $numcols number of columns in table 6 // $headerrow non-zero value will give <th></th> row 7 // $tablestyle anything you want included in the <TABLE> tag 8 // Donncha Butler -- Version 1.0 9 10 echo "<TABLE $tablestyle>\n"; 11 $b = explode($delim, $celldata); 12 for ($i = 0; $i <= sizeof($b); $i++) { 13 $col = $i % $numcols; 14 $hrow = ($i < $numcols) && $headerrow ; 15 switch ($col){ 16 case 0: 17 echo "<TR>\n"; 18 if ($hrow) { 19 echo " <TH>\n"; 20 } else { 21 echo " <TD class=tdinfo>\n"; 22 } 23 echo $b[$i] . "\n"; 24 break; 25 default: 26 if ($hrow) { 27 echo " </TH>\n <TH>\n"; 28 } else { 29 echo " </TD>\n <TD class=tdinfo>\n"; 30 } 31 echo $b[$i] . "\n"; 32 } 33 if($col == $numcols - 1) { 34 if ($hrow) { 35 echo " </TH>\n</TR>\n"; 36 } else { 37 echo " </TD>\n</TR>\n"; 38 } 39 } 40 } 41 echo "</TABLE>\n"; 42 } 43 44 //Example 45 $a = " 46 Air Line #,Function,Colour,Pressure, 47 21,Sealer down,Blue,6 bar, 48 22,Horizontal out,Green, 4 bar, 49 23,Sealer up,Red,3 bar, 50 24,Horizontal in,White,5 bar, 51 "; 52 53 $style = "BORDER=\"1\" CELLSPACING=\"4\" CELLPADDING=\"4\" WIDTH=\"600\""; 54 csv2table ($a, ",", 4, 1, $style); 55 56 ?> 57

No comments avaiable

Add a comment

Name *  

Email (won't be displayed) *    

Website  

Comment *  

Sicherheitscode Security Code *    

RSS