PHP code example of hstanleycrow / easyphpreports

1. Go to this page and download the library: Download hstanleycrow/easyphpreports 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/ */

    

hstanleycrow / easyphpreports example snippets




stanleycrow\EasyPHPReports\PHPReport;
use hstanleycrow\EasyPHPReports\CellFormat;

// 1. Describe the columns.
$columns = [
    ['header' => 'No',     'format' => CellFormat::NUMBER, 'calculate' => false],
    ['header' => 'Date',   'format' => CellFormat::DATE,   'calculate' => false],
    ['header' => 'Branch', 'format' => CellFormat::TEXT,   'calculate' => false],
    ['header' => 'Sales',  'format' => CellFormat::MONEY,  'calculate' => true],
];

// 2. Provide the rows (values in the same order as the columns above).
$rows = [
    ['no' => 1, 'date' => '2023-12-08', 'branch' => 'Downtown', 'amount' => 24.40],
    ['no' => 2, 'date' => '2023-12-08', 'branch' => 'Uptown',   'amount' => 17.25],
    ['no' => 3, 'date' => '2023-12-08', 'branch' => 'Airport',  'amount' => 29.50],
];

// 3. Register the built-in report types once.
PHPReport::initialize();

// 4. Render an on-screen HTML table.
$report = new PHPReport(PHPReport::SCREEN, $columns, $rows);
echo $report->generate();



use hstanleycrow\EasyPHPReports\PHPReport;

PHPReport::initialize();

$path = 'exportedReports/sales-' . date('YmdHis'); // no extension needed

$report = new PHPReport(PHPReport::EXCEL, $columns, $rows, $path);
$file   = $report->generate(); // e.g. "exportedReports/sales-20231208101500.xlsx"

if ($report->isDownloadable()) {
    // serve $file for download, store it, e-mail it, etc.
    header('Content-Disposition: attachment; filename="' . basename($file) . '"');
    readfile($file);
}