markfullmer/datainterface

Web interface for filtering records from a CSV file

1.1.0 2022-09-18 22:29 UTC

This package is auto-updated.

Last update: 2024-04-28 23:12:29 UTC


README

https://github.com/markfullmer/datainterface

This PHP library will automatically parse a CSV file and generate a search interface, displaying results in a table.

Examples

The library takes a CSV file with a header row as its input, as well as arguments for which column names should be filterable and which columns should display in the table. The library provides two main methods for rendered output: the form, and the table.

Specifications & Usage

  1. Create a standard-format CSV file with a header row. A simple example is below:
Artist,Title,Year,Genre
America,America,1971,Rock
Eric Andersen,Bout Changes 'n' Things Take 2,1966,Rock
  1. Add this PHP library to your project in the standard way:
composer require markfullmer/datainterface
  1. Instantiate the data as a PHP class object. In the example below, the code is being instructed to create a filter for 'Year' and 'Genre' (but not 'Artist') and to display 'Artist', 'Title', and 'Year' in the table:
use markfullmer\datainterface\DataInterface;

$app = new DataInterface([
  'file' => 'records.csv',
  'title' => 'List of LPs',
  'filters' => ['Year', 'Genre'],
  'table_columns' => ['Artist', 'Title', 'Year'],
]);

The library also supports reading CSV data over HTTP (including public, published Google Sheets as CSV). To use this approach, provide a url parameter:

$app = new DataInterface([
  'file' => 'https://example.com/records.csv',
  'title' => 'List of LPs',
  'filters' => ['Year', 'Genre'],
  'table_columns' => ['Artist', 'Title', 'Year'],
]);

If reading data over HTTP, the library stores the URL in the filesystem for performance reasons. To refresh the request, add ?reset to the website URL.

  1. Render the parts of the interface as desired:
echo $app->options['title'];
echo $app->buildForm();
echo $app->buildTable();

Features

  • Metadata columns can have multiple values. These should be separated by a semicolon. For example, for an entry that has metadata relating to "Genre" of "Horror" and "Thriller", the CSV file's row for that metadata should be entered as "Horror;Thriller".

Possible enhancements

  • Support other data structure inputs, such as JSON
  • Keyword search (taking column names as parameters for targets)
  • Cache parsing of data for filters for performance