Home > Software engineering >  How to place HTML tags in a CSV?
How to place HTML tags in a CSV?

Time:07-26

I have an excel sheet. In this excel sheet it contains Parts Number, Summary, Brand, Status and Country. I need to reprocess this csv to create another csv. I would like to be able to import CSV data which contains text that already has html so I don’t have to apply all the formatting after import.

In this new csv, it should only contain 3 columns, Parts Number, Summary and html table. In this html table, it contains Parts Number, Summary, Brand, Status and Country. Parts Number and Summary should be in plain text whereas the third column should be in a html table.

I have already created a table using php and it worked, now I am having difficulty trying to bring the table to csv. The end result in the csv should look like this,

12345, summary text, <div ></div>

Here is my code:

<?php
$file = fopen("Book1.csv","r");
$file2 = fopen("Book2.csv","w");
$data = [];
$description = '<table >';
while($row = fgetcsv($file)) {
$data[] = $row; //Get all the data
}
if($data){  
    $n_columns = count($data[0]); //Get number of columns 
}
$description .= '<table border="1">'; 
for ($col = 0; $col < $n_columns; $col  ) {
    $description .= '<tr>';
    foreach ($data as $row_k => $row) {
        if ($row_k == 0) {
            $description .= '<th>';
        } else {
            $description .= '<td>';
        }

        $description .= $row[$col] ?? '';
        if ($row_k == 0) {
            $description .= '</th>';
        } else {
            $description .= '</td>';
        }
    }
    $description .= '</tr>';
    fputcsv($file2, $data); // line 50
}
$description .= '</table>';
fclose($file);
fclose($file2);
?>

The error message I receive is:

Notice: Array to string conversion in C:\Users\Marketing\Desktop\xampp\htdocs\csvtable.php on line 50

Here is how Book1.csv looks like: enter image description here

I am desperate, any help is appreciated.

CodePudding user response:

If I understood you correctly, this should accomplish the task. It uses 3 functions, array_to_csv, csv_to_array and build_table.

<?php

function csv_to_array($filename, $header_row=true)
{
    $handle = fopen($filename, 'r');
        
    if ($header_row === true)
        $header = fgetcsv($handle);

    $data = [];
    while ($row = fgetcsv($handle)) {
        if ($header_row)
            $data[] = array_combine($header, $row);
        else
            $data[] = $row;
    }
    return $data;
}

function array_to_csv($data, $filename, $header_row=true)
{
    $handle = fopen($filename, 'w');

    if ($header_row == true) {
        $header = array_keys($data[0]);
        fputcsv($handle, $header);
    }

    foreach ($data as $line) {
        fputcsv($handle, $line);
    }
    fclose($handle);
}

function build_table($array)
{
    $table = '<table><thead><tr>';
    $headings = array_keys($array[0]);
    foreach ($headings as $heading)
        $table .= '<th>' . $heading . '</th>';    
    $table .= '</tr></thead>';

    $table .= '<tbody>';
    foreach ($array as $row) {
        $table .= '<tr>';
        foreach ($row as $element) {
            $table .= '<td>' . $element . '</td>';
        }
        $table .= '</tr>';
    }
    $table .= '</tbody>';
    return $table;
}

$book1 = csv_to_array('Book1.csv');
$book2 = [];
foreach ($book1 as $row) {
    $key = $row['Parts Number'];
    if (!isset($book2[$key])) {
        $book2[$key] = [
            'Parts Number' => $key,
            'Summary' => $row['Summary']
        ];
    }
    $book2[$key]['rows'][] = $row;
}

foreach ($book2 as &$product) {
    $product['html table'] = build_table($product['rows']);
    unset($product['rows']);
}
unset($product);
$book2 = array_values($book2);

array_to_csv($book2, 'Book2.csv');

Book1.csv contains:

Parts Number,Summary,Brand,Status,Country
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,Discontinued,Germany
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,Discontinued,France
6SE1200-1EA70-1,SIMOVERT P 6,Siemens,In Stock,England
6SE3012-0BA00,SIMOVERT P M,Siemens,Discontinued,-

Book2.csv output:

"Parts Number",Summary,"html table"
6SE1200-1EA70-1,"SIMOVERT P 6","<table><thead><tr><th>Parts Number</th><th>Summary</th><th>Brand</th><th>Status</th><th>Country</th></tr></thead><tbody><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>Discontinued</td><td>Germany</td></tr><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>Discontinued</td><td>France</td></tr><tr><td>6SE1200-1EA70-1</td><td>SIMOVERT P 6</td><td>Siemens</td><td>In Stock</td><td>England</td></tr></tbody></table>"
6SE3012-0BA00,"SIMOVERT P M","<table><thead><tr><th>Parts Number</th><th>Summary</th><th>Brand</th><th>Status</th><th>Country</th></tr></thead><tbody><tr><td>6SE3012-0BA00</td><td>SIMOVERT P M</td><td>Siemens</td><td>Discontinued</td><td>-</td></tr></tbody></table>"
  • Related