cvs: peardoc /en/package/console/console-table examples.xml /en/package/console/console-table/examples filter.php filter.xml

From: Date: Thu, 13 Nov 2008 08:37:34 +0000
Subject: cvs: peardoc /en/package/console/console-table examples.xml /en/package/console/console-table/examples filter.php filter.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-11268@lists.php.net to get a copy of this message
cweiske Thu Nov 13 08:37:34 2008 UTC Added files: /peardoc/en/package/console/console-table/examples filter.php filter.xml Modified files: /peardoc/en/package/console/console-table examples.xml Log: Add another example http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-table/examples.xml?r1=1.1&r2=1.2&diff_format=u Index: peardoc/en/package/console/console-table/examples.xml diff -u peardoc/en/package/console/console-table/examples.xml:1.1 peardoc/en/package/console/console-table/examples.xml:1.2 --- peardoc/en/package/console/console-table/examples.xml:1.1 Wed Nov 12 21:04:32 2008 +++ peardoc/en/package/console/console-table/examples.xml Thu Nov 13 08:37:34 2008 @@ -15,5 +15,6 @@ </para> &package.console.console-table.examples.simple; + &package.console.console-table.examples.filter; </chapter> \ No newline at end of file http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-table/examples/filter.php?view=markup&rev=1.1 Index: peardoc/en/package/console/console-table/examples/filter.php +++ peardoc/en/package/console/console-table/examples/filter.php <?php require_once 'Console/Table.php'; require_once 'Console/Color.php'; //those could come e.g. from database $data = array( array(2001, 128.23), array(2002, 256.42), array(2003, 10.21), array(2004, -25.79), array(2005, 0), array(2006, 982.12), ); //prepare table $tbl = new Console_Table(); $tbl->setHeaders( array('Year', 'Profit') ); $tbl->addData($data); //add filter callback to colorize our profit column values $callback = 'colorize'; $tbl->addFilter(1, $callback); //Values should be aligned right $tbl->setAlign(1, CONSOLE_TABLE_ALIGN_RIGHT); echo $tbl->getTable(); /** * Wraps Console color codes around $value, * depending if its larger or smaller 0. * * @param float $value Value (column 1) * * @return string Colorful value */ function colorize($value) { $str = number_format($value, 2, ',', ''); if ($value < 0) { return Console_Color::convert('%r' . $str . '%n'); } else if ($value == 0) { return $str; } else { return Console_Color::convert('%g' . $str . '%n'); } } ?> http://cvs.php.net/viewvc.cgi/peardoc/en/package/console/console-table/examples/filter.xml?view=markup&rev=1.1 Index: peardoc/en/package/console/console-table/examples/filter.xml +++ peardoc/en/package/console/console-table/examples/filter.xml <?xml version="1.0" encoding="utf-8"?> <section xmlns="http://docbook.org/ns/docbook" xmlns:phd="http://www.php.net/ns/phd" xml:id="package.console.console-table.examples.filter" > <info> <title>Using column filtering with Console_Color</title> <titleabbrev>Using addFilter</titleabbrev> </info> <para> In this example we use <phd:pearapi phd:package="Console_Table" phd:linkend="Console_Table::addFilter"/> to colorize our table cells according to their value. Colorization is very easy using PEAR's <link linkend="package.console.console-color">Console_Color</link> package. </para> <para> After filling our table object with headers and data, we add an output filter by specifying a callback function. The first parameter to <methodname>addFilter</methodname> is the column number that shall be formatted, beginning with 0. Due to backwards compatibility with PHP4, <methodname>addFilter</methodname> requires a variable as second parameter, even if you just want to specfiy a simple function name. </para> <para> </para> <para> We also have <classname>Console_Table</classname> align the "Profit" column right so that the commas are aligned equally. </para> <programlisting role="php"> <xi:include parse="text" xmlns:xi="http://www.w3.org/2001/XInclude" href="&package.console.console-table.examples.filter.php;" > <xi:fallback>FIXME:MISSING XINCLUDE CONTENT</xi:fallback> </xi:include> </programlisting> <para> The code above creates the following output, except that "Profit" values larger than 0 are colored in green, while the ones smaller zero are in red. </para> <screen><![CDATA[ +------+--------+ | Year | Profit | +------+--------+ | 2001 | 128,23 | | 2002 | 256,42 | | 2003 | 10,21 | | 2004 | -25,79 | | 2005 | 0,00 | | 2006 | 982,12 | +------+--------+ ]]></screen> </section>

« previous php.pear.doc (#11268) next »