Skip to content

Instantly share code, notes, and snippets.

@bmdinteractive
Created September 19, 2018 15:33
Show Gist options
  • Save bmdinteractive/7048f9ac4408556159da148f8dd40d09 to your computer and use it in GitHub Desktop.
Save bmdinteractive/7048f9ac4408556159da148f8dd40d09 to your computer and use it in GitHub Desktop.
PHP CSV Table Generation
<?php
$csv_file = get_field('standard_orders');
if ( ( $handle = fopen( $csv_file, "r" ) ) !== FALSE ):
$row_count = 0;
?>
<table class="table">
<?php
$color_col;
while ( ( $data_row = fgetcsv( $handle, 1000, "," ) ) !== FALSE ): // Loop through CSV
if($row_count==0): // Begin Header Row ?>
<thead>
<tr>
<?php
$col_total = count( $data_row );
for ($c=0; $c < $col_total; $c++) {
echo '<th>' . $data_row[ $c ] . "</th>";
if(strtolower($data_row[ $c ])=='color') {
$color_col = $c;
}
}
?>
</tr>
</thead>
<?php else: // End Header Row ?>
<tr>
<?php
$col_total = count( $data_row );
for ($c=0; $c < $col_total; $c++) {
if($c==$color_col):
$color_list = explode('|', $data_row[ $c ]);
echo '<td>';
$current_color = 1;
$color_count = count($color_list);
foreach($color_list as $color):
?>
<span class="color-<?php echo strtolower($color) ?>"><?php echo $color; ?></span><?php if($current_color < $color_count) { echo ', '; } ?>
<?php
$current_color++;
endforeach;
echo '</td>';
else:
echo '<td>' . $data_row[ $c ] . "</td>";
endif;
}
?>
</tr>
<?php
endif; ?>
<?php
$row_count++;
endwhile;
fclose( $handle ); ?>
</table>
<?php
endif; // End CSV file load
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment