reasonableframework/helper/tablewiz.php

78 lines
1.8 KiB
PHP
Raw Normal View History

2018-02-26 04:47:04 +00:00
<?php
/**
* @file tablewiz.php
* @date 2018-02-26
* @author Go Namhyeon <gnh1201@gmail.com>
* @brief TableWiz helper
*/
2018-02-26 05:02:45 +00:00
if(!function_exists("tablewiz_cut_str")) {
function tablewiz_cut_str($str, $strlimit=0) {
if($strlimit > 0) {
2018-02-26 05:10:29 +00:00
// do not use html tag when use cut_str
$str = substr(strip_tags($str), 0, $strlimit);
2018-02-26 05:02:45 +00:00
}
return $str;
}
}
2018-02-26 04:47:04 +00:00
if(!function_exists("tablewiz_create")) {
2018-02-26 05:05:10 +00:00
function tablewiz_create($rows, $bind=array(), $domid="", $domclass="", $strlimit=0, $thead_html=array(), $tbody_html_list=array()) {
2018-02-26 04:47:04 +00:00
$html = "";
if(count($rows) == 0) {
return $html;
}
$dom_element_name = make_random_id(10);
$domid = empty($domid) ? "tablewiz_id_" . $dom_element_name : $domid;
$domclass = empty($domclass) ? "tablewiz_class_" . $dom_element_name : $domclass;
$html_th_elms = "";
foreach($rows[0] as $k=>$v) {
$html_th_text = array_key_empty($k, $bind) ? $k : $bind[$k];
2018-02-26 05:02:45 +00:00
$html_th_elms .= "<th>" . tablewiz_cut_str($html_th_text, $strlimit) . "</th>";
2018-02-26 04:47:04 +00:00
}
2018-02-26 04:55:02 +00:00
// append contents in thead
foreach($thead_html as $k=>$v) {
2018-02-26 05:02:45 +00:00
$html_th_elms .= "<th>" . tablewiz_cut_str($v, $strlimit) . "</th>";
2018-02-26 04:55:02 +00:00
}
2018-02-26 04:47:04 +00:00
$html_tr_elms = "";
foreach($rows as $idx=>$record) {
$html_tr_elms .= "<tr>";
foreach($record as $k=>$v) {
2018-02-26 05:02:45 +00:00
$html_tr_elms .= "<td>" . tablewiz_cut_str($v, $strlimit) . "</td>";
2018-02-26 04:47:04 +00:00
}
$html_tr_elms .= "</tr>";
2018-02-26 04:55:02 +00:00
2018-02-26 04:47:04 +00:00
// append contents in tbody
2018-02-26 04:55:02 +00:00
if(count($tbody_html_list) > $idx) {
$tbody_html = $tbody_html_list[$idx];
if(is_array($tbody_html)) {
foreach($tbody_html as $k=>$v) {
2018-02-26 05:02:45 +00:00
$html_tr_elms .= "<td>" . tablewiz_cut_str($v, $strlimit) . "</td>";
2018-02-26 04:55:02 +00:00
}
}
2018-02-26 04:47:04 +00:00
}
}
$html .= <<<EOF
<table id="$domid" class="$domclass" border="1" cellspacing="0">
<thead>
<tr>
$html_th_elms
</tr>
</thead>
<tbody>
$html_tr_elms
</tbody>
</table>
EOF;
return $html;
}
}