query("SHOW TABLE STATUS");
while ($row = $result->fetch_assoc()) {
if (!isset($row["Engine"])) { // view
continue;
}
$schema[$row["Name"]]["fields"] = fields($row["Name"]);
if ($row["Engine"] == "InnoDB") {
foreach (foreign_keys($row["Name"]) as $val) {
if (!$val["db"]) {
$schema[$val["table"]]["referenced"][$row["Name"]][] = array_combine($val["target"], $val["source"]);
}
}
}
}
$result->free();
?>
$table) {
echo "
\n";
echo '
' . htmlspecialchars($name) . "\n";
foreach (fields($name) as $field) {
$val = htmlspecialchars($field["field"]);
if (preg_match('~char|text~', $field["type"])) {
$val = "
$val";
} elseif (preg_match('~date|time|year~', $field["type"])) {
$val = "
$val";
} elseif (preg_match('~binary|blob~', $field["type"])) {
$val = "
$val";
} elseif (preg_match('~enum|set~', $field["type"])) {
$val = "
$val";
}
echo ($field["primary"] ? "
$val" : $val) . "
\n";
$top += 1.25;
$positions[$name][$field["field"]] = $top;
}
echo "
\n";
$top += 2.5;
}
$left = 46;
foreach ($schema as $name => $table) {
foreach ((array) $table["referenced"] as $target_name => $refs) {
foreach ($refs as $ref) {
$min_pos = $top;
$max_pos = 0;
foreach ($ref as $source => $target) {
$pos1 = $positions[$name][$source];
$pos2 = $positions[$target_name][$target];
$min_pos = min($min_pos, $pos1, $pos2);
$max_pos = max($max_pos, $pos1, $pos2);
echo "
\n";
echo "
\n";
}
echo "
\n";
$left -= 2;
}
}
}
//! JavaScript for dragging tables
?>