XML_Query2XML
[ class tree: XML_Query2XML ] [ index: XML_Query2XML ] [ all elements ]

Source for file case06.php

Documentation is available at case06.php

  1. <?php
  2. require_once('XML/Query2XML.php');
  3. require_once('DB.php');
  4. $query2xml XML_Query2XML::factory(DB::connect('mysql://root@localhost/Query2XML_Tests'));
  5. $dom $query2xml->getXML(
  6.     "SELECT
  7.          s.*,
  8.          manager.employeeid AS manager_employeeid,
  9.          manager.employeename AS manager_employeename,
  10.          d.*,
  11.          department_head.employeeid AS department_head_employeeid,
  12.          department_head.employeename AS department_head_employeename,
  13.          e.*,
  14.          sa.*,
  15.          c.*,
  16.          al.*,
  17.          ar.*,
  18.          (SELECT COUNT(*) FROM sale WHERE sale.store_id = s.storeid) AS store_sales,
  19.          (SELECT
  20.             COUNT(*)
  21.           FROM
  22.             sale, employee, employee_department
  23.           WHERE
  24.             sale.employee_id = employee.employeeid
  25.             AND
  26.             employee_department.employee_id = employee.employeeid
  27.             AND
  28.             employee_department.department_id = d.departmentid
  29.          ) AS department_sales,
  30.          (SELECT
  31.             COUNT(*)
  32.           FROM
  33.             employee, employee_department, department
  34.           WHERE
  35.             employee_department.employee_id = employee.employeeid
  36.             AND
  37.             employee_department.department_id = department.departmentid
  38.             AND
  39.             department.store_id = s.storeid
  40.          ) AS store_employees,
  41.          (SELECT
  42.             COUNT(*)
  43.           FROM
  44.             employee, employee_department
  45.           WHERE
  46.             employee_department.employee_id = employee.employeeid
  47.             AND
  48.             employee_department.department_id = d.departmentid
  49.          ) AS department_employees
  50.      FROM
  51.          store s
  52.           LEFT JOIN employee manager ON s.manager = manager.employeeid
  53.          LEFT JOIN department d ON d.store_id = s.storeid
  54.           LEFT JOIN employee department_head ON department_head.employeeid = d.department_head
  55.           LEFT JOIN employee_department ed ON ed.department_id = d.departmentid
  56.            LEFT JOIN employee e ON e.employeeid = ed.employee_id
  57.             LEFT JOIN sale sa ON sa.employee_id = e.employeeid
  58.              LEFT JOIN customer c ON c.customerid = sa.customer_id
  59.              LEFT JOIN album al ON al.albumid = sa.album_id
  60.               LEFT JOIN artist ar ON ar.artistid = al.artist_id",
  61.     array(
  62.         'rootTag' => 'music_company',
  63.         'rowTag' => 'store',
  64.         'idColumn' => 'storeid',
  65.         'attributes' => array(
  66.             'storeid'
  67.         ),
  68.         'elements' => array(
  69.             'store_sales',
  70.             'store_employees',
  71.             'manager' => array(
  72.                 'idColumn' => 'manager_employeeid',
  73.                 'attributes' => array(
  74.                     'manager_employeeid'
  75.                 ),
  76.                 'elements' => array(
  77.                     'manager_employeename'
  78.                 )
  79.             ),
  80.             'address' => array(
  81.                 'elements' => array(
  82.                     'country',
  83.                     'state' => '#Helper::getStatePostalCode()',
  84.                     'city',
  85.                     'street',
  86.                     'phone'
  87.                 )
  88.             ),
  89.             'department' => array(
  90.                 'idColumn' => 'departmentid',
  91.                 'attributes' => array(
  92.                     'departmentid'
  93.                 ),
  94.                 'elements' => array(
  95.                     'department_sales',
  96.                     'department_employees',
  97.                     'departmentname',
  98.                     'department_head' => array(
  99.                         'idColumn' => 'department_head_employeeid',
  100.                         'attributes' => array(
  101.                             'department_head_employeeid'
  102.                         ),
  103.                         'elements' => array(
  104.                             'department_head_employeename'
  105.                         )
  106.                     ),
  107.                     'employees' => array(
  108.                         'rootTag' => 'employees',
  109.                         'rowTag' => 'employee',
  110.                         'idColumn' => 'employeeid',
  111.                         'attributes' => array(
  112.                             'employeeid'
  113.                         ),
  114.                         'elements' => array(
  115.                             'employeename',
  116.                             'sales' => array(
  117.                                 'rootTag' => 'sales',
  118.                                 'rowTag' => 'sale',
  119.                                 'idColumn' => 'saleid',
  120.                                 'attributes' => array(
  121.                                     'saleid'
  122.                                 ),
  123.                                 'elements' => array(
  124.                                     'timestamp',
  125.                                     'customer' => array(
  126.                                         'idColumn' => 'customerid',
  127.                                         'attributes' => array(
  128.                                             'customerid'
  129.                                         ),
  130.                                         'elements' => array(
  131.                                             'first_name',
  132.                                             'last_name',
  133.                                             'email'
  134.                                         )
  135.                                     ),
  136.                                     'album' => array(
  137.                                         'idColumn' => 'albumid',
  138.                                         'attributes' => array(
  139.                                             'albumid'
  140.                                         ),
  141.                                         'elements' => array(
  142.                                             'title',
  143.                                             'published_year',
  144.                                             'comment' => '?#Helper::summarizeComment(12)',
  145.                                             'artist' => array(
  146.                                                 'idColumn' => 'artistid',
  147.                                                 'attributes' => array(
  148.                                                     'artistid'
  149.                                                 ),
  150.                                                 'elements' => array(
  151.                                                     'name',
  152.                                                     'birth_year',
  153.                                                     'birth_place',
  154.                                                     'genre'
  155.                                                 )
  156.                                             )
  157.                                         // album elements
  158.                                     //album array
  159.                                 //sales elements
  160.                             //sales array
  161.                         //employees elements
  162.                     //employees array
  163.                 //department elements
  164.             // department array
  165.         //root elements
  166.     //root
  167. )//getXML method call
  168.  
  169. $root $dom->firstChild;
  170. $root->setAttribute('date_generated'date("Y-m-d\TH:i:s"1124801570));
  171.  
  172. header('Content-Type: application/xml');
  173. echo '<?xml version="1.0" encoding="UTF-8"?>' "\n";
  174.  
  175. require_once('XML/Beautifier.php');
  176. $beautifier = new XML_Beautifier();
  177. print $beautifier->formatString($dom->saveXML());
  178.  
  179.  
  180.  
  181. /**Static class that provides validation and parsing methods for
  182. * generating XML.
  183. *
  184. * It is static so that we can easyly call its methods from inside
  185. * Query2XML using eval'd code.
  186. */
  187. class Helper
  188. {
  189.     /**Associative array of US postal state codes*/
  190.     public static $statePostalCodes = array(
  191.         'ALABAMA' => 'AL''ALASKA' => 'AK''AMERICAN SAMOA' => 'AS''ARIZONA' => 'AZ''ARKANSAS' => 'AR''CALIFORNIA' => 'CA',
  192.         'COLORADO' => 'CO''CONNECTICUT' => 'CT''DELAWARE' => 'DE''DISTRICT OF COLUMBIA' => 'DC''FEDERATED STATES OF MICRONESIA' => 'FM',
  193.         'FLORIDA' => 'FL''GEORGIA' => 'GA''GUAM' => 'GU''HAWAII' => 'HI''IDAHO' => 'ID''ILLINOIS' => 'IL''INDIANA' => 'IN',
  194.         'IOWA' => 'IA''KANSAS' => 'KS''KENTUCKY' => 'KY''LOUISIANA' => 'LA''MAINE' => 'ME''MARSHALL ISLANDS' => 'MH''MARYLAND' => 'MD',
  195.         'MASSACHUSETTS' => 'MA''MICHIGAN' => 'MI''MINNESOTA' => 'MN''MISSISSIPPI' => 'MS''MISSOURI' => 'MO''MONTANA' => 'MT',
  196.         'NEBRASKA' => 'NE''NEVADA' => 'NV''NEW HAMPSHIRE' => 'NH''NEW JERSEY' => 'NJ''NEW JESEY' => 'NJ''NEW MEXICO' => 'NM''NEW YORK' => 'NY',
  197.         'NORTH CAROLINA' => 'NC''NORTH DAKOTA' => 'ND''NORTHERN MARIANA ISLANDS' => 'MP''OHIO' => 'OH''OKLAHOMA' => 'OK''OREGON' => 'OR',
  198.         'PALAU' => 'PW''PENNSYLVANIA' => 'PA''PUERTO RICO' => 'PR''RHODE ISLAND' => 'RI''SOUTH CAROLINA' => 'SC''SOUTH DAKOTA' => 'SD',
  199.         'TENNESSEE' => 'TN''TEXAS' => 'TX''UTAH' => 'UT''VERMONT' => 'VT''VIRGIN ISLANDS' => 'VI''VIRGINIA' => 'VA''WASHINGTON' => 'WA',
  200.         'WEST VIRGINIA' => 'WV''WISCONSIN' => 'WI''WYOMING' => 'WY'
  201.     );
  202.             
  203.     /**Translates a US state name into its two-letter postal code.
  204.     * If the translation fails, $state is returned unchanged
  205.     * @param $record The record
  206.     */
  207.     public static function getStatePostalCode($record)
  208.     {
  209.         $state $record["state"];
  210.         $s str_replace("  "" "trim(strtoupper($state)));
  211.         if (isset(self::$statePostalCodes[$s])) {
  212.             return self::$statePostalCodes[$s];
  213.         else {
  214.             return $state;
  215.         }
  216.     }
  217.       
  218.     function summarize($str$limit=50$appendString=' ...')
  219.     {
  220.         if (strlen($str$limit{
  221.             $str substr($str0$limit strlen($appendString)) $appendString;
  222.         }
  223.         return $str;
  224.     }
  225.     
  226.     function summarizeComment($record$limit)
  227.     {
  228.         return self::summarize($record["comment"]$limit);
  229.     }
  230. }
  231. ?>

Documentation generated on Mon, 11 Mar 2019 14:49:10 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.