I have a php file that reads a csv file and pulls the data and displays it on the site. But unfortunately I don't know why it creates an extra line which sets the counter to go wrong... How do i fix that?
This is my code:
<style> table{width:100%; border-collapse:collapse; border:3px solid red; font-size: 10px;} td {font-size: 10px; border: 1px solid blue;border-collapse:collapse; text-align: left; width: 50px; padding-top: 1px; padding-left: 1px;} th {background: #ffb300; text-align: left; padding-top: 1px; padding-left: 1px; font-size: 10px;} tr:hover {color: red; font-size: 10px;} .dark {background: #ffb300;} .light {background: #ffb300;} first_name {width: 50px;} last_name {width: 50px;} email {width: 50px;} telephone {width: 50px;} reason {width: 50px;} time {width: 20px;} comment {width: 150px;} count {width: 20px;} today {width: 30px;}</style>
<?php
echo "<html><body><table style=''>";//Table style is empty
$f = fopen("csv.csv", "r"); //File csv.csv is openned and read only "r"
$trcount = 0; //This is just to set a counter for making sure that we have alternating rows
while (($line = fgetcsv($f)) !== false) { //$line = the content of $f which is the content of the CSV --- First Row
$trclass = ''; //It has a table row class formating that is set to nothing...
if ($trcount%2==0)
{ $trclass=' class="dark"'; } //if count divided by 2 has no remainder then it is even otherwise no formating and it is light.
echo "<tr".$trclass.">\n"; //Just echos background for the row ONLY!!!!
$tdcount = 1; //reset to 0 for each inner loop
foreach ($line as $cell) {
$tdclass = '';
if ($tdcount%2==0)
{ $tdclass='class="light"'; } //default to nothing, but if it's even apply a class
echo "<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; //same as before, but now this also has the variable $class to setup a class if needed
$tdcount++; //go up one each loop
}
// echo "\r</tr>";
$trcount++; //go up one each loop
}
fclose($f);
echo "</table></body></html>";
?>