mirror of
https://github.com/EGroupware/egroupware.git
synced 2024-12-05 14:20:40 +01:00
44 lines
14 KiB
HTML
Executable File
44 lines
14 KiB
HTML
Executable File
<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">
|
|
<?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> </font><font class="cmt">//Read file lines<br> </font>$lines<font class="kw">=</font>file<font class="kw">(</font>$file<font class="kw">);<br> </font>$data<font class="kw">=array();<br> foreach(</font>$lines <font class="kw">as </font>$line<font class="kw">)<br> </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> 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> </font><font class="cmt">//Header<br> </font><font class="kw">foreach(</font>$header <font class="kw">as </font>$col<font class="kw">)<br> </font>$<font class="kw">this-></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> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> </font><font class="cmt">//Data<br> </font><font class="kw">foreach(</font>$data <font class="kw">as </font>$row<font class="kw">)<br> {<br> foreach(</font>$row <font class="kw">as </font>$col<font class="kw">)<br> </font>$<font class="kw">this-></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> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> }<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> </font><font class="cmt">//Column widths<br> </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> </font><font class="cmt">//Header<br> </font><font class="kw">for(</font>$i<font class="kw">=</font>0<font class="kw">;</font>$i<font class="kw"><</font>count<font class="kw">(</font>$header<font class="kw">);</font>$i<font class="kw">++)<br> </font>$<font class="kw">this-></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> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> </font><font class="cmt">//Data<br> </font><font class="kw">foreach(</font>$data <font class="kw">as </font>$row<font class="kw">)<br> {<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>0<font class="kw">],</font>6<font class="kw">,</font>$row<font class="kw">[</font>0<font class="kw">],</font><font class="str">'LR'</font><font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>1<font class="kw">],</font>6<font class="kw">,</font>$row<font class="kw">[</font>1<font class="kw">],</font><font class="str">'LR'</font><font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>2<font class="kw">],</font>6<font class="kw">,</font>number_format<font class="kw">(</font>$row<font class="kw">[</font>2<font class="kw">]),</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'R'</font><font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>3<font class="kw">],</font>6<font class="kw">,</font>number_format<font class="kw">(</font>$row<font class="kw">[</font>3<font class="kw">]),</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'R'</font><font class="kw">);<br> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> }<br> </font><font class="cmt">//Closure line<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>array_sum<font class="kw">(</font>$w<font class="kw">),</font>0<font class="kw">,</font><font class="str">''</font><font class="kw">,</font><font class="str">'T'</font><font class="kw">);<br>}<br><br></font><font class="cmt">//Colored table<br></font><font class="kw">function </font>FancyTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">)<br>{<br> </font><font class="cmt">//Colors, line width and bold font<br> </font>$<font class="kw">this-></font>SetFillColor<font class="kw">(</font>255<font class="kw">,</font>0<font class="kw">,</font>0<font class="kw">);<br> </font>$<font class="kw">this-></font>SetTextColor<font class="kw">(</font>255<font class="kw">);<br> </font>$<font class="kw">this-></font>SetDrawColor<font class="kw">(</font>128<font class="kw">,</font>0<font class="kw">,</font>0<font class="kw">);<br> </font>$<font class="kw">this-></font>SetLineWidth<font class="kw">(</font>.3<font class="kw">);<br> </font>$<font class="kw">this-></font>SetFont<font class="kw">(</font><font class="str">''</font><font class="kw">,</font><font class="str">'B'</font><font class="kw">);<br> </font><font class="cmt">//Header<br> </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> for(</font>$i<font class="kw">=</font>0<font class="kw">;</font>$i<font class="kw"><</font>count<font class="kw">(</font>$header<font class="kw">);</font>$i<font class="kw">++)<br> </font>$<font class="kw">this-></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">,</font>1<font class="kw">);<br> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> </font><font class="cmt">//Color and font restoration<br> </font>$<font class="kw">this-></font>SetFillColor<font class="kw">(</font>224<font class="kw">,</font>235<font class="kw">,</font>255<font class="kw">);<br> </font>$<font class="kw">this-></font>SetTextColor<font class="kw">(</font>0<font class="kw">);<br> </font>$<font class="kw">this-></font>SetFont<font class="kw">(</font><font class="str">''</font><font class="kw">);<br> </font><font class="cmt">//Data<br> </font>$fill<font class="kw">=</font>0<font class="kw">;<br> foreach(</font>$data <font class="kw">as </font>$row<font class="kw">)<br> {<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>0<font class="kw">],</font>6<font class="kw">,</font>$row<font class="kw">[</font>0<font class="kw">],</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'L'</font><font class="kw">,</font>$fill<font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>1<font class="kw">],</font>6<font class="kw">,</font>$row<font class="kw">[</font>1<font class="kw">],</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'L'</font><font class="kw">,</font>$fill<font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>2<font class="kw">],</font>6<font class="kw">,</font>number_format<font class="kw">(</font>$row<font class="kw">[</font>2<font class="kw">]),</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'R'</font><font class="kw">,</font>$fill<font class="kw">);<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>$w<font class="kw">[</font>3<font class="kw">],</font>6<font class="kw">,</font>number_format<font class="kw">(</font>$row<font class="kw">[</font>3<font class="kw">]),</font><font class="str">'LR'</font><font class="kw">,</font>0<font class="kw">,</font><font class="str">'R'</font><font class="kw">,</font>$fill<font class="kw">);<br> </font>$<font class="kw">this-></font>Ln<font class="kw">();<br> </font>$fill<font class="kw">=!</font>$fill<font class="kw">;<br> }<br> </font>$<font class="kw">this-></font>Cell<font class="kw">(</font>array_sum<font class="kw">(</font>$w<font class="kw">),</font>0<font class="kw">,</font><font class="str">''</font><font class="kw">,</font><font class="str">'T'</font><font class="kw">);<br>}<br>}<br><br></font>$pdf<font class="kw">=new </font>PDF<font class="kw">();<br></font><font class="cmt">//Column titles<br></font>$header<font class="kw">=array(</font><font class="str">'Country'</font><font class="kw">,</font><font class="str">'Capital'</font><font class="kw">,</font><font class="str">'Area (sq km)'</font><font class="kw">,</font><font class="str">'Pop. (thousands)'</font><font class="kw">);<br></font><font class="cmt">//Data loading<br></font>$data<font class="kw">=</font>$pdf<font class="kw">-></font>LoadData<font class="kw">(</font><font class="str">'countries.txt'</font><font class="kw">);<br></font>$pdf<font class="kw">-></font>SetFont<font class="kw">(</font><font class="str">'Arial'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font>14<font class="kw">);<br></font>$pdf<font class="kw">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></font>BasicTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">);<br></font>$pdf<font class="kw">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></font>ImprovedTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">);<br></font>$pdf<font class="kw">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></font>FancyTable<font class="kw">(</font>$header<font class="kw">,</font>$data<font class="kw">);<br></font>$pdf<font class="kw">-></font>Output<font class="kw">();<br></font>?>
|
|
</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>
|