一般我們在寫網頁,表格的數量都是相等而且可控制的,但偶爾會遇到每一行的<td></td>數量是不固定的情況,例如在製作報表的時候,如果沒有將不足的行補滿,表格將會呈現出殘缺的狀況,非常難看。

這在ASP.NET有內建的類別可以處理這樣的問題,而PHP卻沒有內建的方法,必須自己去拼湊,當然每次硬寫程式碼解決也行,但維護起來就會非常痛苦。直接實做ASP.NET的類別是解決這個問題的根本之道。

<?PHP
class HtmlTag {
    var $TagName;
    var $Attr;
    function HtmlTag($TagName) {
        $this->TagName=$TagName;
    }
    function AddAttr($Name,$Value) {
        $this->Attr[$Name]=$Value;
    }
    function BegTag() {
        $html="<".$this->TagName;
        if (is_array($this->Attr)) {
            foreach(array_keys($this->Attr) as $Name) {
                $html.=" $Name='".$this->Attr[$Name]."'";
            }
        }
        $html.=">\n";
        return $html;
    }
    function EndTag() {
        $html="</".$this->TagName.">\n";
        return $html;
    }
}

class HtmlTable {
    var $ID;
    var $CssClass;
    var $Rows;
    var $Tag;
    function HtmlTable() {
        $this->Tag=new HtmlTag("table");
    }
    function AddRow() {
        $newrow=new HtmlRow();
        $this->Rows[]=$newrow;
        return $newrow;
    }
    function resupplyCell() {
        $maxcell=0;
        if (is_array($this->Rows)) {
            foreach($this->Rows as $row) {
                if ($maxcell<count($row->Cells)) {
                    $maxcell=count($row->Cells);
                }
            }
            foreach($this->Rows as $row) {
                while(count($row->Cells)<$maxcell) {
                    $row->AddCell("","td");
                }
            }
        }
    }
    function begTag() {
        if ($this->ID) $this->Tag->AddAttr("id",$ID);
        if ($this->CssClass) $this->Tag->AddAttr("class",$this->CssClass);
        return $this->Tag->BegTag();
    }
    function endTag() {
        return $this->Tag->EndTag();
    }
    function Html() {
        $Html=$this->begTag();
        if (is_array($this->Rows)) {
            foreach($this->Rows as $row) {
                $Html.=$row->begTag();
                foreach($row->Cells as $cell) {
                    $Html.=$cell->Html();
                }
                $Html.=$row->endTag();
            }
        }
        $Html.=$this->endTag();
        return $Html;
    }
}
class HtmlRow {
    var $ID;
    var $CssClass;
    var $Cells;
    var $Tag;
    function HtmlRow() {
        $this->Tag=new HtmlTag("tr");
    }
    function AddCell($InnerHtml,$Type) {
        $newcell=new HtmlCell($InnerHtml,$Type);
        $this->Cells[]=$newcell;
        return $newcell;
    }
    function begTag() {
        if ($this->ID) $this->Tag->AddAttr("id",$ID);
        if ($this->CssClass) $this->Tag->AddAttr("class",$this->CssClass);
        return $this->Tag->BegTag();
    }
    function endTag() {
        return $this->Tag->EndTag();
    }
}
class HtmlCell {
    var $ID;
    var $CssClass;
    var $InnerHtml;
    var $Tag;
    function HtmlCell($InnerHtml,$Type) {
        $this->InnerHtml=$InnerHtml;
        $this->Tag=new HtmlTag($Type);
    }
    function begTag() {
        if ($this->ID) $this->Tag->AddAttr("id",$ID);
        if ($this->CssClass) $this->Tag->AddAttr("class",$this->CssClass);
        return $this->Tag->BegTag();
    }
    function endTag() {
        return $this->Tag->EndTag();
    }
    function Html() {
        return $this->begTag().$this->InnerHtml.$this->endTag();
    }
}
?>

程式碼看起來很長,其實都是差不多的,主要就是用類別的方式讓它自行組裝html tag,其中HtmlCell在建立時,你必須告訴它你要做的是td還是th,當有所分別得時候使用css會比較好控制。那樣我們只需要去檢查每一行的數量就行了,而resupplyCell()就是在做補充表格的動作。

<?PHP
$sql="select RoomNo,CheckInTime,CheckOutTime,StayRest from Selling where ";
$sql.="HotelID=".QTR($HotelID)." and SellDate=".QTR($selldate);
$sql.=" order by RoomNo,StayRest DESC,SortValue";

$result=mysql_query($sql);
$lastRoomNo="";
$table=new HtmlTable();
$newrow="";
while ($row=mysql_fetch_row($result)) {
    list($RoomNo,$CheckIn,$CheckOut,$StayRest)=$row;

    if ($lastRoomNo!=$RoomNo) {
        $newrow=$table->AddRow();
        $sql="select CheckInTime,CheckOutTime,StayRest from Selling ";
        $sql.=" where HotelID=".QTR($HotelID);
        $sql.=" and SellDate=".QTR($lastdate);
        $sql.=" and RoomNo=".QTR($RoomNo);
        $sql.=" order by SortValue DESC Limit 0,1;";
        $res1=mysql_query($sql);
        if($row1=mysql_fetch_row($res1)) {
            list($CheckIn1,$CheckOut1,$StayRest1) = $row1;
            $newcell=$newrow->AddCell(CellHtml($CheckIn1,$CheckOut1),"td");
            if ($StayRest1=="S") $newcell->CssClass="stay";
        } else {
            $newcell=$newrow->AddCell("&nbsp;","td");
        }
        $newcell=$newrow->AddCell($RoomNo,"th");
        $lastRoomNo=$RoomNo;
    }
    $newcell=$newrow->AddCell(CellHtml($CheckIn,$CheckOut),"td");
    if ($StayRest=="S") $newcell->CssClass="stay";
}
$table->resupplyCell();
echo "\n".$table->Html();
?>

這是個幫旅館寫的小程式,用來輸出每個房間休息與住宿狀況,由於每個房間入住的數量並不一定,所以使用這種方法將有資料的<td>先全部填上去,然後再呼叫resupplyCell()方法,將不足的格子補上去,這樣輸出的表格就是很完整的,不會發生殘缺的現象了。

創作者介紹

人生四十宅開始 二號宅

漠哥 發表在 痞客邦 留言(0) 人氣()