TableMagic is a powerful and flexible PHP library designed for creating and manipulating tables in console output. Inspired by Python's PrettyTable, TableMagic empowers developers to effortlessly display tabular data with customizable alignment, sorting, and styling options.
- Easy Table Creation: Quickly create tables with headers and rows.
- Dynamic Row Addition: Add rows easily with support for automatic column width adjustment.
- Customizable Column Alignments: Align columns to the left, right, or center.
- UTF-8 Support: Properly display non-ASCII characters.
- Sorting Capability: Sort tables by any column in ascending or descending order.
- Export and Import Options: Import data from CSV, JSON, and XML formats, and export to HTML, CSV, JSON, and XML.
- Terminal Interaction: Paginate through large tables in the console.
Here is the structure of the repository:
You can install TableMagic using Composer:
composer require chernegasergiy/table-magic
Here's a quick example to get you started with TableMagic:
require_once 'vendor/autoload.php';
use ChernegaSergiy\TableMagic\Table;
// Define headers and alignments
$headers = ['Employee ID', 'Name', 'Department', 'Performance Score', 'Review Date'];
$alignments = [
'Employee ID' => 'r',
'Performance Score' => 'r',
'Review Date' => 'c',
];
// Create a new Table instance
$table = new Table($headers, $alignments);
// Add rows to the table
$table->addRow([1001, 'Alice Thompson', 'Marketing', 85, '2024-06-15']);
$table->addRow([1002, 'Brian Lee', 'Sales', 90, '2024-06-18']);
$table->addRow([1003, 'Carol Martinez', 'Engineering', 88, '2024-06-20']);
// Display the table
echo $table;
// Sort the table by 'Performance Score' (descending order)
$table->sortTable('Performance Score', 'desc');
echo "\n\nSorted by Performance Score (Descending):\n";
echo $table;
This will output:
+-------------+----------------+-------------+-------------------+-------------+
| Employee ID | Name | Department | Performance Score | Review Date |
+-------------+----------------+-------------+-------------------+-------------+
| 1001 | Alice Thompson | Marketing | 85 | 2024-06-15 |
| 1002 | Brian Lee | Sales | 90 | 2024-06-18 |
| 1003 | Carol Martinez | Engineering | 88 | 2024-06-20 |
+-------------+----------------+-------------+-------------------+-------------+
Sorted by Performance Score (Descending):
+-------------+----------------+-------------+-------------------+-------------+
| Employee ID | Name | Department | Performance Score | Review Date |
+-------------+----------------+-------------+-------------------+-------------+
| 1002 | Brian Lee | Sales | 90 | 2024-06-18 |
| 1003 | Carol Martinez | Engineering | 88 | 2024-06-20 |
| 1001 | Alice Thompson | Marketing | 85 | 2024-06-15 |
+-------------+----------------+-------------+-------------------+-------------+
You can import data into the table from various formats:
$tableImporter = new TableImporter();
$table = $tableImporter->import($data, 'json'); // 'csv', 'xml' are also supported
Export your table to different formats:
$tableExporter = new TableExporter($table);
$htmlOutput = $tableExporter->export('html'); // 'csv', 'json', 'xml' are also supported
For large datasets, you can interactively paginate through the table:
$terminalInteraction = new TerminalInteraction($table);
$terminalInteraction->run();
Contributions are welcome and appreciated! Here's how you can contribute:
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Please make sure to update tests as appropriate and adhere to the existing coding style.
This project is licensed under the CSSM Unlimited License v2 (CSSM-ULv2). See the LICENSE file for details.
- Inspired by the Python PrettyTable library
- Thanks to all contributors who have helped shape TableMagic