jasonglisson
2/9/2016 - 7:42 PM

Render table in PHP

Render table in PHP

	$rows = 1;
	
	if (($handle = fopen("<path to csv file>", "r")) !== FALSE) {
	    
	    echo '<table class="table table-bordered toggle-square-filled default breakpoint footable">';
	    
	    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
	        $num = count($data);
	        if ($rows == 1) {
	            echo '<thead><tr>';
	        } else {
	            echo '<tr>';
	        }
	        
	        for ($c=0; $c < $num; $c++) {
	            //echo $data[$c] . "<br />\n";
	            if(empty($data[$c])) {
	               $value = "&nbsp;";
	            } else {
	               $value = $data[$c];
	            }
	            if ($rows == 1) {
	                echo '<th>'.$value.'</th>';
	            } else { 
	                echo '<td>'.$value.'</td>';
	            }
	        }
	        
	        if ($rows == 1) {
	            echo '</tr></thead><tbody>';
	        } else {
	            echo '</tr>';
	        }
	        $rows++;
	    }
	    
	    echo '</tbody></table>';
	    fclose($handle);
	} else {
		echo '<h4>No CSV file found</h4>';
	}