First Commit

This commit is contained in:
mark committed 2026-10-19 15:22:52 -07:00
1 parent 5eb9dd037d
commit 2e62c0e60b
11877 files changed
+2035312

No files matched your search

+492
View File
@@ -0,0 +1,492 @@
<?php
if (! $TABLE_INCLUDED) {
$TABLE_INCLUDED = 1;
// Define the Table class
// - This assumes we have already connected to the database
class Table
{
var $table_name;
var $column_names = array();
var $column_types = array();
var $column_nulls = array();
var $key_columns = array();
var $num_columns;
var $columns = array();
var $userdata;
var $err;
var $err_no;
//
// Initialize the table data object with column and key information
//
function Table($table_name) {
// Setup main parameters
$this->table_name = $table_name;
// Get table information
$query = "show columns from $table_name";
$result = mysql_query($query);
if (! $result) {
$this->err = mysql_error();
$this->err_no = 103;
return;
}
// Get column information
$c = 0;
$data = array();
while ($row = mysql_fetch_array($result)) {
$data[$c] = $row;
$c++;
}
$this->num_columns = count($data);
$this->columns = $data;
for ($i = 0; $i < $this->num_columns; $i++) {
$row = $data[$i];
$name = $row[0];
$type = ereg_replace("\(.*\)", "", $row[1]);
$type = ereg_replace(" unsigned", "", $type);
$null = ($row[2] ? "Y" : "N");
$key = $row[3];
$this->column_names[$i] = $name;
$this->column_types[$i] = $type;
$this->column_nulls[$i] = $null;
$this->key_columns[$i] = ($key == "PRI");
$this->column_types[$name] = $type;
$this->column_nulls[$name] = $null;
$this->key_columns[$name] = ($key == "PRI");
}
$this->err_no = 0;
} // End of Table() function
//
// Display the table structure in report format
//
function ShowTable() {
printf("<B> Table: $this->table_name</B>\n\n");
printf("<B> %-20s %-15s %-15s% -15s</B>\n",
"Column", "Type", "Null", "Pri");
for ($i = 0; $i < count($this->column_names); $i++) {
printf("%4d %-20s %-15s %-15s %-15s\n",
$i + 1,
$this->column_names[$i],
$this->column_types[$i],
$this->column_nulls[$i],
$this->key_columns[$i]);
}
return;
} // End of ShowTable() function
//
// Display table structure in SQL format
//
function ShowStructure() {
print("create table $this->table_name (\n");
for ($i = 0; $i < count($this->columns); $i++) {
$row = $this->columns[$i];
$name = $row[0];
$type = $row[1];
$null = ($row[2] ? "" : " not null");
switch ($row[3]) {
case "PRI":
if ($primary) $primary .= ", ";
$primary .= $name;
break;
case "UNI":
$unique .= " unique ($name),\n";
break;
case "MUL":
$index .= " index ($name),\n";
break;
default:
if ($row[3]) printf("%s - ", $row[3]);
}
printf(" %-20s%s%s,\n", $name, $type, $null);
}
printf("\n");
if ($unique) {
printf("%s\n", $unique);
}
if ($index) {
printf("%s\n", $index);
}
if ($primary) {
printf(" primary key (%s)\n", $primary);
}
print(");\n\n");
return;
} // End of ShowStructure() function
//
// Build an SQL query statement based on the current values
// of GLOBALS variables
//
// - This assumes that all values that are set in GLOBALS are to be used.
// It may be a problem if lingering data is in the GLOBALS array
//
function BuildQuery($orderby_fields="",$special_where="",$prefix="") {
// Initialize variables
$query = "";
$where = "$special_where";
$orderby = "";
$col = 0;
$ord = 0;
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$data = $GLOBALS[$name];
if (! empty($data) && $data != "NULL") {
// If there is a column already in the where clause add 'and'
if ($where) {
$where .= " and ";
}
// LIKE WILDCARD
if (ereg("[\*\?%]", $data)) {
$data = ereg_replace("[\*]", "%", $data);
$where .= "$prefix$name like " . $this->FieldData($name, $data);
}
// NOT EQUAL TO
elseif (ereg("^!= ",$data)) {
$data = ereg_replace("^!= ","",$data);
$where .= "$prefix$name != " . $this->FieldData($name, $data);
}
// LESS THAN OR EQUAL TO
elseif (ereg("^<= ",$data)) {
$data = ereg_replace("^<= ","",$data);
$where .= "$prefix$name <= " . $this->FieldData($name, $data);
}
// LESS THAN
elseif (ereg("^< ",$data)) {
$data = ereg_replace("^< ","",$data);
$where .= "$prefix$name < " . $this->FieldData($name, $data);
}
// GREATER THAN OR EQUAL TO
elseif (ereg("^>= ",$data)) {
$data = ereg_replace("^>= ","",$data);
$where .= "$prefix$name >= " . $this->FieldData($name, $data);
}
// GREATER THAN
elseif (ereg("^> ",$data)) {
$data = ereg_replace("^> ","",$data);
$where .= "$prefix$name > " . $this->FieldData($name, $data);
}
// EQUAL TO
else {
$where .= "$prefix$name = " . $this->FieldData($name, $data);
}
$col = 1;
}
// Build order by part based on field sequence
if ($name != "record_num" && $this->key_columns[$i]) {
if ($ord) {
$orderby .= ",";
}
$orderby .= "$prefix$name";
$ord = 1;
}
}
// Build up the statement
$query = "select * from $this->table_name";
// If where clause then append it
if ($where) {
$query .= " where $where";
}
// If there is an orderby clause append it
if ($orderby_fields) {
$query .= " order by " . $orderby_fields;
}
else {
if ($ord) {
$query .= " order by $orderby";
}
}
return $query;
} // End of BuildQuery() function
//
// Build the set of hidden fields for this table
// - This function has probably been replaced with the script:
// "hidden-form-fields.php"
//
function BuildHidden() {
// Initialize variables
$hidden = "";
$col = 0;
$ord = 0;
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$data = mysql_real_escape_string($GLOBALS[$name]);
$hidden .= "<INPUT TYPE=\"HIDDEN\" NAME=\"$name\" VALUE=\"$data\">\n";
}
return $hidden;
} // End of BuildHidden() function
//
// Build an SQL insert statement based on the values in the GLOBALS array
//
function BuildInsert($include_record_num='No') {
// Initialize variables
$query = "";
$q1 = "insert into $this->table_name (";
$q2 = "values (";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$data = $GLOBALS[$name];
if ($name != "record_num" || $include_record_num == 'Yes') {
// If there is a column already in the string add a comma
if ($i) {
$q1 .= ", ";
$q2 .= ", ";
}
$q1 .= $name;
$q2 .= $this->FieldData($name, $data);
}
}
$query = $q1 . ") " . $q2 . ")";
return $query;
} // End of BuildInsert() function
//
// Build an SQL update statement based on the values in the GLOBALS array
//
function BuildUpdate($mode="single") {
// Initialize variables
$query = "update $this->table_name set ";
$update = "";
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
if (($name != "record_num") && (isset($GLOBALS[$name]))) {
// If there is a column already in the string add a comma
$value = $this->FieldData($name, $GLOBALS[$name]);
// Single record update statement
if ($mode == "single") {
if ($update) $update .= ", ";
$update .= $name . " = " . $value;
}
// Multi record update statement
if ($mode == "multi") {
if ($value == "''") $value = NULL;
if ($value == "NULL") $value = NULL;
if ($value == "0") $value = NULL;
if ($value) {
if ($update) $update .= ", ";
$update .= $name . " = " . $value;
}
}
$col = 1;
}
}
// Define the where clause
$where_clause = "";
if ($mode == "single") {
$record_num = intval($GLOBALS["record_num"]);
$where_clause = "where record_num = $record_num";
}
$query = "$query $update $where_clause";
return $query;
} // End of BuildUpdate() function
//
// Format the data correctly based on the field type in
// the table structure
//
function FieldData($field_name, $data) {
$value = "";
$type = $this->column_types[$field_name];
$null = $this->column_nulls[$field_name];
$data = trim($data);
// format data based on field type
switch ($type) {
case "varchar":
case "text":
case "longtext":
case "char":
case "time":
case "enum":
$data = mysql_real_escape_string($data);
$value = "'$data'";
break;
case "int":
case "smallint":
case "decimal":
if (strlen($data) == 0) {
if ($null == "Y") {
$value = "NULL";
}
else {
$value = "0";
}
}
else {
if ($type == 'decimal') $value = floatval($data);
else $value = intval($data);
}
break;
case "date":
if (empty($data)) {
if ($null == "Y") {
$value = "NULL";
}
else {
$dt = date("Y-m-d", time());
$value = "'$dt'";
}
}
else {
$data = mysql_real_escape_string($data);
$value = "'$data'";
}
break;
case "datetime":
if (! $data) {
$dt = date("Y-m-d H:i:s", time());
$value = "'$dt'";
}
else {
$data = mysql_real_escape_string($data);
$value = "'$data'";
}
break;
case "timestamp":
if (! $data) {
$dt = date("YmdHis", time());
$value = "'$dt'";
}
else {
$data = mysql_real_escape_string($data);
$value = "'$data'";
}
break;
default:
$data = mysql_real_escape_string($data);
$value = "'$data'";
}
// return the correctly formatted data
return $value;
} // End of FieldData() function
//
// Function to convert a data record into GLOBALS data
//
function RecordData($record) {
// Process each field in the table
for ($i = 0; $i < count($this->column_names); $i++) {
$name = $this->column_names[$i];
$GLOBALS[$name] = $record[$name];
}
} // End of RecordData() function
} // End of class definition
} // End of if TABLE_INCLUDED statement
?>