The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections Settings

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

HTML conversion

<?php

class HTMLConverter
{
    public function convert(iterable $records): string
    public function table(string $class_name, string $id_value = ''): self
    public function td(string $fieldname_attribute_name): self
    public function tr(string $record_offset_attribute_name): self
}

HTMLConverter converts a CSV records collection into a HTML Table using PHP’s DOMDocument class.

Settings

Prior to converting your records collection into a HTML table, you may wish to configure optional information to improve your table rendering.

Because we are using internally the XMLConverter, if an error occurs while validating the submitted values a DOMException exception will be thrown.

HTMLConverter::table

<?php
public HTMLConverter::table(string $class_name, string $id_value = ''): self

This method sets the optional table class and id attribute values

The default class attribute value is table-csv-data.

The default id attribute value is the empty string.

HTMLConverter::tr

<?php
public HTMLConverter::tr(string $record_offset_attribute_name): self

This method sets the optional attribute name for the record offset on the HTML tr tag.

If none is use or an empty string is given, the record offset information won't be exported to the HTML table.

HTMLConverter::td

<?php
public HTMLConverter::td(string $fieldname_attribute_name): self

This method sets the optional attribute name for the field name on the HTML td tag.

If none is use or an empty string is given, the field name information won't be exported to the HTML table.

Conversion

<?php
public HTMLConverter::convert(iterable $records): string

The HTMLConverter::convert accepts an iterable which represents the records collection and returns a string.

<?php

use League\Csv\HTMLConverter;

//we fetch the info from a DB using a PDO object
$sth = $dbh->prepare("SELECT firstname, lastname, email FROM users LIMIT 2");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$sth->execute();

$converter = (new HTMLConverter())
    ->table('table-csv-data', 'users')
    ->tr('data-record-offset')
    ->td('title')
;

// The PDOStatement Object implements the Traversable Interface
// that's why Converter::convert can directly insert
// the data into the HTML Table
$html = $converter->convert($sth);

echo $html;

// <table class="table-csv-data" id="users">
// <tr data-record-offset="0">
// <td title="firstname">john</td>
// <td title="lastname">doe</td>
// <td title="email">john.doe@example.com</td>
// </tr>
// <tr data-record-offset="1">
// <td title="firstname">jane</td>
// <td title="lastname">doe</td>
// <td title="email">jane.doe@example.com</td>
// </tr>
// </table>

If needed you can use the CharsetConverter object to correctly encode your CSV records before conversion.