Generating Dynamic PHP Reports with Smart Report Engine: A Comprehensive Tutorial

A.karim Amin
3 min readAug 15, 2023

In this tutorial, you’ll learn how Smart Report Engine can add reporting features to your project.

We’ll mainly focus on the open-source community edition, which you can get in a few ways: by downloading it directly from here, cloning this public repository as follows:

git clone https://github.com/webuccinoco/PHP-Reporting-Library-For-MYSQL

Alternatively, if you’re using Composer, you have the alternative option of employing the following Composer command:

composer create-project webuccinoco/sre-community

The package you’ve downloaded should resemble a structure similar to the following:

├── SmartReportingEngine/
├── sre_config/
└── config.php
├── sre_reports/
├── db/
└── example.sql
├── examples/
└── sre_bootstrap.php
└── composer.json
  • Inside the SmartReportEngine directory, you’ll find the core components of Smart Report Engine.
  • The sre_reports directory serves as the repository for all automatically generated reports. When a new report is created by Smart Report Engine, a corresponding sub-directory is automatically generated under sre_reports, using the report’s name. Within this automatically generated subdirectory, the content of the new report is placed. For instance, if a report named “Sales” is automatically generated, you’ll find it at /sre_reports/repSales/repSales.php. Furthermore, all configuration details specific to this automatically generated report will be located at /sre_reports/repSales/config.php.
  • In situations where a name isn’t explicitly assigned to the report, Smart Report Engine will assign a name based on its creation timestamp. This results in a structure similar to: sre_reports/rep1691646585168400/rep1691646585168400.php.
  • The primary configuration of Smart Report Engine should be located in sre_config/config.php. Inside this file, you are required to establish the connection to your MySQL database, as this connection is crucial for the generation of your reports.
  • Several examples are available within the /examples directory. To execute these examples successfully, it’s necessary to import an existing MySQL “items” table from the /db/example.sql file. Importing this file will create an “Items” table within your MySQL database. To execute any of these examples, just access their URLs from your web browser. This will enable you to view the generated report based on the code in each example.

Following are some reports generated by Smart Report Engine:

An example report generated by Smart Report Engine — Community Edition.

Code Walkthrough: Understanding an Example

To generate a report using Smart Report Engine the code will be something like the following:

use SRE\Engine\CustomEngine;
use SRE\Engine\ReportOptions;

require_once "sre_bootstrap.php";

try {

$report = new ReportOptions();
$report->select_tables("items")
->set_grouping("country")
->sort_by("country", 1)
->filter_like("category", "sunglasses")
->filter_between("price", 15, 50)
->filter_more("rating", 3.5)
->filter_not_null("code")
->set_title("Using data filters")
->select_fields(array("id","code","name","price","reorder_level","units_in_stock","category","country","rating"));
$engine = new CustomEngine($report);
$report_path = $engine->create_report();
if ($report_path) {
// The user will be redirected to the URL of the generated report. All generated reports are stored as subdirectories under /sre_reports.
header("location: ".$report_path);
exit();
}
} catch (Exception $e) {
echo $e->getMessage();
}

The given code demonstrates a basic example of using Smart Report Engine effectively. Let’s walk through the code step by step to understand how it functions:

  • Namespaces: The given code utilizes two namespaces, “SRE\Engine\CustomEngine” and “SRE\Engine\ReportOptions.” These namespaces help organize and access specific parts of the code.
  • Requiring the autoloader: If you manually downloaded the community edition (without using Composer), you need to add a special file called “sre_bootstrap.php” to your code. Otherwise, if Composer has been used, you’ll have to include the Composer autoloader file “vendor/autoload.php”.
  • Creating the ReportOptions Object: The code initializes an object from the “ReportOptions” class. This object is responsible for defining the options needed for your report. In the code example provided earlier, we applied some member methods of the ReportOptions class, enabling us to achieve the following:
  • Combining specific data filter methods to extract records and display a report based on selected criteria.
  • Sorting and grouping the report by country in descending order.
  • Instead of displaying all columns from the table, we chose to select only specific columns to be shown in the report.
  • Passing ReportOptions Object to CustomEngine: Once you have set your report options, you pass the “ReportOptions” object to the constructor of the “CustomEngine” class. This class handles the creation of your report based on the provided options.
  • Calling CreateReport Function: To generate your report, you call the “CreateReport” function using the “CustomEngine” object. This function processes the defined options and generates the report. Upon successful creation, it returns the URL of the report. All the reports generated using Smart Report Engine will be automatically saved in the “sre_reports” directory.

--

--