忍者ブログ
[PR]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。



2024/04/25 21:23 |
PHP1-25:HTML_Table

HTML_Table 1.8.2 (stable)
http://pear.php.net/package/HTML_Table

HTMLでテーブルを作成するのはとても面倒です。
普通に一覧を作成するだけならいいのですが、たとえば列を追加したくなった場合など、おそろしい労力が必要です。
PHPならforeachで簡単ですが、colspanやrowspanを使用した表を作成したい場合はこれまた簡単にはできません。

そんなわけでセルや行や列を直接指定して内容を指定できるHTML_Tableを使ってみましょう。
なんといっても便利なのは、setContents(30,6,'中身')と直接場所を簡単に指定できるところです。
PHP側に直接タグを書かなくても、foreachで突っ込めばいいので簡単ですし、メンテナンス性も向上します。

そんなに便利ならもっと普及していいのではと思うのですが、このクラス、メソッドを小分けしすぎて逆に使いづらかったり、またcolspanとかの属性とかは結局直接指定しなければならなかったりするので、もう少しどうにかします。

table.class.php 

<?php
    //初期設定
    define('PEAR_DIR',$_SERVER['DOCUMENT_ROOT'].'/src/php/pear/');
    ini_set('include_path',PEAR_DIR.PATH_SEPARATOR.ini_get('include_path'));
    require_once(PEAR_DIR.'HTML/Table.php');
   
    class pearHtmlTableModel {
        //メンバ変数
        private $table ='';
        private $attribute =array();
       
        //コンストラクタ
        public function __construct($border=0,$class_table='table'){
            $this->table=new HTML_Table(array('border'=>$border,'class'=>$class_table));
            $this->table->setAutoGrow(TRUE);
        }
       
        //テーブル全体の指定
        public function setTableContents($caption){
            if($caption!==""){$this->table->setCaption($caption);}
        }
       
        //見出し
        public function setTableHeaders($type,$header_array){
            if($type=='col'){
                foreach($header_array as $key=>$val){
                    $this->table->setCellContents(0,$key,$val);
                }
                $this->table->setRowType(0,'th');
            }else if($type=='row'){
                foreach($header_array as $key=>$val){
                    $this->table->setCellContents($key,0,$val);
                }
                $this->table->setColType(0,'th');
            }
        }
       
        //セルの内容と属性セット
        public function setCellContents($row,$col,$contents='',$class_td='',$rowspan='',$colspan=''){
            if(is_null($col) || is_null($row)){return false;}
            if($contents){
                $this->table->setCellContents($row,$col,$contents);
            }
            $attribute_array=array();
            if($class_td){$attribute_array['class']=$class_td;}
            if($rowspan){$attribute_array['rowspan']=$rowspan;}
            if($colspan){$attribute_array['colspan']=$colspan;}
            if($attribute_array){$this->table->setCellAttributes($row,$col,$attribute_array);}
        }
       
        //列の内容と属性セット
        public function setColContents($col,$contents='',$class_td='',$rowspan='',$colspan=''){
            if(is_null($col)){return false;}
            if($contents){
                for($i=0;$i<$this->table->getRowCount();$i++){
                    $this->table->setCellContents($i,$col,$contents);
                }
            }
            $attribute_array=array();
            if($class_td){$attribute_array['class']=$class_td;}
            if($rowspan){$attribute_array['rowspan']=$rowspan;}
            if($colspan){$attribute_array['colspan']=$colspan;}
            if($attribute_array){$this->table->updateColAttributes($col,$attribute_array);}
        }
       
        //行の内容と属性セット
        public function setRowContents($row,$contents='',$class_td='',$rowspan='',$colspan=''){
            if(is_null($row)){return false;}
            if($contents){
                for($i=0;$i<$this->table->getColCount();$i++){
                    $this->table->setCellContents($row,$i,$contents);
                }
            }
            $attribute_array=array();
            if($class_td){$attribute_array['class']=$class_td;}
            if($rowspan){$attribute_array['rowspan']=$rowspan;}
            if($colspan){$attribute_array['colspan']=$colspan;}
            if($attribute_array){$this->table->updateRowAttributes($row,$attribute_array);}
        }
       
        //穴埋め
        public function setAutoFill($fill){
            $this->table->setAutoFill($fill);
        }
       
        //行毎に属性セット
        public function altRowAttributes($attribute1,$attribute2){
            $this->table->altRowAttributes(0,$attribute1,$attribute2);
        }
       
        //表示
        public function toHtml(){
            return $this->table->toHtml();
        }
       
    #↓クラスのおわり
    }


例によってエラー処理(ry
今度は逆に機能を纏め過ぎてて却って分かり難くなっているような気がしないでもないですが、そこらへんはスルーということで。
また、colspanとrowspanが重なった場合の処理に微妙なところがあるので、変則的なテーブルにはあまり使わない方がいいかもしれません。
まあとりあえずでは使ってみましょう。

table.php 

<?php
    #初期設定
    header("Content-type: text/html; charset=utf-8");
    require_once('./table.class.php');
    $table = new pearHtmlTableModel(1);
   
    $row=array('0','1','2','3','4','5','6','7','8','9');
    $table->setTableHeaders('row',$row);
   
    $table->setCellContents(8,8,'cell','idcell',2,2);
    $table->setColContents(3,'col','idcol',2,1);
    $table->setRowContents(3,'row','idrow',1,2);
   
    $table->altRowAttributes(array('bgcolor'=>'#ffa0a0'),array('bgcolor'=>'#a0ffa0'));
    $table->setAutoFill('__');
   
    $ret=$table->toHtml();
   
    print($ret);

出力結果は以下のようになります。

0 __ __ col __ __ __ __ __ __
1 __ __ __ __ __ __ __ __
2 __ __ col __ __ __ __ __ __
row row row row row
4 __ __ col __ __ __ __ __ __
5 __ __ __ __ __ __ __ __
6 __ __ col __ __ __ __ __ __
7 __ __ __ __ __ __ __ __
8 __ __ col __ __ __ __ cell
9 __ __ __ __ __ __

このようにさくさくとテーブルを作成することができます。
細かな調整は各自でやっていただくとして、データベースから取り出したデータをテーブルで表示したい場合などに威力を発揮するのではないかと思われます。

PR


2008/09/12 15:57 | Comments(0) | TrackBack() | PHP

トラックバック

トラックバックURL:

コメント

コメントを投稿する






Vodafone絵文字 i-mode絵文字 Ezweb絵文字 (絵文字)



<<ひとつ気付いたんだが | HOME | PHP1-24:Text_Highlighter>>
忍者ブログ[PR]