tuto5.htm
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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>
<h1>Tables</h1>
This tutorial shows different ways to make tables.
<div class="source">
<pre><code><?php
<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
class </span>PDF <span class="kw">extends </span>FPDF
<span class="kw">{
</span><span class="cmt">// Load data
</span><span class="kw">function </span>LoadData<span class="kw">(</span>$file<span class="kw">)
{
</span><span class="cmt">// Read file lines
</span>$lines <span class="kw">= </span>file<span class="kw">(</span>$file<span class="kw">);
</span>$data <span class="kw">= array();
foreach(</span>$lines <span class="kw">as </span>$line<span class="kw">)
</span>$data<span class="kw">[] = </span>explode<span class="kw">(</span><span class="str">';'</span><span class="kw">,</span>trim<span class="kw">(</span>$line<span class="kw">));
return </span>$data<span class="kw">;
}
</span><span class="cmt">// Simple table
</span><span class="kw">function </span>BasicTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Header
</span><span class="kw">foreach(</span>$header <span class="kw">as </span>$col<span class="kw">)
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>40<span class="kw">,</span>7<span class="kw">,</span>$col<span class="kw">,</span>1<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Data
</span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
foreach(</span>$row <span class="kw">as </span>$col<span class="kw">)
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>40<span class="kw">,</span>6<span class="kw">,</span>$col<span class="kw">,</span>1<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
}
}
</span><span class="cmt">// Better table
</span><span class="kw">function </span>ImprovedTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Column widths
</span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>40<span class="kw">, </span>45<span class="kw">);
</span><span class="cmt">// Header
</span><span class="kw">for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw"><</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>$i<span class="kw">],</span>7<span class="kw">,</span>$header<span class="kw">[</span>$i<span class="kw">],</span>1<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Data
</span><span class="kw">foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>0<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>0<span class="kw">],</span><span class="str">'LR'</span><span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>1<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>1<span class="kw">],</span><span class="str">'LR'</span><span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>2<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>2<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>3<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>3<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
}
</span><span class="cmt">// Closing line
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>array_sum<span class="kw">(</span>$w<span class="kw">),</span>0<span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'T'</span><span class="kw">);
}
</span><span class="cmt">// Colored table
</span><span class="kw">function </span>FancyTable<span class="kw">(</span>$header<span class="kw">, </span>$data<span class="kw">)
{
</span><span class="cmt">// Colors, line width and bold font
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>255<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>255<span class="kw">);
</span>$<span class="kw">this-></span>SetDrawColor<span class="kw">(</span>128<span class="kw">,</span>0<span class="kw">,</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetLineWidth<span class="kw">(</span>.3<span class="kw">);
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">);
</span><span class="cmt">// Header
</span>$w <span class="kw">= array(</span>40<span class="kw">, </span>35<span class="kw">, </span>40<span class="kw">, </span>45<span class="kw">);
for(</span>$i<span class="kw">=</span>0<span class="kw">;</span>$i<span class="kw"><</span>count<span class="kw">(</span>$header<span class="kw">);</span>$i<span class="kw">++)
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>$i<span class="kw">],</span>7<span class="kw">,</span>$header<span class="kw">[</span>$i<span class="kw">],</span>1<span class="kw">,</span>0<span class="kw">,</span><span class="str">'C'</span><span class="kw">,</span>true<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span><span class="cmt">// Color and font restoration
</span>$<span class="kw">this-></span>SetFillColor<span class="kw">(</span>224<span class="kw">,</span>235<span class="kw">,</span>255<span class="kw">);
</span>$<span class="kw">this-></span>SetTextColor<span class="kw">(</span>0<span class="kw">);
</span>$<span class="kw">this-></span>SetFont<span class="kw">(</span><span class="str">''</span><span class="kw">);
</span><span class="cmt">// Data
</span>$fill <span class="kw">= </span>false<span class="kw">;
foreach(</span>$data <span class="kw">as </span>$row<span class="kw">)
{
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>0<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>0<span class="kw">],</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>$fill<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>1<span class="kw">],</span>6<span class="kw">,</span>$row<span class="kw">[</span>1<span class="kw">],</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'L'</span><span class="kw">,</span>$fill<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>2<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>2<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">,</span>$fill<span class="kw">);
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>$w<span class="kw">[</span>3<span class="kw">],</span>6<span class="kw">,</span>number_format<span class="kw">(</span>$row<span class="kw">[</span>3<span class="kw">]),</span><span class="str">'LR'</span><span class="kw">,</span>0<span class="kw">,</span><span class="str">'R'</span><span class="kw">,</span>$fill<span class="kw">);
</span>$<span class="kw">this-></span>Ln<span class="kw">();
</span>$fill <span class="kw">= !</span>$fill<span class="kw">;
}
</span><span class="cmt">// Closing line
</span>$<span class="kw">this-></span>Cell<span class="kw">(</span>array_sum<span class="kw">(</span>$w<span class="kw">),</span>0<span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'T'</span><span class="kw">);
}
}
</span>$pdf <span class="kw">= new </span>PDF<span class="kw">();
</span><span class="cmt">// Column headings
</span>$header <span class="kw">= array(</span><span class="str">'Country'</span><span class="kw">, </span><span class="str">'Capital'</span><span class="kw">, </span><span class="str">'Area (sq km)'</span><span class="kw">, </span><span class="str">'Pop. (thousands)'</span><span class="kw">);
</span><span class="cmt">// Data loading
</span>$data <span class="kw">= </span>$pdf<span class="kw">-></span>LoadData<span class="kw">(</span><span class="str">'countries.txt'</span><span class="kw">);
</span>$pdf<span class="kw">-></span>SetFont<span class="kw">(</span><span class="str">'Arial'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>14<span class="kw">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>BasicTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>ImprovedTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>AddPage<span class="kw">();
</span>$pdf<span class="kw">-></span>FancyTable<span class="kw">(</span>$header<span class="kw">,</span>$data<span class="kw">);
</span>$pdf<span class="kw">-></span>Output<span class="kw">();
</span>?></code></pre>
</div>
<p class='demo'><a href='tuto5.php' target='_blank' class='demo'>[Demo]</a></p>
A table being just a collection of cells, it's 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, headings are centered,
and numbers right aligned. Moreover, horizontal lines have been removed. This is done by means
of the <code>border</code> 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 (<code>L</code>) and right (<code>R</code>) 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 <code>LRB</code> for the <code>border</code>
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>