TITAN Library for PHP5  5.6.0
Web Software Development Kit for PHP5
Example 4: "pdo/MySQL to XML"

Table of Contents

PHP Source:

<?php
    $titan_library_path = '/path/to/titan/lib/';
    require_once $titan_library_path.'tl.core.init.php';

    $db = new \tl\storage\pdo\mysql('mysql_hostname', 'mysql_username', 'mysql_password', 'mysql_database');
    $table = 'auth_department';
    $parent = 'dept';
    $depts = $db->get_rows("select `idx`, `item_id`, `description` from `".$table."` where `parent_id` = '".$parent."'");

    $xml = new \tl\output\xml();
    $xml->open('Envelope');

    $xml->open('departments');
    foreach ($depts as $dept) {
        $xml->open($parent);
        foreach ($dept as $field => $value) {
            $xml->tag($field, $value);
        }
        $xml->close($parent);
    }
    $xml->complete();

Output:

<?xml version="1.0" ?>
<Envelope>
    <departments>
        <dept>
            <idx>2</idx>
            <item_id>dept_unassigned</item_id>
            <description>-- unassigned --</description>
        </dept>
        <dept>
            <idx>3</idx>
            <item_id>dept_acct</item_id>
            <description>Accounting</description>
        </dept>
        <dept>
            <idx>4</idx>
            <item_id>dept_admin</item_id>
            <description>Administration</description>
        </dept>
        <dept>
            <idx>5</idx>
            <item_id>dept_cs</item_id>
            <description>Customer Service</description>
        </dept>
        <dept>
            <idx>6</idx>
            <item_id>dept_hr</item_id>
            <description>Human Resources</description>
        </dept>
        <dept>
            <idx>7</idx>
            <item_id>dept_it</item_id>
            <description>Information Technology</description>
        </dept>
        <dept>
            <idx>8</idx>
            <item_id>dept_mfg</item_id>
            <description>Manufacturing</description>
        </dept>
        <dept>
            <idx>9</idx>
            <item_id>dept_mkt</item_id>
            <description>Marketing</description>
        </dept>
        <dept>
            <idx>10</idx>
            <item_id>dept_recv</item_id>
            <description>Receiving</description>
        </dept>
        <dept>
            <idx>11</idx>
            <item_id>dept_ship</item_id>
            <description>Shipping</description>
        </dept>
    </departments>
</Envelope>