PHP code example of jdz / table

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

    

jdz / table example snippets


use JDZ\Table\Table;

// Create a table with headers
$table = new Table(['Name', 'Age', 'City']);

// Add rows
$table->addRow(['John Doe', '30', 'New York'])
      ->addRow(['Jane Smith', '25', 'Los Angeles']);

// Render the table
echo $table->render();

$table = new Table(['Column 1', 'Column 2', 'Column 3']);

use JDZ\Table\Th;

$header1 = new Th('Name');
$header1->setStyle('color', 'blue');

$table = new Table([$header1, 'Age', 'Email']);

$table->addRow(['Alice', '28', '[email protected]']);

use JDZ\Table\Td;

$cell = new Td('Important');
$cell->setStyle('color', 'red')
     ->setAttribute('class', 'highlight');

$table->addRow([$cell, 'Data', 'More data']);

$table->setColumnWidth(0, 50)  // First column: 50%
      ->setColumnWidth(1, 25)  // Second column: 25%
      ->setColumnWidth(2, 25); // Third column: 25%

use JDZ\Table\Td;

$spanningCell = new Td('This spans 2 columns', 2);
$table->addRow([$spanningCell, 'Normal cell']);

$cell = new Td('Content');
$cell->setAttribute('class', 'my-class');

$cell = new Td('Content');
$cell->setStyle('color', 'red')
     ->setStyle('font-weight', 'bold')
     ->setStyle('background-color', '#ffff99');

$cell = new Td('Content');
$cell->setAttribute('class', 'highlight')
     ->setAttribute('id', 'cell-1')
     ->setAttribute('data-value', '123');

use JDZ\Table\Th;

$header = new Th('Status');
$header->setWidth(20)  // 20% width
       ->setStyle('color', 'blue')
       ->setAttribute('class', 'header-cell');

use JDZ\Table\Table;
use JDZ\Table\Td;

// Create table with headers
$table = new Table(['Product', 'Price', 'Stock']);

// Set column widths
$table->setColumnWidth(0, 50)
      ->setColumnWidth(1, 25)
      ->setColumnWidth(2, 25);

// Add rows with styled cells
$priceCell = new Td('$999');
$priceCell->setStyle('color', 'green')
          ->setStyle('font-weight', 'bold');

$stockCell = new Td('Low');
$stockCell->setStyle('color', 'red');

$table->addRow(['Laptop', $priceCell, $stockCell]);
$table->addRow(['Mouse', '$29', '150']);

// Render
echo $table;  // Uses __toString() magic method