PHP code example of datatable / core

1. Go to this page and download the library: Download datatable/core library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

datatable / core example snippets


use DataTable\Core\Table;
use DataTable\Core\Writer\Csv as CsvWriter;

$table = new Table();
$table->setName("My data"); // Give it a user-friendly name

$namecolumn = $table->getColumnByName("name");
$emailcolumn = $table->getColumnByName("email");

// get the first row (index 0)
$row = $table->getRowByIndex(0);

// get a cell by columnname:
$cell = $row->getCellByColumnName("name");
// assign value to the cell
$cell->setValue("Joe Johnson");

// do the same for the second cell (email)
$cell = $row->getCellByColumnName("email");
$cell->setValue("[email protected]");


// do the same for a second row (index 1)
$row = $table->getRowByIndex(1);

$cell = $row->getCellByColumnName("name");
$cell->setValue("John Jackson");

$cell = $row->getCellByColumnName("email");
$cell->setValue("[email protected]");


// use a writer to export the datatable to a .csv file
$writer = new CsvWriter();
$output = $writer->write($table);
echo $output;


use DataTable\Core\Table;
use DataTable\Core\Reader\Csv as CsvReader;

// Create the DataTable\Core\Table object
$table = new Table();
$table->setName("My user data"); // Give it a user-friendly name
$reader->loadFile($table, "users.csv");

// Loop through all the rows in $table

foreach($table->getRows() as $row) {

  // Read field contents from the row by columnname    
  $name = $row->getValueByColumnName("name");
  $email = $row->getValueByColumnName("email");
    
  // use the data, for example:
  // ensure database record for user with name+email
}

use DataTable\Core\Table;
use DataTable\Core\Reader\Csv as CsvReader;
use DataTable\Core\Writer\AsciiTable as AsciiTableWriter;

// Create the DataTable\Core\Table object
$table = new Table();
$table->setName(basename($inputfile)); // Give it a user-friendly name

// Instantiate a Reader, in this case a .csv file reader
$reader = new CsvReader();
$reader->setSeperator(',');
$reader->loadFile($table, $inputfile);

// The $table now contains data from the .csv file

// Instantiate a Writer, in this case an Ascii table writer
$writer = new AsciiTableWriter();
$output = $writer->write($table);
echo $output;