I have a PHP file that is pulling information in from a CSV file and is successfully turning it into a table that only shows specific rows and columns.
However, the CSV file has entries in it that have a comma and space in them, wrapped in quotation marks. My PHP file is splitting these entries into two, because of the comma, and is displaying the quote marks instead of using the quote marks to indicate that this is a field that includes a comma and space.
For example, the CSV file looks like this:
AA,BB,"Surname, Initial",CC,DD,EE
"Surname, and Initial" should be in the same field in the resulting table, but instead two fields are being created.
I really would like to solve this in the PHP code, and not by tinkering with the CSV file itself. The code is here:
echo "<meta content=\"text/html; charset=utf-8\" http-equiv=\"Content-Type\"/>
<link rel=\"stylesheet\" type=\"text/css\" href=\"test.css\">";
$file = 'CSV/test.csv';
$lines = file($file);
if (file_exists($file)){
//echo "$file exists<br/>";
echo '<table>'; //create the html table
$x=2; //define the first line to display
$y=20; //define the last line to display
$line_counter=0;
foreach ($lines as $line) { //loop to fill your table with csv data
if($x<=$line_counter && $line_counter<=$y)
{
$expl = explode(",", $line); //explode each line on "," to create the rows
echo "<tr>"; //create each line of your table
$c=0;
while ($c<=47) { //loop to extract columns 2 to 47
if(($c % 2) == 1){ //odd rows
echo "<td class=\"odd\">".$expl[$c]."</td>";
}
elseif(($c == 0)){
echo "<td class=\"even\">".$expl[$c]."</td>";
}
else{
echo "<td class=\"even\">".$expl[$c]."</td>"; //even rows
}
$c++;
}
echo "</tr>";
$x++;
}
$line_counter++;
}
echo "</table>";
}