I have a html table structure like this;
<tr style="font-weight: bold">
<td>ID</td>
<td>Navn</td>
<td>Adresse</td>
<td>By</td>
<td>Post nr</td>
<td>E-mail</td>
<td>Telefon</td>
<td>Status og dato</td>
<td>Dropdown info</td>
<td>Produkt info</td>
<td>Buydate</td>
<td>Ref nr. (3 første cifre)</td>
</tr>
<tr>
<td>40563</td>
<td>Firstname Lastname</td>
<td>Address</td>
<td>Copen</td>
<td>2100</td>
<td>[email protected]</td>
<td>123123</td>
<td>Ikke indløst</td>
<td>EEE-BBB</td>
</tr>
I would like to convert this into a csv/excel file by php.
So each is a row in excel, and each is a cell in the row,
Please how can this be done?
I have researched and found Converting HTML Table to a CSV automatically using PHP? but the answer does not work properly for me, Im getting all the cell results in one 'cell', so each row only have one cell.
This is what i have tried;
$html = str_get_html($table);
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename=sample.csv');
$fp = fopen("php://output", "w");
foreach($html->find('tr') as $element)
{
$td = array();
foreach( $element->find('td') as $row)
{
$td [] = $row->plaintext;
}
fputcsv($fp, $td);
}
fclose($fp);
exit;
Where $table is the html above. Using simple html dom plugin