Generating Reports with Core PHP

0 0
Read Time:4 Minute, 18 Second

In the world of web development, generating reports is a common requirement. Whether it’s for data analysis, user activity summaries, or financial reports, PHP offers a variety of ways to generate reports. In this blog post, we’ll explore different methods to create reports using Core PHP, covering HTML, CSV, Excel, PDF, and template-based reports.

1. Simple HTML Reports

One of the easiest ways to generate reports is by creating simple HTML files. This method is suitable for basic reporting needs and is very straightforward. Here’s how you can do it:

<?php
header('Content-Type: text/html');
header('Content-Disposition: attachment; filename="report.html"');

// Sample data
$data = [
    ['Name' => 'Alice', 'Age' => 30],
    ['Name' => 'Bob', 'Age' => 25],
];

echo "<html><body>";
echo "<h1>Report</h1>";
echo "<table border='1'>";
echo "<tr><th>Name</th><th>Age</th></tr>";

foreach ($data as $row) {
    echo "<tr><td>{$row['Name']}</td><td>{$row['Age']}</td></tr>";
}

echo "</table>";
echo "</body></html>";
?>

This script generates an HTML table with sample data, which can be downloaded as an HTML file.

2. CSV Files

CSV files are a popular format for exporting data due to their simplicity and compatibility with various applications like Microsoft Excel. PHP makes it easy to generate CSV files using the fputcsv function.

<?php
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="report.csv"');

// Sample data
$data = [
    ['Name', 'Age'],
    ['Alice', 30],
    ['Bob', 25],
];

// Open output stream
$output = fopen('php://output', 'w');

// Write data to CSV
foreach ($data as $row) {
    fputcsv($output, $row);
}

// Close output stream
fclose($output);
?>

This script creates a CSV file with sample data that users can download.

3. Excel Files

Creating Excel files in Core PHP is more complex compared to HTML or CSV. However, it can be done using Excel-compatible XML. For more advanced features, using libraries like PhpSpreadsheet is recommended. Here’s a simple approach to create Excel-compatible XML:

<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="report.xls"');

// Sample data
$data = [
    ['Name', 'Age'],
    ['Alice', 30],
    ['Bob', 25],
];

echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"';
echo ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
echo ' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
echo '<Worksheet ss:Name="Sheet1"><Table>';

foreach ($data as $row) {
    echo '<Row>';
    foreach ($row as $cell) {
        echo '<Cell><Data ss:Type="String">' . htmlspecialchars($cell) . '</Data></Cell>';
    }
    echo '</Row>';
}

echo '</Table></Worksheet></Workbook>';
?>

This generates an Excel file that can be opened in Excel or other spreadsheet applications.

4. PDF Files

Generating PDF reports in Core PHP requires a library since PHP does not natively support PDF creation. One popular library is FPDF. Here’s a basic example of how to create a PDF using FPDF:

Example:

  1. Download FPDF from FPDF.org and include it in your project.
  2. Generate PDF:
<?php
require('fpdf.php');

// Create instance of FPDF
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);

// Sample data
$data = [
    ['Name' => 'Alice', 'Age' => 30],
    ['Name' => 'Bob', 'Age' => 25],
];

$pdf->Cell(40, 10, 'Name');
$pdf->Cell(40, 10, 'Age');
$pdf->Ln();

foreach ($data as $row) {
    $pdf->Cell(40, 10, $row['Name']);
    $pdf->Cell(40, 10, $row['Age']);
    $pdf->Ln();
}

$pdf->Output('D', 'report.pdf');
?>

This script generates a PDF document with sample data and forces a download of the PDF file.

5. Template-Based Reports

For more complex reporting, you might want to use templates. While PHP does not have built-in template engines, you can use PHP’s include or require functions to integrate templates into your reports.

<!DOCTYPE html>
<html>
<head>
    <title>Report</title>
</head>
<body>
    <h1>Report</h1>
    <table border="1">
        <tr><th>Name</th><th>Age</th></tr>
        <?php foreach ($data as $row): ?>
        <tr><td><?php echo htmlspecialchars($row['Name']); ?></td><td><?php echo htmlspecialchars($row['Age']); ?></td></tr>
        <?php endforeach; ?>
    </table>
</body>
</html>
<?php
// Sample data
$data = [
    ['Name' => 'Alice', 'Age' => 30],
    ['Name' => 'Bob', 'Age' => 25],
];

// Include template
include('report_template.php');
?>

This approach separates the report structure from the data, making it easier to manage complex reports.

Conclusion

Generating reports with Core PHP offers flexibility and a range of options depending on your needs. Whether you’re creating simple HTML reports, CSV files for data export, Excel-compatible files, PDFs, or using templates for more complex structures, Core PHP provides the tools you need to create effective and professional reports.

Feel free to experiment with these methods to find the one that best suits your reporting requirements. Happy coding!

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

About Author

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Reply

Your email address will not be published. Required fields are marked *