PHP Arrays

With DataKit SDK, you can render a DataView by passing it a pure PHP array.

Sometimes, you just want to render an array of data! It shouldn’t be hard. You now have a solution.

This is a DataView 👇

On this page, we embedded a shortcode referencing a DataView named: php-array-demo.

HTML
[dataview id="php-array-demo"]

Now we just need to create it!

Creating the DataView using a few lines of PHP

And here’s the code that use used to render it.

First, we create an array of features to display. The indexes (0, 1, and 2) will be referenced as the field IDs later. These can be string keys as well.

PHP
$datakit_features = [
  [
    0 => 'PHP Array Data Source',
    1 => 'Display data from a PHP array. That is what you are looking at right now.',
    2 => 'Done',
  ],
  // …Etc.
];

Next register a data source that we will pass to the DataView. This is an ArrayDataSource.

PHP
$array_data_source = new ArrayDataSource( 'php-array-demo', $datakit_features );

Now, we have the data, the DataSource…we just need to render it!

We register each of the fields we want to display in the View. Fields can be visible or hidden: visible fields can be toggled using the view controls.

There are many different field types. In this demo, we show the TextField and StatusIndicatorField. The StatusIndicatorField has default values that can be overriden; we override them to show feature progress.

PHP
$php_array_demo   = DataView::table(
	'php-array-demo', // The ID that will be used in the shortcode.
	$array_data_source, // The array of PHP data to render.
	[
		TextField::create( 0, 'Feature' ),
		TextField::create( 1, 'Description' ),
		StatusIndicatorField::create( 2, 'Status' )
			->show_value()
      ->mapping(
				'Done',
				'In Progress',
				'Not Planned',
				'Blocked',
				'Not Started'
      ),
	],
	Sort::asc( 0 ) // Sort by feature name (column 0).
);

Finally, we register the DataView:

PHP
do_action( 'datakit/dataview/register', $php_array_demo );

When you register the DataView using datakit/dataview/register, it automatically registers a shortcode.


Discover more from DataKit

Subscribe to get the latest posts and announcements sent to your email.