xPDO quick displays of database table

Tags:
xpdo,

Create quick database table dumps

The functions provided on this page show various aspects of producing tabled output for a given display.

Protect the output

Password fields and others may need to be restricted from the display. A simple array can be used to supply the names of the fields to the display-creation functions to be skipped.

<?php
/**
 * Establishes a collection of fields not to be used in dump displays.
 *
 * @return array
 */
private function _getSkipFields() {
    return array ();
}

Create table headers

Field names can be retrieved directly from schema definitions. Formatting can be achieved using PHP and MODx Revolution filters: :ucfirst, :ucwords, or :ucase.

<?php
/**
 * Translate the record column names into a table header.
 *
 * @uses _getSkipFields() to limit the fields
 */
public function createTableHeader() {
    $skipArray = $this->_getSkipFields ();
    $out = '';
    foreach ($this->_fields as $field => $value  ) {
        if (! in_array ( $field, $skipArray )) {
            $out .= '<th>' . $field . '</th>';
        }
    }
    return ($out) ? '<thead></;tr>' . $out . '<tr></thead>' : '';
}

Create table datum

Takes the current record and returns the values for display output.

<?php
/**
 * Translate the record column values into a table row.
 *
 * @uses _getSkipFields() to limit the fields
 */
public function createTableRow() {
    $skipArray = $this->_getSkipFields ();
    $out = '';
    foreach ( $this->_fields as $field => $value ) {
        if (! in_array ( $field, $skipArray )) {
            $out .= '<td>' . $value . '</td>';
        }
    }
    return ($out) ? '<tr>' . $out . '</tr>' : '';
}

Implementation

To use these functions a collection of objects would be retrieved and then looped through.

<?php
$collection = $this->getSomeCollection ();
    if ($collection) {
        $c = 1;
        foreach ( $collection as $obj ) {
            if ($obj instanceof someClass) {
                if ($c == 1) {
                    $out .= $obj->createTableHeader ();
                    $c = 999;
                }
                $out .= $obj->createTableRow ();
            }
        }
    }
 return ($out) ? $out : '';

ject">restricted from the display. A simple array can be used to supply the names of the fields to the display-creation functions to be skipped.

 

<?php
/**
 * Establishes a collection of fields not to be used in dump displays.
 *
 * @return array
 */
private function _getSkipFields() {
    return array ();
}

Create table headers

Field names can be retrieved directly from schema definitions. Formatting can be achieved using PHP and MODx Revolution filters: :ucfirst, :ucwords, or :ucase.

<?php
/**
 * Translate the record column names into a table header.
 *
 * @uses _getSkipFields() to limit the fields
 */
public function createTableHeader() {
    $skipArray = $this->_getSkipFields ();
    $out = '';
    foreach ($this->_fields as $field => $value  ) {
        if (! in_array ( $field, $skipArray )) {
            $out .= '<th>' . $field . '</th>';
        }
    }
    return ($out) ? '<thead></;tr>' . $out . '<tr></thead>' : '';
}

Create table datum

Takes the current record and returns the values for display output.

<?php
/**
 * Translate the record column values into a table row.
 *
 * @uses _getSkipFields() to limit the fields
 */
public function createTableRow() {
    $skipArray = $this->_getSkipFields ();
    $out = '';
    foreach ( $this->_fields as $field => $value ) {
        if (! in_array ( $field, $skipArray )) {
            $out .= '<td>' . $value . '</td>';
        }
    }
    return ($out) ? '<tr>' . $out . '</tr>' : '';
}

Implementation

To use these functions a collection of objects would be retrieved and then looped through.

<?php
$collection = $this->getSomeCollection ();
    if ($collection) {
        $c = 1;
        foreach ( $collection as $obj ) {
            if ($obj instanceof someClass) {
                if ($c == 1) {
                    $out .= $obj->createTableHeader ();
                    $c = 999;
                }
                $out .= $obj->createTableRow ();
            }
        }
    }
 return ($out) ? $out '';