DEV Community

Cover image for Automate HTML Tables Generation in PHP with this Modern Library
ucscode
ucscode

Posted on

Automate HTML Tables Generation in PHP with this Modern Library

Imagine writing a single line of code that will generate a fully functional table like the one below:

Image description

Interestingly, this is not based on random data but one that fully aligns with your project. Yes, a simple line of code can create a modular and customizable table from your database information.


The Struggle With HTML Tables

If you've ever worked with HTML tables in PHP, you know how tedious they can be. Manually writing out <table>, <tr>, and <td> elements for dynamic content quickly becomes messy and unmanageable. Developers often resort to concatenating strings or using templating engines, but these approaches come with their own challenges:

  • Error-prone: A missing closing tag can break your entire layout.
  • Difficult to maintain: Making changes to table structure requires rewriting large chunks of code.
  • Limited flexibility: Applying different styles, attributes, or handling special cases can get complicated.
  • Reusability: Managing dynamic data across multiple tables can be cumbersome.

Tables are one of the most common elements in web development. Whether it's an E-commerce product listing, admin user management, or a helpdesk ticket system, generating tables is a repetitive task.


Introducing: The Ucscode Table Generator

Wouldn’t it be great if generating tables in PHP were as simple as working with arrays or objects? That’s exactly what Ucscode/table-generator offersβ€”a structured, object-oriented approach to creating and managing HTML tables efficiently.

πŸš€ Basic Usage: Creating a Simple Table

Let’s start with a simple example. Suppose you want to generate a table displaying user data from an associative array:

$data = [
    [
        'id' => 1, 
        'name' => 'John Doe', 
        'email' => 'john@example.com'
    ],
    [
        'id' => 2,
        'name' => 'Jane Doe', 
        'email' => 'jane@example.com'
    ]
];
Enter fullscreen mode Exit fullscreen mode

With Ucscode Table Generator, you can generate a table effortlessly:

use Ucscode\HtmlComponent\TableGenerator\Adapter\AssocArrayAdapter;
use Ucscode\HtmlComponent\TableGenerator\TableGenerator;

$adapter = new AssocArrayAdapter($data);
$tableGenerator = new TableGenerator($adapter);

echo $tableGenerator->render();
Enter fullscreen mode Exit fullscreen mode

πŸ–₯️ Output:

<table>
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Email</th>
    </tr>
    <tr>
        <td>1</td>
        <td>John Doe</td>
        <td>john@example.com</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Jane Doe</td>
        <td>jane@example.com</td>
    </tr>
</table>
Enter fullscreen mode Exit fullscreen mode

πŸ” Breaking It Down

Adapters: Making Data Work

The AssocArrayAdapter is just one of many adapters. An adapter is responsible for accepting a specific data type and converting it into a format that the TableGenerator can process.

  • Working with an associative array? Use AssocArrayAdapter.
  • Fetching data from MySQL? Use MysqliResultAdapter.
  • Want to process API responses? You can create a custom adapter.

TableGenerator: Your Table Engine

The TableGenerator takes the adapter and processes the extracted data into an HTML table. This method keeps data separate from presentation logic, improving clarity and maintainability.

βœ… Why This Approach Works

  • No messy HTML strings – The library handles all the markup.
  • Clearer structure – Data remains separate from presentation.
  • Easier maintenance – Updating the table structure is straightforward.

πŸ”₯ Advanced Features

Adapters for Different Data Sources

One powerful feature of this library is its ability to work with various data sources through Adapters. For instance, if your data comes from an API, you can create a CustomApiJsonAdapter to process the response effortlessly.

Customization: Enhancing Your Table

By default, the table is plain. But you can fully customize it with Middleware to modify columns, apply styles, or add interactive elements.

Middleware: The Power of Modification

Middleware allows you to transform table data dynamically without modifying the core logic.

πŸ“Œ Example: Adding Checkboxes to Rows

Checkboxes are essential for bulk actions. Instead of manually adding <input> elements, use CheckboxMiddleware:

use Ucscode\HtmlComponent\HtmlTableGenerator\Middleware\CheckboxMiddleware;

$tableGenerator->addMiddleware(new CheckboxMiddleware());
Enter fullscreen mode Exit fullscreen mode

This automatically inserts a checkbox into the first column of each row.


πŸ“Œ Why Choose Ucscode/table-generator?

  • Lightweight – No unnecessary dependencies.
  • Flexible API – Works for both simple and complex use cases.
  • Extensible – Supports custom middlewares and adapters.
  • Beginner-Friendly – Quick setup with minimal learning curve.
  • Open Source – Free to use, modify, and contribute!

πŸš€ Get Started Today!

If you're tired of manually generating HTML tables in PHP, give ucscode/table-generator a try! It's an elegant solution for creating tables in a structured, maintainable way.

πŸ”— Check out the full documentation

πŸ”— Check out the repository on GitHub

Have feedback or feature suggestions? Drop a comment below! πŸ‘‡

Top comments (0)