This example shows how to handle data requests and modify SQL queries or parameters on the server side using
Stimulsoft Viewer for PHP. First, include the Stimulsoft libraries:
<?php
require_once '../vendor/autoload.php';
use Stimulsoft\Events\StiDataEventArgs;
use Stimulsoft\Report\StiReport;
use Stimulsoft\Viewer\StiViewer;
?>
Next, create a
Viewer object and configure JavaScript-related options:
$viewer = new StiViewer();
$viewer->javascript->relativePath = '../';
$viewer->javascript->appendHead('<link rel="shortcut icon" href="/../favicon.ico" type="image/x-icon">');
Define viewer events before processing. You can assign a PHP function, the name of a JavaScript function, or a JavaScript function as a string. Multiple event handlers can be added using the
append() method. In this example, the
onBeginProcessData event is used to modify the data connection, SQL queries, and query parameters:
$viewer->onBeginProcessData = function (StiDataEventArgs $args) {
if ($args->connection == 'MyConnectionName')
$args->connectionString = 'Server=localhost; Database=northwind; UserId=root; Pwd=;';
if ($args->dataSource == 'MyDataSource')
$args->queryString = 'SELECT * FROM MyTable';
if ($args->dataSource == 'MyDataSourceWithParams') {
$args->parameters['Parameter1']->value = 'TableName';
$args->parameters['Parameter2']->value = 10;
$args->parameters['Parameter3']->value = '2019-01-20';
}
};
Process the request on the server side using
process():
$viewer->process();
Create a
Report object and load a report template. The
loadFile() method does not load the report on the server side; it only generates JavaScript code for the client side:
$report = new StiReport();
$report->loadFile('../reports/SimpleListSQL.mrt');
Assign the report to the viewer:
$viewer->report = $report;
Finally, render the viewer as a complete HTML page using
printHtml():
$viewer->printHtml();