egroupware_official/phpgwapi/inc/fpdf/tutorial/tuto5.htm

44 lines
14 KiB
HTML
Raw Normal View History

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Tables</TITLE>
<LINK TYPE="text/css" REL="stylesheet" HREF="../fpdf.css">
</HEAD>
<BODY>
<H2>Tables</H2>
This tutorial shows how to make tables easily.
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
&lt;?php<br>define<font class="kw">(</font><font class="str">'FPDF_FONTPATH'</font><font class="kw">,</font><font class="str">'font/'</font><font class="kw">);<br>require(</font><font class="str">'fpdf.php'</font><font class="kw">);<br><br>class </font>PDF <font class="kw">extends </font>FPDF<br><font class="kw">{<br></font><font class="cmt">//Load data<br></font><font class="kw">function </font>LoadData<font class="kw">(</font>$file<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Read file lines<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$lines<font class="kw">=</font>file<font class="kw">(</font>$file<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$data<font class="kw">=array();<br>&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font>$lines <font class="kw">as </font>$line<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$data<font class="kw">[]=</font>explode<font class="kw">(</font><font class="str">';'</font><font class="kw">,</font>chop<font class="kw">(</font>$line<font class="kw">));<br>&nbsp;&nbsp;&nbsp;&nbsp;return </font>$data<font class="kw">;<br>}<br><br></font><font class="cmt">//Simple table<br></font><font class="kw">function </font>BasicTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Header<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">foreach(</font>$header <font class="kw">as </font>$col<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>40<font class="kw">,</font>7<font class="kw">,</font>$col<font class="kw">,</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">();<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Data<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">foreach(</font>$data <font class="kw">as </font>$row<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach(</font>$row <font class="kw">as </font>$col<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>40<font class="kw">,</font>6<font class="kw">,</font>$col<font class="kw">,</font>1<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}<br><br></font><font class="cmt">//Better table<br></font><font class="kw">function </font>ImprovedTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Column widths<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$w<font class="kw">=array(</font>40<font class="kw">,</font>35<font class="kw">,</font>40<font class="kw">,</font>45<font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Header<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">for(</font>$i<font class="kw">=</font>0<font class="kw">;</font>$i<font class="kw">&lt;</font>count<font class="kw">(</font>$header<font class="kw">);</font>$i<font class="kw">++)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>$w<font class="kw">[</font>$i<font class="kw">],</font>7<font class="kw">,</font>$header<font class="kw">[</font>$i<font class="kw">],</font>1<font class="kw">,</font>0<font class="kw">,</font><font class="str">'C'</font><font class="kw">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Ln<font class="kw">();<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="cmt">//Data<br>&nbsp;&nbsp;&nbsp;&nbsp;</font><font class="kw">foreach(</font>$data <font class="kw">as </font>$row<font class="kw">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font>$<font class="kw">this-&gt;</font>Cell<font class="kw">(</font>$w<font class="kw">[</font>0<font class="kw">],</font>6<font c
</font>
</code></NOBR></TD></TR></TABLE><P></P>
<SCRIPT>
<!--
if(document.location.href.indexOf('http:')==0)
{
document.write("<P CLASS='demo'><A HREF='tuto5.php' TARGET='_blank' CLASS='demo'>[Demo]</A></P>");
}
//-->
</SCRIPT>
A table being just a collection of cells, it is natural to build one from them. The first
example is achieved in the most basic way possible: simple framed cells, all of the same size
and left aligned. The result is rudimentary but very quick to obtain.
<BR>
<BR>
The second table brings some improvements: each column has its own width, titles are centered
and figures right aligned. Moreover, horizontal lines have been removed. This is done by means
of the <TT>border</TT> parameter of the <A HREF='../doc/cell.htm'>Cell()</A> method, which specifies which sides of the
cell must be drawn. Here we want the left (<TT>L</TT>) and right (<TT>R</TT>) ones. It remains
the problem of the horizontal line to finish the table. There are two possibilities: either
check for the last line in the loop, in which case we use <TT>LRB</TT> for the <TT>border</TT>
parameter; or, as done here, add the line once the loop is over.
<BR>
<BR>
The third table is similar to the second one but uses colors. Fill, text and line colors are
simply specified. Alternate coloring for rows is obtained by using alternatively transparent
and filled cells.
</BODY>
</HTML>