13.2. Simple report example with data access in code #

13.2.1. Data
13.2.2. C program code
13.2.3. PHP program code
13.2.4. RLIB compatible PHP program code
13.2.5. Report description
13.2.6. Report PDF result

This example below is mostly the same as the previous one, with one exception: the database access is done from program code instead of putting it into the report XML description file.

13.2.1. Data #

As the same data is used as in the previous example, it's not duplicated here.

13.2.2. C program code #

The program code adds the datasource and the query before loading the report XML description. The order of these are not important, as the ocrpt_execute() call performs matching expressions with query column names internally.

#include <stdio.h>
#include <opencreport.h>

int main(int argc, char **argv) {
    opencreport *o = ocrpt_init();

    struct ocrpt_input_connect_parameter conn_params[] = {
        { .param_name = "dbname", .param_value = "ocrpttest" },
        { .param_name = "user", .param_value = "ocrpt" },
        { NULL }
    };

    ocrpt_datasource *ds = ocrpt_datasource_add(o, "pgsql", "postgresql", conn_params);

    ocrpt_query_add_sql(ds, "q", "select * from flintstones;");

    if (!ocrpt_parse_xml(o, "example2.xml")) {
        printf("XML parse error\n");
        ocrpt_free(o);
        return 0;
    }

    ocrpt_set_output_format(o, OCRPT_OUTPUT_PDF);
    ocrpt_execute(o);
    ocrpt_spool(o);
    ocrpt_free(o);

    return 0;
}

13.2.3. PHP program code #

Here's the equivalent program code in PHP.

<?php
$o = new OpenCReport();

$conn_params = [
    "dbname" => "ocrpttest",
    "user" => "ocrpt"
];

$ds = $o->datasource_add("pgsql", "postgresql", $conn_params);

$ds->query_add("q", "select * from flintstones;");

if (!$o->parse_xml("example2.xml")) {
    echo "XML parse error" . PHP_EOL;
    exit(0);
}

$o->execute();
$o->spool();

13.2.4. RLIB compatible PHP program code #

Here's the equivalent program code in PHP, using the RLIB compatibility functions.

<?php
$r = rlib_init();

rlib_add_datasource_postgres($r, "pgsql", "dbname=ocrpttest user=ocrpt");
rlib_add_query_as($r, "pgsql", "select * from flintstones;", "q");

if (!rlib_add_report($r, "example2.xml")) {
    echo "XML parse error" . PHP_EOL;
    exit(0);
}

rlib_execute($r);
rlib_spool($r);

13.2.5. Report description #

The program code uses this file contents from example2.xml. Note that the <Datasources> and <Queries> nodes that describe the database access and the query in the previous example are missing here. The equivalent code was added to the different program codes above.

<?xml version="1.0"?>
<!DOCTYPE OpenCReport SYSTEM "opencreport.dtd">
<OpenCReport>
    <Report query="q">
        <PageHeader>
            <Output>
                <Line>
                    <literal width="20">The Flintstones</literal>
                    <field value="printf('Page %d / %d', r.pageno, r.totpages)" align="right" />
                </Line>
            </Output>
        </PageHeader>

        <PageFooter>
            <Output>
                <Line>
                    <literal>The Flintstones</literal>
                    <field value="printf('Page %d / %d', r.pageno, r.totpages)" align="right" />
                </Line>
            </Output>
        </PageFooter>

        <Detail>
            <FieldHeaders>
                <Output>
                    <Line>
                        <literal width="4" align="'right'">ID</literal>
                        <literal width="1"/>
                        <literal width="20">Name</literal>
                        <literal width="1"/>
                        <literal width="8" align="'center'">Property</literal>
                        <literal width="1"/>
                        <literal width="6">Age</literal>
                        <literal width="1"/>
                        <literal width="5" align="'center'">Adult</literal>
                    </Line>
                </Output>
            </FieldHeaders>

            <FieldDetails>
                <Output>
                    <Line>
                        <field width="4" align="right" value="id" />
                        <literal width="1"/>
                        <field width="20" value="name" />
                        <literal width="1"/>
                        <field width="8" align="'center'" value="property" />
                        <literal width="1"/>
                        <field width="6" align="'right'" value="age" format="'%.2d'" />
                        <literal width="1"/>
                        <field value="adult ? 'yes' : 'no'" width="5" align="'center'"/>
                    </Line>
                </Output>
            </FieldDetails>
        </Detail>
    </Report>
</OpenCReport>

13.2.6. Report PDF result #

The result is identical to the previous example, it's not duplicated here.