mistergraphx
8/7/2015 - 9:14 PM

csv2table

<?php 
Function csv2table ($celldata, $delim, $numcols, $headerrow, $tablestyle) {
	// $celldata 	delimited data
	// $delim		delimiter
	// $numcols		number of columns in table
	// $headerrow	non-zero value will give <th></th> row
	// $tablestyle  anything you want included in the <TABLE> tag
	// 		Donncha Butler	-- 	Version 1.0
	
	echo "<TABLE $tablestyle>\n";
	$b = explode($delim, $celldata); 
	for ($i = 0; $i <= sizeof($b); $i++) {
	    $col = $i % $numcols;
	    $hrow = ($i < $numcols) && $headerrow ;
	    switch ($col){
	    	case 0:		
				echo "<TR>\n";
				if ($hrow) {
					echo "  <TH>\n"; 
				} else { 
					echo "  <TD class=tdinfo>\n"; 
				}
				echo $b[$i] . "\n";
				break;
			default:
				if ($hrow) {
		 	 		echo "	</TH>\n  <TH>\n"; 
		 	 	} else { 
		 	 		echo "	</TD>\n  <TD class=tdinfo>\n"; 
		 	 	}
		 		echo $b[$i] . "\n";
		}
		if($col == $numcols - 1) { 
			if ($hrow) {
	 	 		echo "	</TH>\n</TR>\n"; 
	 	 	} else {
				echo "	</TD>\n</TR>\n"; 
			}
		}	 
	}
	echo "</TABLE>\n";
}

//Example		
$a = "
Air Line #,Function,Colour,Pressure,
21,Sealer down,Blue,6 bar,
22,Horizontal out,Green, 4 bar,
23,Sealer up,Red,3 bar,
24,Horizontal in,White,5 bar,
";

$style = "BORDER=\"1\" CELLSPACING=\"4\" CELLPADDING=\"4\" WIDTH=\"600\"";
csv2table ($a, ",", 4, 1, $style);

?>