I created this snippet to examine the structure of tables in the MODx database.
It was useful to me and might help others who want to develop methods of reading and editing database tables, but I don’t know if it’s useful enough to be in the Repository.
I welcome any suggestions on improving it and opinions on whether it’s Repository-worthy.
<?php
/*
::::::::::::::::::::::::::::::::::::::::
Snippet name: ShowMetaData
Short Desc: Displays db table metadata
Version: 0.5
Author: Bob Ray
Date: May 7, 2007
::::::::::::::::::::::::::::::::::::::::
Description:
Shows the metadata (table structure) of a table in the MODx database.
I wrote this as a quick and dirty way to look at the structure of any
table in the database.
I'm hoping to create a generic snippet that will
allow front-end editing of tables in the database.
I did this as a first step but thought it might be
useful, or at least instructive, for others.
::::::::::::::::::::::::::::::::::::::::
Example Usage:
[!ShowMetaData? &tableName=`modx_active_users`!]
The table name is just the name of the database
table you want to see the structure of.
Note: $tableName is a required parameter and must be
surrounded by backticks, not single quotes.
To use this snippet, just paste this code (minus the php tags)
into a new snippet called ShowMetaData. Then create a new document
with the snippet call: [!ShowMetaData? &tableName='name of your table'!]
::::::::::::::::::::::::::::::::::::::::
*/
//Check for $tableName parameter
if (!isset($tableName)) {
return 'Error: No table name in Snippet call.<br>
Example: [!showMetaData? &tableName=`mydbtable`!]<br>';
}
// Get the metadata from the MODx db
$structureArray = $modx->db->gettablemetadata($tableName);
echo '<h2>Structure of table: ',$tableName,'</h2>';
// Raw view of structure
// Comment out the following two lines if you don't want raw view
echo '<h3>Raw View</h3>';
print_r(array_values($structureArray)); //prints raw table structure
echo '<h3>Prettified View</h3>';
reset($structureArray); //may not be necessary but can't hurt
while (list($key, $val) = each($structureArray)) // walk through fields
{
echo "<b>$key</b>";echo "<br />"; // field header
while (list ($newkey, $newval) = each($val)) { //walk through each field's specifiers
echo "<span style='color:#ff0000;'> <b>$newkey:</b></span> $newval";
echo "<br />";
}
echo '<p> </p>'; //separate the fields
}
echo "Note: If Null: is not YES then field doesn't allow null values.";
?>
Bob Ray