cvs: peardoc /peardoc2/en/package/html/html-table intro.xml
| From: | Alexander Merz | Date: | Sat, 02 Nov 2002 10:30:47 +0000 |
| Subject: | cvs: peardoc /peardoc2/en/package/html/html-table intro.xml | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-7261@lists.php.net to get a copy of this message | ||
alexmerz Sat Nov 2 05:30:47 2002 EDT
Added files:
/peardoc/peardoc2/en/package/html/html-table intro.xml
Log:
ups, forgot this file
Index: peardoc/peardoc2/en/package/html/html-table/intro.xml
+++ peardoc/peardoc2/en/package/html/html-table/intro.xml
<!-- $Revision: 1.1 $ -->
<refentry id="package.html.html-table.intro">
<refnamediv>
<refname>Introduction</refname>
<refpurpose>Creating an HTML-Table</refpurpose>
</refnamediv>
<refsect1 id="package.html.html-table.intro.whatis">
<title>What is HTML_Table?</title>
<para>
<classname>HTML_Table</classname> offers an interface for
create a HTML table. You can work with the table like a spreadsheet.
Instead of working with HTML code and linear adding of cells, you
can address and fill cells independend of there position. There is no
different, wether you start with fill a cell at the beginning, in the
middle or at the end of the table, a row or a column.
</para>
</refsect1>
<refsect1 id="package.html.html-table.intro.flags">
<title>The autoGrow and autoFill value</title>
<refsect2 id="package.html.html-table.intro.flags.autogrow">
<title>The <parameter>autoGrow</parameter> flag</title>
<para>
Normaly, you would define a table with a constant number
of rows and columns. But sometimes, you does not know, how many
rows or columns you need: ie. transforming user input or the
result of a database query to an HTML table.
</para>
<para>
In this case, you should
<link linkend="package.html.html-table.html-table.setautogrow">enable
</link> the <parameter>autoGrow</parameter> feature.
In this mode, <classname>HTML_Table</classname> adds new
rows or columns automatically, if you use a cell address located in a
not existing row or column.
</para>
</refsect2>
<refsect2 id="package.html.html-table.intro.flags.autofill">
<title>The <parameter>autoFill</parameter> value</title>
<para>
If you create a table of data, sometimes you have not to fill
all cells with different values. Perhaps you does not know the
value for a cell, or you want to insert a default value - ie. retrieving
data about users. Not every user has a mobile, a email address etc.,
in this case, a <parameter>"n/a"</parameter> should insert into to
specific cell.
</para>
<para>
So, simply <link linkend="package.html.html-table.html-table.setautofill">
define</link> <parameter>"n/a"</parameter> as
<parameter>autoFill</parameter> value and fill only
the cells where data exist. You need not to fill every cell; unfilled
cells contains automatically a <parameter>"n/a"</parameter>.
</para>
</refsect2>
</refsect1>
<refsect1 id="package.html.html-table.intro.create">
<title>Creating a table</title>
<refsect2 id="package.html.html-table.intro.create.data">
<title>The demonstation data</title>
<para>
Our HTML table to create should contain the following data:
<programlisting role="php">
<![CDATA[
$data = array(
'0' => array("Bakken", "Stig", "",
"stig@example.com"),
'1' => array("Merz", "Alexander", "alex.example.com",
"alex@example.com"),
'2' => array("Daniel", "Adam", "", "")
);
]]>
</programlisting>
</para>
</refsect2>
<refsect2 id="package.html.html-table.intro.create.start">
<title>Start</title>
<para>
Let us now start, create a new instance of
<classname>HTML_Table</classname>. The table
should be 600 pixel wide. We do not know
the quantity of the data to insert into the table -
so we enable the <parameter>autoGrow</parameter> feature.
Unfilled cells should contain an <parameter>"n/a"</parameter>.
<programlisting role="php">
<![CDATA[
require_once "HTML/Table.php";
$tableAttrs = array("width" => "600");
$table = new HTML_Table($tableAttrs);
$table -> setAutoGrow(true);
$table -> setAutoFill("n/a");
]]>
</programlisting>
</para>
</refsect2>
<refsect2 id="package.html.html-table.intro.create.adddata">
<title>Add data rows</title>
<para>
Now process every data entry. Here we use also the alternate feature
of <classname>HTML_Table</classname>. Every second row will be colored
red.
<programlisting role="php">
<![CDATA[
for($nr = 0; $nr < count($data); $nr++) {
$table -> setHeaderContents( $nr+1, 0, (string)$nr);
for($i = 0; $i < 4; $i++) {
if("" != $data[$nr][$i])
$table -> setCellContents( $nr+1, $i+1, $data[$nr][$i]);
}
}
$altRow = array("bgcolor"=>"red");
$table -> altRowAttributes(1, null, $altRow);
]]>
</programlisting>
</para>
</refsect2>
<refsect2 id="package.html.html-table.intro.create.addhead">
<title>Add header cells</title>
<para>
Now we want to define the cells in the first row and column
as header cells. It should looks like a spreadsheet
application, so we want to use the color "silver" as the
background colour for each header cell. The first row
contains a column headline, the first column the number
of the data set row.
<programlisting role="php">
<![CDATA[
$table -> setHeaderContents(0, 0, "");
$table -> setHeaderContents(0, 1, "Surname");
$table -> setHeaderContents(0, 2, "Name");
$table -> setHeaderContents(0, 3, "Website");
$table -> setHeaderContents(0, 1, "EMail");
$hrAttrs = array("bgcolor" => "silver");
$table -> setRowAttributes(0, $hrAttrs, true);
$table -> setColAttributes(0, $hrAttrs);
]]>
</programlisting>
</para>
</refsect2>
<refsect2 id="package.html.html-table.intro.create.html">
<title>Print the table</title>
<para>
It is done! Our table is finished, now we can put out the
table as HTML code.
<programlisting role="php">
<![CDATA[
echo $table->toHTML();
]]>
</programlisting>
The output will be look like this:
<programlisting>
<![CDATA[
<!-- BEGIN TABLE LEVEL: 0 -->
<table width="600">
<tr>
<th bgcolor="silver"> </th>
<th bgcolor="silver">Surname</th>
<th bgcolor="silver">Name</th>
<th bgcolor="silver">Website</th>
<th bgcolor="silver">EMail</th>
</tr>
<tr>
<th bgcolor="silver">0</th>
<td>Bakken</td>
<td>Stig</td>
<td>n/a</td>
<td>stig@example.com</td>
</tr>
<tr>
<th bgcolor="silver">1</th>
<td bgcolor="red">Merz</td>
<td bgcolor="red">Alexander</td>
<td bgcolor="red">alex.example.com</td>
<td bgcolor="red">alex@example.com</td>
</tr>
<tr>
<th bgcolor="silver">2</th>
<td>Daniel</td>
<td>Adam</td>
<td>n/a</td>
<td>n/a</td>
</tr>
</table>
<!-- END TABLE LEVEL: 0 -->
]]>
</programlisting>
</para>
</refsect2>
</refsect1>
</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->