API reference
Exporting data in tabluar form is a reoccurring task in any development project. This task is quickly accomplished by using the Topincs API. The next video shows the implementation of such a service and following you find another example from a different use case, how the computational and presentational code for such a service look like.
Check it out
<?php
// Not from Santa, but from a different system with invoices.
$columns = [
// Just the name, if the values are strings:
"Invoice nr",
// Otherwise, add datatype and format:
["name" => "Invoice date", "datatype" => "date", "format" => "dformat"],
// Format is optional on integers:
["name" => "Issue count", "datatype" => "integer"],
["name" => "Hour rate", "datatype" => "decimal", "format" => "mformat"]
];
$rows = [];
foreach (Tobject::all_invoices() as $invoice) {
$rows[] = (object) [
"nr" => $invoice->get_invoice_number(),
"date" => $invoice->get_invoice_date(),
"count" => $invoice->count_issues()
"rate" => $invoice->get_hourly_rate()
];
});
function mformat($n) {
return number_format($n, 2, ",", "");
}
function dformat($date) {
// null will not arrive here, so no need to check:
return $date->format("d.m.Y");
}
<?php
content_type("csv", "attachment", "export.csv");
// For easy debugging:
// content_type("plain");
echo table(rows: $rows, columns: $columns, format: "csv");
This page cannot be displayed in your browser. Use Firefox, Opera, Safari, or Chrome instead.
Saving …