PHP Classes

Jupitern Table: Generate HTML tables from cell data and attributes

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 126 This week: 1All time: 9,407 This week: 560Up
Version License PHP version Categories
jupitern-table 1.0.3Free for non-comm...5HTML, PHP 5
Description 

Author

This package can generate HTML tables from cell data and attributes.

The main class can compose the definition of a HTML table using a fluent interface to define the table headers and body cell data from arrays, attributes, CSS styles.

The class generates HTML for the composed table is returned as a HTML string.

A separate class can also compose the table definition in terms of table columns.

Picture of Nuno Chaves
  Performance   Level  
Name: Nuno Chaves <contact>
Classes: 4 packages by
Country: Portugal Portugal
Age: 42
All time rank: 7929 in Portugal Portugal
Week rank: 411 Up3 in Portugal Portugal Up

Example

<?php

require '../../vendor/autoload.php';

$table = \Jupitern\Table\Table::instance()
    ->
setData([
        [
'id' => 1, 'name' => 'Peter', 'age' => '35', 'phone' => '961 168 851'],
        [
'id' => 2, 'name' => 'John', 'age' => '44', 'phone' => '169 899 742'],
        [
'id' => 3, 'name' => 'Peter', 'age' => '22', 'phone' => '737 853 346'],
        [
'id' => 4, 'name' => 'Clark', 'age' => '34', 'phone' => '169 574 741'],
        [
'id' => 5, 'name' => 'Alex', 'age' => '65', 'phone' => '732 753 467'],
    ])
// ->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])
   
->attr('table', 'id', 'demoTable')
    ->
attr('table', 'class', 'table table-bordered table-striped table-hover')
    ->
attr('table', 'cellspacing', '0')
    ->
attr('table', 'width', '100%')
    ->
attr('tr', 'data-text', 'bla bla bla bla bla')
    ->
attr('tr', 'data-id', function($row) {
        return
'row-' . $row['id'];
    })
    ->
css('tr', 'background-color', 'red')
    ->
column()
        ->
title('Name')
        ->
value(function ($row) {
            return
rand(1,10)%2 ? '<b>'.$row['name'].'</b>' : $row['name'];
        })
        ->
attr('td', 'data-text', 'bla bla bla')
        ->
css('th', 'color', 'green')
        ->
css('td', 'width', '50%')
        ->
css('td', 'background-color', '#ccc')
    ->
add()
    ->
column()
        ->
title('Age')
        ->
value('age')
        ->
css('th', 'color', 'red')
        ->
css('td', 'width', '20%')
    ->
add()
    ->
column('Phone')
        ->
value('phone')
        ->
css('td', 'color', 'red')
        ->
css('td', 'width', '20%')
    ->
add()
    ->
column()
        ->
value(function ($row) {
            return
'<a href="country/'. $row['id'] .'">edit</a>';
        })
        ->
css('td', 'width', '10%')
    ->
add();
?>


<html>
    <head>
        <!-- JQUERY -->
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

        <!-- DATATABLES -->
        <link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet">
        <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

        <!-- Bootstrap and Datatables Bootstrap theme (OPTIONAL) -->
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

        <script type="text/javascript">

            $(document).ready(function(){
                $('#demoTable').DataTable();
            });

        </script>
    </head>
    <body>
        <div style="width: 50%; margin: 30px;">
            <?php $table->render(); ?>
</div>
    </body>
</html>


Details

Build Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License

jupitern/table

HTML table generation with PHP.

Pass your data using: * JSON, Arrays (associative or not). * result set using PDO or you favourite framework ORM. * directly or using ajax requests. * Integrates easily with your preferred js library. * more to come...

Demo:

soon...

Requirements

PHP 5.6 or higher.

Installation

Include jupitern/table in your project, by adding it to your composer.json file.

{
    "require": {
        "jupitern/table": "1.*"
    }
}

Usage

// instance Table with instance name
\Jupitern\Table\Table::instance()

// set data for non ajax requests
// using a array
->setData([ [1, 'Peter', '35', '961 168 851'], [2, 'John', '44', '169 853 741'] ])
// using a associative array
->setData([
	['id' => 1, 'name' => 'Peter', 'age' => '35', 'phone' => '961 168 851'],
	['id' => 2, 'name' => 'John', 'age' => '44', 'phone' => '169 853 741'],
])
// using json string
->setData([[1,"Peter","35","961 168 851"],[2,"John","44","169 853 741"]])
// using PDO result or your framework ORM. see example how to grab $data at the end
->setData($data)

// add attributes to the <table> html tag one by one
->attr('table', 'id', 'demoTable')
->attr('table', 'class', 'table table-bordered table-striped table-hover')
->attr('table', 'cellspacing', '0')

// or add all <table> attributes at once
->attrs('table', ['class' => 'table table-bordered', 'cellspacing' => '0'])

// add attributes to the table rows
->css('tr', 'background-color', 'red')

// add attributes to the table rows using a callable
->attr('tr', 'data-id', function($row) {
    return 'row-' . $row['id'];
})

// add a new column for array data
->column()
	->title('Name')
	->value(1)
->add()

// add a new column for (associtive array, PDO or ORM) data
->column()
	->title('Age')
	->value('age')
->add()

// add a column with a closure for value field to process data in execution
// this example assumes data as object
->column()
	->title('Name')
	->value(function ($row) {
		return rand(1,10)%2 ? '<b>'.$row->name.'</b>' : $row->name;
	})
->add()

// onether closure example for adding a column with edit action with no title on <th>
// this example assumes data associative array
->column()
	->value(function ($row) {
		return '<a href="edit/'.$row['id'].'">edit '.$row['name'].'</a>';
	})
->add()

// add a column with text field as filter
->column()
	->title('Name')
	->value('name')
	->filter()
->add()

// add a column with a drop down field as filter
// $filterData as array
->column()
	->title('Name')
	->value('name')
	->filter([[1, 'Peter'], [2, 'John']])
->add()

// add a column with a drop down field as filter
// $filterData from (associtive array, PDO or ORM). see example how to grab $data at the end
->column()
	->title('Name')
	->value('name')
	->filter($filterData)
->add()

// add a column with some attributes and css for <th> and <td>
->column()
	->title('Name')
	->value('name')
	->attr('th', 'data-val', 'foo')		        // add attributes to <th>
    ->css('th', 'background-color', '#f5f5f5')	// add css to <th>
    ->attr('td', 'data-val', 'bar')				// add attributes to <td>
    ->css('td', 'background-color', '#f5f5f5')	// add css to <td>
->add()

// echo table output
->render();

// OR return table output
->render(true);

Example using PDO and datatables

// grab data from db with PDO or in alternative from your framework ORM
$db = new PDO('mysql:host=HOST_NAME;dbname=DB_NAME;charset=utf8', 'DB_USERNAME', 'DB_PASSWORD',
		array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)
);
// data to populate table
$data = $db->query("SELECT id, name, age, phone FROM persons")->fetchAll(PDO::FETCH_OBJ);
// used for column filter
$filterData = $db->query("SELECT name as val, name FROM persons limit 10")->fetchAll(PDO::FETCH_OBJ);

\Jupitern\Table\Table::instance()
	->setData($data)
	->attr('table', 'id', 'demoTable')
	->attr('table', 'class', 'table table-bordered table-striped table-hover')
	->attr('table', 'cellspacing', '0')
	->attr('table', 'width', '100%')
	->column()
		->title('Name')
		->value(function ($row) {
			return rand(1,10)%2 ? '<b>'.$row->name.'</b>' : $row->name;
		})
		->filter($filterData)
		->css('td', 'color', 'green')
		->css('td', 'width', '50%')
		->css('td', 'background-color', '#ccc', true)
	->add()
	->column()
		->title('Age')
		->value('age')
		->filter()
		->css('td', 'color', 'red')
		->css('td', 'width', '20%')
	->add()
	->column('Phone')
		->filter()
		->value('phone')
		->css('td', 'color', 'red')
		->css('td', 'width', '20%')
	->add()
	->column()
		->value(function ($row) {
			return '<a href="country/'.$row->id.'">edit</a>';
		})
		->css('td', 'width', '10%')
	->add()
	->render();
?>

Include Jquery, Datatables and Bootstrap (optional) in your html.

<!-- JQUERY -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>

<!-- DATATABLES -->
<link href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css" rel="stylesheet">
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>

<!-- Bootstrap and Datatables Bootstrap theme (OPTIONAL) -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript">

	$(document).ready(function(){
		$('#demoTable').DataTable();
	});

</script>

Roadmap

- [ ] add demo and more examples - [ ] code some tests

Contributing

- welcome to discuss a bugs, features and ideas.

License

jupitern/table is release under the MIT license.

You are free to use, modify and distribute this software, as long as the copyright header is left intact


  Files folder image Files  
File Role Description
Files folder imagesrc (2 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExamples (2 files)
Files folder imageTable (3 files)

  Files folder image Files  /  src  /  Examples  
File Role Description
  Accessible without login Plain text file test1.php Example Example script
  Accessible without login Plain text file test2.php Example Example script

  Files folder image Files  /  src  /  Table  
File Role Description
  Plain text file Properties.php Class Class source
  Plain text file Table.php Class Class source
  Plain text file TableColumn.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:126
This week:1
All time:9,407
This week:560Up