PHP code example of rafaelwendel / ci4tablemaker

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

    

rafaelwendel / ci4tablemaker example snippets




namespace App\Controllers;

use CI4TableMaker\TableMaker; // If installed via Composer, or use App\Libraries\TableMaker; if local

class Users extends BaseController
{
    public function index(): string
    {
        // 1. Fetch user data (e.g., from a Model)
        $userModel = new \App\Models\UserModel();
        $users = $userModel->findAll(); // Array of records containing: id, name, profile

        // 2. Instantiate TableMaker passing CodeIgniter 4's Table library
        $tableMaker = new TableMaker(new \CodeIgniter\View\Table());

        // 3. Set table template and headers. The same as \CodeIgniter4\View\Table::setTemplate() method.
        $template = [
            'table_open' => '<table class="table table-bordered table-striped">'
        ];
        $tableMaker->setTemplate($template);
        $tableMaker->setHeading(['ID', 'Name', 'Profile', 'Actions']);

        // 4. Set the data and the URL base for your action links
        $tableMaker->setData($users);
        $tableMaker->setUrlBase(base_url('users'));

        // 5. Add action links
        // It automatically replaces `{id}` with the "id" value from each row
        $tableMaker->addLink('edit/{id}', 'id', 'Edit', 'class="btn btn-primary btn-sm"');
        $tableMaker->addLink('delete/{id}', 'id', 'Delete', 'class="btn btn-danger btn-sm"');

        // 6. Send the instance to your View
        return view('users/index', ['table' => $tableMaker]);
    }
}

<div class="container">
    <h1>Users List</h1>
    <?= $table 

$condition = [
    'field'    => 'profile',
    'operator' => '==',
    'value'    => 'Admin'
];

$tableMaker->addLink(
    'settings/{id}', 
    'id', 
    'Advanced Settings', 
    'class="btn btn-warning btn-sm"', 
    $condition
);

// Supports replacing multiple properties found in the row data
$tableMaker->addLink(
    'departments/{profile}/users/{id}/details', 
    null, 
    'View Details', 
    'class="btn btn-info btn-sm"'
);

// Your data may contain: id, name, profile, and password_hash.
// To only display Name and Profile in this exact order:
$tableMaker->setColumns(['name', 'profile']);