Location: PHPKode > scripts > Report generating and drawing > report-generating-and-drawing/as_reportool.ru.htm
<html>
<head><title>as_reportool: PHP êëàññ äëÿ ôîðìèðîâàíèÿ îò÷åòîâ</title>
<meta http-equiv="Content-Type" Content="text/html; charset=windows-1251">
<style type="text/css">
  body     { font-family:arial,helvetica;font-size:12px; }
  pre { color:#0000FF; border: 1px solid #e0e0e0; padding-left:8px; background: #f8f8ff;}
  td   { font-family:arial,helvetica;font-size:12px;}
  tr.odd   { background-color: #F0F0F8; color:#000000; }
  tr.even  { background-color: #F8F8FF; color:#000000; }
  h4       { background-color: #F8F8FF; color:#000000; text-align: left; }
  h5       { background-color: #F8F8FF; color:#000000; text-align: left; }
  hr       { color: #c0c0ff; border: none; height: 1px;}
</style>

</head>
<body>

<!-- DOC BEGIN -->
<h3>as_reportool: PHP êëàññ äëÿ ôîðìèðîâàíèÿ îò÷åòîâ</h3>

<p>as_reportool - ýòî î÷åðåäíîé âàðèàíò êëàññà, ïðåäíàçíà÷åííîãî äëÿ ôîðìèðîâàíèÿ îò÷åòîâ èç äàííûõ, âîçâðàùåííûõ
SQL-çàïðîñîì. Îñíîâíàÿ öåëü ïðè íàïèñàíèè êëàññà áûëà ñäåëàòü ìàêñèìàëüíî ïðîñòûì è ïðîçðà÷íûì ïðîöåññ
ïîäãîòîâêè îïèñàíèÿ îò÷åòà - â èòîãå îí ñîñòîèò â ïîñëåäîâàòåëüíîì âûçîâå íåñêîëüêèõ ìåòîäîâ
(ïåðåäà÷à SQL çàïðîñà, ñïèñêà âûâîäèìûõ è ãðóïïèðóþùèõ ïîëåé, äîïîëíèòåëüíûõ ïàðàìåòðîâ) ëèáî çàãðóçêà
âñåõ ïàðàìåòðîâ èç ïîäãîòîâëåííîãî XML ôàéëà ñ îïèñàíèåì îò÷åòà.

<br />
Ïðîãðàììà ðàñïðîñòðàíàÿåòñÿ ïî ëèöåíçèè <a href="http://www.gnu.org/copyleft/gpl.html" target="_blank">GNU GPL</a>,
ò.å. áåñïëàòíà äëÿ âñåõ âèäîâ èñïîëüçîâàíèÿ.

<p>Óñòàíîâêà ñêðèïòà ñòàíäàðòíàÿ - ñêîïèðóéòå ôàéë <b>as_reportool.php</b> è âñïîìîãàòåëüíûé <b>as_dbutils.php</b> â îäíó èç ïàïîê ñâîåãî ñàéòà
è èñïîëüçóéòå îïåðàòîðû "include(), require_once()" äëÿ ïîäêëþ÷åíèÿ êëàññà.
</p>

<h3>Èñïîëüçîâàíèå as_reportool</h3>
<p>
<pre>
require_once('as_reportool.php');
//...
$rep = new CReporTool();
</pre>

Áóäåì ïðèäåðæèâàòüñÿ "åñòåñòâåííîãî" ïîðÿäêà âûçîâà, îäíàêî îí íå ñòîëü âàæåí. Ãëàâíîå - âûçûâàòü ìåòîä DrawReport()
â ïîñëåäíþþ î÷åðåäü.

<br /><br />
<b>Øàã 1</b>: ïåðåäàåì â êëàññ òåêñò SQL çàïðîñà, êîòîðûé âåðíåò âûâîäèìûå äàííûå, îòñîðòèðîâàííûå  â íóæíîì ïîðÿäêå,
äëÿ ÷åãî âûçûâàåì ìåòîä SetQuery().

<br /><br />
<b>Øàã 2</b>:, ñ ïîìîùüþ ìåòîäà AddField() ïîñëåäîâàòåëüíî äîáàâëÿåì âñå "ïå÷àòàåìûå" ïîëÿ îò÷åòà.
Ó÷òèòå, ÷òî "èäåíòèôèêàòîðû" ïîëåé äîëæíû ñîâïàäàòü ñ "ïñåâäî-èìåíåì" ïîëåé â äàííûõ, âîçâðàùàåìûõ SQL çàïðîñîì.
<br />
Íàïðèìåð, åñëè íàø çàïðîñ ñîäåðæèò êîñíòðóêöèþ òèïà "SELECT ... (field1+field2) `field1_field2`",
äëÿ ïå÷àòè òàêîãî "âû÷èñëÿåìîãî" çíà÷åíèÿ èñïîëüçóåì èìÿ "field1_field2" :
<pre>$rep-&gt;AddField('field1_field2','ñóììà F1 è F2');</pre>

<br />
<b>Øàã 3</b>: Åñëè íàì íóæíû ïðîìåæóòî÷íûå èòîãè, ïåðåäàåì â êëàññ èìåíà "ãðóïïèðóþùèõ" ïîëåé
(åñòåñòâåííî, íàø SQL çàïðîñ äîëæåí â ýòîì ñëó÷àå ãðóïïèðîâàòü è ñîðòèðîâàòü çàïèñè â ñîîòâåòñòâèè ñ ýòèì íàáîðîì ïîëåé),
èñïîëüçóåì âûçîâ ìåòîäà AddGroupingField().

<br />Ãðóïïèðóþùèõ ïîëåé ìîæåò áûòü áîëåå ÷åì îäíî, ïðè íàëè÷èè äâóõ è áîëåå ãðóïïèðóþùèõ ïîëåé ïîëó÷àòñÿ ìíîãîóðîâíåâûå
ïîä-èòîãè. Ïåðâîå ïîëå, óêàçàííîå â ìåòîäå AddGroupingField(), áóäåò çàäàâàòü "âåðõíèé" óðîâåíü â ýòîé èåðàðõèè,
à âñå ïîñëåäóþùèå - óðîâíè íèæå ïðåäûäóùåãî.
<br />

Ïðè âûçîâå DrawReport, âî âðåìÿ öèêëà îáðàáîòêè äàííûõ (è ïå÷àòè ñòðîê îò÷åòà), êîãäà ìåíÿåòñÿ çíà÷åíèå îäíîãî
èç ãðóïïèðóþùèõ ïîëåé, âûâîäÿòñÿ âñå íàêîïëåííûå ïîä-èòîãè íà÷èíàÿ ñ ïîñëåäíåãî óðîâíÿ è äî äàííîãî ïîëÿ âêëþ÷èòåëüíî.

<br /><br />
<b>Øàã 4</b>: âûçûâàåì DrawReport() äëÿ âûâîäà îò÷åòà.

<br /><br />Ñôîðìèðîâàííûé îò÷åò ïðåäñòàâëÿåò HTML êîä, êîòîðûé àäåêâàòíî âûãëÿäèò íå òîëüêî íà ýêðàíå, íî è íà
ðàñïå÷àòêå. Îòâå÷àþò çà ýòî ïîäãîòîâëåííûå ñòèëè (CSS), êîòîðûå âûâîäÿòñÿ ïåðåä íà÷àëîì ñàìîãî îò÷åòà.

Âîçìîæíî, âàì çàõî÷åòñÿ çàäàòü ñâîè ñòèëè ôîðìàòèðîâàíèÿ âìåñòî ïðåäëîæåííûõ.  ýòîì ñëó÷àå âûâîä CSS - áëîêà ìîæíî îòìåíèòü,
âûçâàâ ìåòîä SuppressCss(). ×òîáû âûâîä áûë àäåêâàòíûì, â ýòîì ñëó÷àå ïðèäåòñÿ ñàìîñòîÿòåëüíî îïèñàòü css-ñòèëè ñ èäåíòèôèêàòîðàìè:
td.rep_ltrb, td.rep_lrb, td.rep_rb, td.num, td.cnt, td.newgroup.


<h3>CReporTool - ñïèñîê ìåòîäîâ</h3>

<li>
<b>CReporTool([$filename [, $outcharset]])</b> - êîíñòðóêòîð ìîæåò áûòü èñïîëüçîâàí íå òîëüêî äëÿ ñîçäàíèÿ ïóñòîãî îáúåêòà,
íî è äëÿ çàãðóçêè îïèñàíèÿ îò÷åòà èç ïîäãîòîâëåíîãî XML ôàéëà.

<br />Ïåðåäàéòå åãî èìÿ â ïåðâîì ïàðàìåòðå îïåðàòîðà <b>new CReporTool()</b>.
Åñëè ïàðàìåòð îïóùåí, ñîçäàåòñÿ ïóñòîé îáúåêò CReporTool.
<br />Âòîðîé ïàðàìåòð èñïîëüçóåòñÿ, åñëè âû çàãðóæàåòå îïèñàíèå èç XML ôàéëà, è âûõîäíàÿ êîäèðîâêà (charset) îòëè÷àåòñÿ îò UTF-8.
 ýòîì ñëó÷àå óêàæèòå èìÿ êîíå÷íîãî character set (íàïðèìåð, 'windows-1251'), è âñå ñèìâîëüíûå çíà÷åíèÿ (çàãîëîâêè äëÿ ïîëåé, ãðóïïèðîâîê, èòîãîâ)
 çàãðóæåííûå èç XML, áóäóò êîíâåðòèðîâàíû èç UTF-8.

(Ìîæåò, ÿ íå äîêîïàëñÿ äî âñåõ âîçìîæíîñòåé simpleXML, íî ïîêà ïîëó÷èëîñü, ÷òî simpleXML ôóíêöèÿ <b>simplexml_load_file</b>
íå ðàçáèðàåò ôàéëû â êîäèðîâêå îòëè÷íîé îò UTF-8 files, ïîõîæå, îíà âîîáùå èãíîðèðóåò àòðèáóò encoding â çàãîëîâêå XML.
À ïîòîìó ïðèõîäèòñÿ XML ôàéë âñåãäà ãîòîâèòü èìåííî â UTF-8.)
</li>

<li>
<b>LoadFromXml($filename [,$outcharset=''])</b> ìåòîä çàãðóæàåò îïèñàíèå îò÷åòà èç XML ôàéëà.
Ñîáñòâåííî, ýòîò ìåòîä è âûçûâàåòñÿ â êîíñòðóêòîðå, êîãäà ìû âûçûâàåì åãî ñ íåïóñòûì ïàðàìåòðîì.
Ïåðâûé ïàðàìåòð - ýòî èìÿ ôàéëà, âòîðîé - êîäèðîâêà, (ñì. îïèñàíèå êîíñòðóêòîðà âûøå).
Ñòðóêòóðà XML ôàéëà äîëæíà ñîîòâåòñòâîâàòü ïðèâåäåííîìó íèæå ïðèìåðó, èç êîòîðîãî ïðàêòè÷åñêè âñå ïîíÿòíî.
<br />ÂÍÈÌÀÍÈÅ: åñëè âû ïëàíèðóåòå èñïîëüçîâàòü ýòîò ìåòîä, óäîñòîâåðüòåñü â òîì, èñïîëüçóåòå íà õîñòèíãå PHP 5-îé âåðñèè ñ âêëþ÷åííîé
è ÷òî åñòü ïîääåðæêà simpleXML.

<br /><br />
Ïðèìåð XML ôàéëà
<pre>

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;report_def version="1.00"&gt;
  &lt;query&gt;SELECT c.categoryid, b.animalid, a.nickname,a.gender,a.birth, a.weight
   FROM big_zoo a, animals b, animal_categories c
   WHERE a.animalid=b.animalid AND b.category=c.categoryid ORDER BY c.categoryid, b.animalid
  &lt;/query&gt;
  &lt;headings&gt;&lt;![CDATA[
&lt;tr&gt;&lt;td colspan="8" class="rep_ltrb"&gt;This is additional headings to print before default columns headings&lt;/td&gt;&lt;/tr&gt;
]]&gt;&lt;/headings&gt;
  &lt;nodefaultheadings value="1" /&gt;
  &lt;summary title="Summary totals (%rowcount%)" /&gt;
  &lt;delimiters decimal="," thousand=" " /&gt;
  &lt;grpfield name="categoryid" fconv="GetAnymalCategoryName" title="Animal category " totaltitle="Totals for category" /&gt;
  &lt;grpfield name="animalid" fconv="GetAnymalClassName" title="Animal class" totaltitle="Totals for class" /&gt;

  &lt;field name="nickname" title="Nick" fconv="" format="" /&gt;
  &lt;field name="gender" title="Gender" fconv="DecodeGender" /&gt;
  &lt;field name="weight" title="Weight, kg" summable="1" format="i" /&gt;

&lt;/report_def&gt;

</pre>
</li>
<br /><b>query</b> - òåã çàäàåò SQL çàïðîñ, àíàëîãè÷íî âûçîâó SetQuery().
<br /><b>headings</b> - çàäàíèå äîïîëíèòåëüíûõ çàãîëîâêîâ (ñì. ìåòîä SetHeadings() );
<br /><b>nodefaultheadings</b> - ýòîò òåã ñ àòðèáóòîì value="1" "ïðÿ÷åò" àâòî-ôîðèðóåìûå çàãîëîâêè äëÿ êîëîíîê â øàïêå îò÷åòà (ñì. ñ íåïóñòûì âòîðûì ïàðàìåòðîì: SetHeadings($new_headings,1);
<br /><b>grpfield</b> - çàäàíèå ãðóïïèèðóþùåãî ïîëÿ (ñì. AddGroupingField() ),
<br /><b>field</b> - äîáàâëåíèå ïå÷àòàåìîé êîëîíêè ñ ïîëåì (âûçîâ AddField() );
<br /><b>summary</b> - âêëþ÷åíèå âûâîäà ôèíàëüíûõ èòîãîâ ïî îò÷åòó, ñ çàäàíèåì ñòðîêè òèòóëà (âûçîâ SetSummary() );
<br /><b>delimiters</b> - ïåðåêëþ÷åíèå ñèìâîëîâ ðàçäåëèòåëåé â âûâîäå ÷èñëîâûõ çíà÷åíèé (ñì. SetNumberDelimiters($decimal,$thousand) ).

<br />
<br />
<li>
<b>AddGroupingField($fldid,$fconv='',$title='',$totaltitle='')</b> äîáàâëÿåò ãðóïïèðóþùåå ïîëå,
ïðè ñìåíå çíà÷åíèÿ êîòîðîãî áóäóò ïå÷àòàòüñÿ ñòðîêè ñ ïîä-èòîãàìè.
<ul>
  <li><b>$fldid</b> - èìÿ ïîëÿ (èëè ïñåâäî-èìÿ äëÿ êîëîíêè äàííûõ,</li>
  <li><b>$fconv</b> èìÿ ôóíêöèè, èñïîëüçóåìîé äëÿ äëÿ ïðåîáðàçîâàíèÿ çíà÷åíèÿ ïîëÿ âî ÷òî-òî áîëåå èíôîðìàòèâíîå (äëÿ âûâîäà íà ïå÷àòü).
  Íàïðèìåð, åñëè çíà÷åíèå - ýòî ÷èñëîâîé ID ïîäðàçäåëåíèÿ, óêàçàííàÿ ôóíêöèÿ äîëæíà âîçâðàùàòü íàçâàíèå ïîäðàçäåëåíèÿ ïî åãî íîìåðó (ID).
  <li><b>$title</b> òèòóë, èëè íàèìåíîâàíèå äëÿ ïîëÿ, èç êîòîðîãî áóäåò ôîðìèðîâàòüñÿ áëîê çàãîëîâêîâ (øàïêà îò÷åòà)</li>
  <li><b>$totaltitle</b> òåêñò, âûâîäèìûé â ñòðîêå èòîãîâ ñëåâà îò èòîãîâûõ çíà÷åíèé. Ìàêðîñ "%name%" â ýòî éñòðîêå áóäåò çàìåíåí
  íà çíà÷åíèå ãðóïïèðóþùåãî ïîëÿ (ïîäðàçäåëåíèÿ, íàïðèìåð),
  ò.å. åñëè çàäàòü "Èòîãî äëÿ %name%", ñòðîêà èòîãîâ áóäåò âûãëÿäåòü êàê "Èòîãî äëÿ (íàçâàíèå çäåñü)"</li>
</ul>

Êàê óæå ãîâîðèëîñü, ìîæíî çàäàâàòü áîëåå îäíîãî ãðóïïèðóþùåãî ïîëÿ (ïîñëåäîâàòåëüíûì âûçîâîì ìåòîäà).
</li>

<br /><br />
<li>
<b>AddField($fldid,$fldtitle='',$summable=0,$fconv='', $format='')</b> äîáàâëÿåò â îò÷åò êîëîíêó ñ ïîëåì.
<br />Ïàðàìåòðû:
  <ul>
  <li>$fldid - èìÿ ïîëÿ (èëè ïñåâäî-èìÿ êàê ïðåäñòàâëåíî â SQL çàïðîñå)</li>
  <li>$fldtitle íàèìåíîâàíèå äëÿ ïîëÿ, (èñïîëüçóåòñÿ â çàãîëîâêàõ øàïêè îò÷åòà)</li>
  <li>$summable : åñëè true (èëè ëþáîå íåïóñòîå çíà÷åíèå), äëÿ äàííîãî ïîëÿ áóäóò ïîäñ÷èòûâàòüñÿ è âûâîäèòüñÿ
  ïîä-èòîãè</li>
  <li>$fconv - íåîáÿçàòåëüíîå èìÿ ôóíêöèè ïðåîáðàçîâàíèÿ çíà÷åíèÿ ïîëÿ â åãî "ïå÷àòíîå" ïðåäñòàâëåíèå.
  <li>$format - íåîáÿçàòåëüíûé èäåíòèôèêàòîð ôîðìàòà ïîëÿ. Ïîääåðæèâàþòñÿ ñëåäóþùèå ôîðìàòû:
    <table border=0 cellpadding="3" cellspacing="1" style="background:#B8B8FE;">
    <tr class="even"><td>format</td><td>×òî îçíà÷àåò</td></tr>
    <tr class="odd"><td>c</td><td>"Centered" - çíà÷åíèå öåíòðóåòñÿ â ÿ÷åéêå</td></tr>
    <tr class="even"><td>r</td><td>"Right" - çíà÷åíèå âûðàâíèâàåòñÿ âïðàâî</td></tr>
    <tr class="odd"><td>money</td><td>çíà÷åíèå ïðåîáðàçóåòñÿ ôóíêöèåé <b>number_format()</b> ê "äåíåæíîìó" âèäó (NNN NNN.00)
    è âûðàâíèâàåòñÿ âïðàâî</td></tr>
    <tr class="even"><td>i</td><td>"Integer" - çíà÷åíèå ôîðìàòèðóåòñÿ <b>number_format()</b>-îì êàê
    öåëî÷èñëåííîå (ñ ðàçäåëèòåëåì òûñÿ÷) è âûðàâíèâàåòñÿ âïðàâî</td></tr>
    </table>
  </li>
  </ul>
</li>

<br /><br />
<li><b>SetHeadings($par, $no_defaultheader=0)</b> : ïî óìîë÷àíèþ äëÿ îò÷åòà ôîðìèðóåòñÿ ïðîñòàÿ "øàïêà", ñîñòîÿùàÿ èç íàèìåíîâàíèé ïîëåé,
 ïåðåäàííûõ â ìåòîäå AddField() (ïàðàìåòð $fldtitle).
<br />×òîáû âûâåñòè ÷òî-òî áîëåå ñëîæíîå, ïîäãîòîâüòå ñâîé HTML êîä øàïêè è ïåðåäàéòå åãî â êëàññ ñ ïîìîùüþ ìåòîäà SetHeadings().
Êðîìå ñàìîãî êîäà (ïåðâûé ïàðàìåòð) ìîæíî òàêæå ïåðåäàòü íåïóñòîé (0, true) âòîðîé ïàðàìåòð, îòêëþ÷àþùèé ãåíåðàöèþ
ñòàíäàðòíîãî êîäà øàïêè.
HTML êîä äîëæåí ïðåäñòàâëÿòü íàáîð òàáëè÷íûõ ñòðîê, ò.å. íà÷èíàòüñÿ òåãîì &lt;tr&gt; è çàêàí÷èâàòüñÿ &lt;/tr&gt;.
Îí âûâîäèòñÿ ïåðåä ñòàíäàðòíûì (ñãåíåðèðîâàííûì) êîäîì çàãîëîâêîâ.
</li>

<br /><br />
<li><b>SetSummary($title)</b> : ìåòîä âûçûâàåòñÿ, åñëè â êîíöå îò÷åòà íóæíî ðàñïå÷àòàòü èòîãîâóþ ñòðîêó.
Ìîæíî ïåðåäàòü ñâîé òåêñò çàãîëîâêà â ïàðàìåòðå $title. ×òîáû â ýòîì òåêñòå âûâåñòè îáùåå ÷èñëî ñòðîê äàííûõ, ïîïàâøèõ â îò÷åò,
âêëþ÷èòå â ñòðîêó ìàêðîñ <b>%rowcount%</b>.
</li>

<br /><br />
<li><b>SetNumberDelimiters($dec, $tho)</b> çàìåíÿåò ñòàíäàðòíûå ñèìâîëû ðàçäåëèòåëåé äëÿ äðîáíîé ÷àñòè è òûñÿ÷,
èñïîëüçóåìûå ïðè âûçîâàõ number_format, êîãäà ôîðìàòèðóþòñÿ ÷èñëîâûå çíà÷åíèÿ ïîëåé (ïîëåé ñ òèïàìè ôîðìàòà i,money).
<br />Ñòàíäàðòíûå ñèìâîëû ðàçäåëèòåëåé - "," äëÿ òûñÿ÷ è "." for äðîáíîé ÷àñòè.
</li>


<br /><br />
<li><b>SetFontStyles($par)</b> óñòàíàâëèâàåò íîâûé ñòèëü äëÿ âñåãî âûâîäèìîãî îò÷åòà è äîëæåí ñîñòîÿòü èç css àòðèáóòîâ.
Íàïðèìåð, ÷òîáû âûâåñòè îò÷åò øðèôòîì "verdana" ñ ðàçìåðîì 3mm, ïåðåäàéòå ñòðîêó :

<pre>$rep-&gt;SetFontStyles("font-family: Verdana; font-size:3mm;");</pre>
</li>

<br /><br />
<li><b>DrawReport($title='')</b> ôèíàëüíûé ìåòîä, ãåíåðèðóþùèé è âûâîäÿùèé HTML êîä îò÷åòà.
Ïîñëåäîâàòåëüíî îòðàáàòûâàþòñÿ ñòðîêè äàííûõ, âîçâðàùàåìûå ïåðåäàííûì SQL çàïðîñîì, ïîäñ÷èòûâàþòñÿ ïðîìåæóòî÷íûå (è ôèíàëüíûå) èòîãè.
<br />Íåîáÿçàòåëüíûé ïàðàìåòð $title ïåðåäàåò òèòóëüíóþ ñòðîêó, âûâîäèìóþ â íà÷àëå îò÷åòà (ïåðåä "øàïêîé").
</li>

<h3>Ïðèìåð äàííûõ äëÿ äåìîíñòðàöèè ðàáîòû êëàññà.</h3>

Äëÿ äåìîíñòðàöèè íàì ïîíàäîáÿòñÿ òðè òàáëèöû â íàøåé ÁÄ.
Ýòî áóäåò îïèñàíèå ãèïîòåòè÷åñêîãî çîîïàðêà.
Ïåðâàÿ òàáëèöà áóäåò ñîäåðæàòü ñïèñîê "áàçîâûõ" êàòåãîðèé æèâîòíûõ (ìîðñêèå, ...)
Âòîðàÿ - ñïèñîê òèïîâ æèâîòíûõ (ñëîíû, ãîðèëëû è ò.ä.),
è òðåòüÿ - ñïèñîê îáèòàòåëåé çîîïàðêà.
<br />Äëÿ ãåíåðàöèè äàííûõ ïðèãîòîâëåí sql-ôàéë, êîòîðûé ìîæíî âûïîëíèòü â êîíñîëè mySql êîìàíäîé <b>source sampledata.sql</b>
<br /><br />

<table border=0 cellpadding="3" cellspacing="1" style="background:#B8B8FE;">
<tr class="even"><td>Ïîëå</td><td>òèï</td><td>îïèñàíèå</td></tr>

<tr class="odd"><td colspan=3><b>òàáëèöà animal_categories</b></td></tr>
<tr class="even"><td>categoryid</td><td>INT</td><td>unique category's ID</td></tr>
<tr class="odd"><td>categoryname</td><td>VARCHAR</td><td>category name</td></tr>

<tr class="even"><td colspan=3><b>òàáëèöà animals</b></td></tr>
<tr class="odd"><td>animalid</td><td>INT</td><td>unique animal type's ID</td></tr>
<tr class="even"><td>category</td><td>INT</td><td>category of the animal</td></tr>
<tr class="odd"><td>animalname</td><td>VARCHAR</td><td>animal's name</td></tr>

<tr class="even"><td colspan=3><b>òàáëèöà big_zoo - all animals living in the zoo</b></td></tr>
<tr class="odd"><td>itemid</td><td>INT</td><td>living creature's ID</td></tr>
<tr class="even"><td>animalid</td><td>INT</td><td>what kind of animal is it (ID)</td></tr>
<tr class="odd"><td>nickname</td><td>VARCHAR</td><td>nick name given to this creature</td></tr>
<tr class="even"><td>birth</td><td>DATE</td><td>birth date</td></tr>
<tr class="odd"><td>gender</td><td>CHAR(1)</td><td>gender(m for male, f for female)</td></tr>
<tr class="even"><td>weight</td><td>INT</td><td>creature's weight</td></tr>
</table>

<br /><br />
Áóäåì âûâîäèòü îò÷åò ïî âñåì îñîáÿì, æèâóùèì â çîîïàðêå, ñ ïðîìåæóòî÷íûìè èòîãàìè
è ñòðîêîé îáùèõ èòîãîâ.

<pre>
$rep = new CReporTool();

$rep-&gt;SetQuery("SELECT c.categoryid, b.animalid, a.nickname,a.gender,a.birth,a.weight
   FROM big_zoo a, animals b, animal_categories c
   WHERE a.animalid=b.animalid AND b.category=c.categoryid ORDER BY c.categoryid, b.animalid");

$rep-&gt;AddGroupingField('categoryid','GetAnymalCategoryName','Animal category ','Totals for category');

$rep-&gt;AddGroupingField('animalid','GetAnymalClassName','Animal class ','Totals for class');

$rep-&gt;AddField('nickname','Nick');

$rep-&gt;AddField('gender','Gender',0,'DecodeGender'); // DecodeGender() will show 'male' for 'm' and female for 'f' value.
$rep-&gt;AddField('birth','Birth date',0,'DateToChar'); // your function DateToChar converts DATE value to be more readable
$rep-&gt;AddField('weight','Weight, kg',1,'','i'); // this field is summable and will be printed right-aligned and number_format()ted
$rep-&gt;DrawReport('Report: All animals in zoo');
</pre>

<br />Âîçìîæíî, Âû ðåøèòå âîñïîëüçîâàòüñÿ âîçìîæíîñòÿìè áàçû äëÿ ôîðìèðîâàíèÿ ñòðîê ïîä-èòîãîâ (íàïðèìåð, èñïîëüçóÿ GROUP BY <b>WITH ROLLUP</b> â MySQL)
- òàê âîò, äëåàòü ýòî íå ñòîèò, ò.ê. CReporTool ñ÷èòàåò èòîãè ñàìîñòîÿòåëüíî è íå óìååò ðàñïîçíàâàòü èòîãîâûå ñòðîêè â ðåêîðäñåòå.
</li>
<p>

Ïðèìåðíûé âèä îò÷åòà ïðèâîäèòñÿ íèæå:<br /><br />

<style type="text/css">
/** styles for report drawing **/
   td.rep_ltrb { border: 1px solid #000000; text-align:center; font-family:arial,verdana; font-size:4mm;}
   td.rep_lrb { border-left:1px solid #000000; border-top:none; border-right: 1px solid #000000; border-bottom: 1px solid #000000; text-align:left; font-family:arial,verdana; font-size:4mm;}
   td.rep_rb  { border-left:none; border-top:none; border-right: 1px solid #000000; border-bottom: 1px solid #000000; text-align:left; font-family:arial,verdana; font-size:4mm; }
   td.num { text-align: right; };
   td.cnt { text-align: center; };
   td.newgroup { background: #EAEAEA; font-family:arial,verdana; font-size:4mm; }
</style>
<h4 style='text-align:center; font-weight:bold;'>Report: All animals in zoo</h4>
<div id="divreport" style="text-align:center;"><table border="0" cellpadding="2" cellspacing="0"><tr><td class="rep_ltrb">Nick</td><td class="rep_ltrb">Gender</td><td class="rep_ltrb">Birth date</td><td class="rep_ltrb">Weight, kg</td></tr><tr><td class="rep_lrb newgroup" colspan="4">Animal category  Sea animals</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4"> &nbsp;&nbsp;class : whales</td></tr>
<tr><td class="rep_lrb">&nbsp;Whale-Boy</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;06/22/1997</td><td class="rep_rb num">&nbsp;7,000</td></tr>
<tr><td class="rep_lrb">&nbsp;Whale-Girl</td><td class="rep_rb">&nbsp;female</td><td class="rep_rb">&nbsp;08/01/2000</td><td class="rep_rb num">&nbsp;6,500</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for whales</td><td class="rep_rb num" >13,500</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4"> &nbsp;&nbsp;class : sharks</td></tr>
<tr><td class="rep_lrb">&nbsp;BloodyJaws</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;09/15/2001</td><td class="rep_rb num">&nbsp;1,500</td></tr>
<tr><td class="rep_lrb">&nbsp;HungryMary</td><td class="rep_rb">&nbsp;female</td><td class="rep_rb">&nbsp;11/10/2002</td><td class="rep_rb num">&nbsp;1,200</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for sharks</td><td class="rep_rb num" >2,700</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4"> &nbsp;&nbsp;class : dolphins</td></tr>
<tr><td class="rep_lrb">&nbsp;BigMartha</td><td class="rep_rb">&nbsp;female</td><td class="rep_rb">&nbsp;05/20/1998</td><td class="rep_rb num">&nbsp;3,000</td></tr>
<tr><td class="rep_lrb">&nbsp;BigBob</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;01/12/1996</td><td class="rep_rb num">&nbsp;3,500</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for dolphins</td><td class="rep_rb num" >6,500</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for category Sea animals</td><td class="rep_rb num" >22,700</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4">Animal category  Jungle animals</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4"> &nbsp;&nbsp;class : elephants</td></tr>
<tr><td class="rep_lrb">&nbsp;LazyTom</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;10/23/2004</td><td class="rep_rb num">&nbsp;5,000</td></tr>
<tr><td class="rep_lrb">&nbsp;MightyJack</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;06/16/2003</td><td class="rep_rb num">&nbsp;5,300</td></tr>
<tr><td class="rep_lrb">&nbsp;Samantha-Shy</td><td class="rep_rb">&nbsp;female</td><td class="rep_rb">&nbsp;04/24/2004</td><td class="rep_rb num">&nbsp;4,200</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for elephants</td><td class="rep_rb num" >14,500</td></tr>
<tr><td class="rep_lrb newgroup" colspan="4"> &nbsp;&nbsp;class : gorillas</td></tr>
<tr><td class="rep_lrb">&nbsp;Gorilla-Mom</td><td class="rep_rb">&nbsp;female</td><td class="rep_rb">&nbsp;08/23/2001</td><td class="rep_rb num">&nbsp;150</td></tr>
<tr><td class="rep_lrb">&nbsp;Gorilla-Son</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;04/15/2008</td><td class="rep_rb num">&nbsp;40</td></tr>
<tr><td class="rep_lrb">&nbsp;Gorilla-Dad</td><td class="rep_rb">&nbsp;male</td><td class="rep_rb">&nbsp;02/05/2000</td><td class="rep_rb num">&nbsp;180</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for gorillas</td><td class="rep_rb num" >370</td></tr>
<tr><td class="rep_lrb" colspan="3">Totals for category Jungle animals</td><td class="rep_rb num" >14,870</td></tr>
<tr><td class="rep_lrb" colspan="3">Summary all animals (12) </td><td class="rep_rb num" >37,570</td></tr>
</table></div>
</p>

<br />
 äèñòðèáóòèâå äîëæåí ïðèñóòñòâîâàòü ïðèìåð ñêðèïòà, ôîðìèðóþùèé íàø îò÷åò.


<h4>Change log (Èñòîðèÿ èçìåíåíèé)</h4>

<h5>1.00.002 (11/26/2008)</h5>
Ïåðâûé ðåëèç

<!-- DOC END -->
<hr>
<div align=center><font size=-2>Copyright &copy; Alexander Selifonov, <a href="http://www.selifan.ru">www.selifan.ru</a>,
distrubuted under <a href="http://www.gnu.org/copyleft/gpl.html" target="_blank">GPL</a>
</font></div>
</body></html>
Return current item: Report generating and drawing