html table generator,GitHub - CodeDriller/maketable: HTML Table Generator for PHP PDO Database Queri...

HTML Table Generator for PHP PDO Database Queries

maketable() is a carefully crafted PHP function that allows a developer to easily create

professional-looking and standards-compliant (X)HTML tables from results of PDO database queries

while maintaning fine control over table's style and markup.

Licensed under the terms of the MIT license.

No more broken indentation, ugly text alignment and manually summing up numbers for "Total" value.

maketable() will make it all for you!

PDO is a modern interface used to access

various database systems (MySQL, SQLite, MS SQL Server, Oracle and 7 others) in PHP in

a unified manner. With PDO your app can work on a dozen of database systems with minimal effort.

Usage

With maketable() you can make a table out of PDO SQL query in just 2 lines of code:

include_once 'maketable.php';

echo maketable( $pdo_connection->query( 'SELECT `name`, `city`, `orders` FROM `clients`' ) );

maketable() will bring column headers for you automatically using information from database.

But if you want to set custom headers, you can do it with just another one line of code:

$column_headers = array("Client's name", 'City', 'Number of orders')

maketable() will nicely format values in the table automatically, aligning text to the left and

other data to the center.

But if you want to set custom alignment, you can do it with just another one line of code:

$column_align = array('left', 'left', 'center')

maketable() will make a perfect HTML indentation for you.

But if you want to set custom indents, you can do it with just another 2 lines of code:

$first_indent = "\t\t\t",

$indent = "\t"

maketable() can easily add predefined classes to column cells.

But if you would like to use semantic class names, you can do it with just another line of code:

$column_classes = array('name', 'city', 'number')

You can also set custom id, caption, styles and class for the table.

But the most interesting thing is that you can generate a complex footer for the table with...

yes, just another one line of code:

$footer_columns = array('Clients: COUNT()', '', 'Total: SUM()')

maketable() supports SUM(), AVG(), MIN(), MAX() and COUNT() functions in footer cells.

maketable() also formats boolean values nicely making it appear as checkmarks instead of ugly 0/1.

Complete example

Now look how all this goes together with PDO code.

include_once 'maketable.php';

$sql = 'SELECT `name`, `address`, `orders` FROM `clients`';

try {

$conn = new PDO( 'mysql:host=MY_HOST;dbname=MY_DATABASE', 'MY_USERNAME', 'MY_PASSWORD' );

$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

echo maketable(

$executed_pdo_statement = $conn->query( $sql ),

$column_headers = array("Client's name", 'City', 'Number of orders'),

$column_align = array('left', 'left', 'center'),

$first_indent = "\t\t\t",

$indent = "\t",

$id = 'clients-table',

$class = 'strict-blue',

$style = 'padding: 0px; margin: 0px;',

$caption = 'Clients',

$column_classes = array('name', 'city', 'number'),

$even_row_class = 'tinted',

$footer_columns = array('Clients: COUNT()', '', 'Total: SUM()')

);

} catch( PDOException $e ) {

echo '

DATABASE ERROR:
' . $e->getMessage() . '
SQL query: ' . $sql . '

';

}

$conn = null;

Parameter names are given here for reference only, they can certainly be omitted.

Detailed parameters description can be found on our

wiki.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值