Skip to content

Instantly share code, notes, and snippets.

@thagxt
Created October 28, 2015 15:49
Show Gist options
  • Save thagxt/1199f61214210b7f1538 to your computer and use it in GitHub Desktop.
Save thagxt/1199f61214210b7f1538 to your computer and use it in GitHub Desktop.
Display CSV file as HTML Table with first 2 columns clickable
<?php
$file = "releases.csv"; // your csv file name
$directory = $_SERVER['DOCUMENT_ROOT']."/var/releases/".$file; // path to file
$row = 1; // horizontal
if (($handle = fopen($directory, "r")) !== FALSE) {
echo '<table id="releases-calendar">';
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) { // change ";" if your csv delimiter is different...
// var_dump($data);
$num = count($data);
if ($row == 1) {
echo '<thead class="thead"><tr>';
} else {
echo '<tr>';
}
for ($c=0; $c < $num; $c++) {
if(empty($data[$c])) {
$value = "&nbsp;";
} else{
$value = $data[$c];
}
// Getting URLs from Array
$stackUrl = $data[4]; // data Stream
$urlStack = explode('*****', $stackUrl); // Explode
$urlOne = $urlStack[0]; // Get URL 1
$urlTwo = $urlStack[1]; // Get URL 2
if ($row == 1) {
echo '<th>'.$value.'</th>';
} else {
/* remove if you don't the first 2 columns to be clickable */
if ($data[$c] == $data[0]) {
echo '<td><a href="'.$urlOne.'">'.$value.'</a></td>';
} else if ($data[$c] == $data[1]) {
echo '<td><a href="'.$urlTwo.'">'.$value.'</a></td>';
} else {
/* end part to be removed. */
echo '<td>'.$value.'</td>';
}
}
}
if ($row == 1) {
echo '</tr></thead><tbody class="tbody">';
} else {
echo '</tr>';
}
$row++;
}
echo '</tbody></table>';
fclose($handle);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment