메뉴 건너뛰기

프로그램언어

조회 수 1624 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

배열을 테이블로 만들 필요가 생겨 작성했습니다.
rowspan을 적용한 테이블로 만드는 소스 입니다.

만약 아래와 같은 배열이 있다면

$arr = [
 'a' => [
  'b' => 1,
  'c' => 1
 ],
];

아래와 같은 테이블로 만듭니다.

<table>
<tr>
<td rowspan="2">a</td><td>b</td><td>1</td>
</tr>
<tr>
<td>c</td><td>1</td>
</tr>
</table>


class Maketable
{
 
    protected $CI;
 
    public function __construct()
    {
        $this->CI =& get_instance();
    }
 
    public function make_table($data, $convert_fg = true)
    {
        $str = '';
        foreach ($data as $key => $value) {
            $this->make_tr($key, $value, $convert_fg, $str);
        }
        return $str;
    }
 
    public function cnt_depth($data, &$cnt = 0)
    {
        if (is_array($data)) {
            foreach ($data as $value) {
                $this->cnt_depth($value, $cnt);
            }
        } else {
            $cnt++;
        }
        return $cnt;
    }
 
    public function make_tr($key, $data, $convert_fg, &$str = '', &$first = false)
    {
        if (is_array($data)) {
            $head_tr = ($first === true) ? '' : '<TR>';
            $rowspan = $this->cnt_depth($data);
            $key_str = ($convert_fg === true) ? $this->CI->lang->line($key) : $key;
            $str .= $head_tr . "<TD rowspan=\"{$rowspan}\">{$key_str}</TD>";
            $first = true;
            foreach ($data as $key => $value) {
                $this->make_tr($key, $value, $convert_fg, $str, $first);
            }
            $str .= "</TR>";
        } else {
            $head_tr = ($first === true) ? '' : '<TR>';
            $first = false;
            $key_str = ($convert_fg === true) ? $this->CI->lang->line($key) : $key;
            $str .= $head_tr . "<TD>{$key_str}</TD><TD>{$data}</TD></TR>";
        }
        return $str;
    }
}


하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved