📄 phpmyedit.inc
字号:
<?php
/*
This is a generic table editing program. The table and fields to be
edited are defined in the calling program.
This program works in three passes. Pass 1 (the last part of
the program) displays the selected MySQL table in a scrolling table
on the screen. Radio buttons are used to select a record for editing
or deletion. If the user chooses Add, Change, or Delete buttons,
Pass 2 starts, displaying the selected record. If the user chooses
the Save button from this screen, Pass 3 processes the update and
the display returns to the original table view (Pass 1).
version 3.5 - 06-May-01
important variables passed between calls to this program
$fm first record to display
$inc no of records to display (SELECT ... LIMIT $fm,$inc)
$fl is the filter row displayed (boolean)
$rec no. of record selected for editing
$qf0,.. value of filter for column 0
$qfn value of all filters used during the last pass
$sfn sort field number (- = descending sort order)
$operation operation to do: Add, Change, Delete
$filter filter query
$sw filter display/hide button
$prev, $next navigation buttons
$labels narrative for buttons, etc
Conversion to PHP Classes by Pau Aliagas (pau@newtral.com)
*/
class PHPMyEdit {
var $hn; // hostname
var $un; // user name
var $pw; // password
var $db; // database
var $tb; // table
var $key; // Name of field which is the unique key
var $key_type; // Type of key field (int/real/string/date etc)
var $key_delim;
var $inc; // no of records to display (SELECT ... LIMIT $fm, $inc)
var $fm; // first record to display
var $fl; // is the filter row displayed (boolean)
var $options; /* Options for users: A(dd) C(hange) D(elete)
F(ilter) U(nsorted) */
var $fdd; // field definitions
var $qfn; // value of all filters used during the last pass
var $sfn; // sort field number (- = descending sort order)
var $rec; // no. of record selected for editing
var $prev, $next; // navigation buttons
var $sw; // filter display/hide button
var $labels; // labels for buttons, etc (multilingual)
var $operation; // operation to do: Add, Change, Delete
var $saveadd;
var $moreeadd;
var $savechange;
var $savedelete;
var $fds; // sql field names
var $num_fds; // number of fields
function set_values_from_table($database, $table, $key, $desc = '', $sel = '')
{
if ($desc == '') {
$desc = $key;
}
$qry = 'SELECT DISTINCT '.$key.','.$desc.' FROM '.$table.$sel.' ORDER BY '.$desc;
// echo('<p>'.$qry.'</p>');
$res = mysql_db_query($database, $qry);
while ($row = mysql_fetch_row($res)) {
$values['option'][] = $row[0];
$values['value'][] = $row[1];
}
return $values;
}
function fully_qualified_name($master_table, $field_defs, $field_name, $format='N')
{
if (isset($field_defs[$field_name]['values']['description'])) {
$fqn = $field_defs[$field_name]['values']['table'].'.`'.$field_defs[$field_name]['values']['description'].'`';
} else {
$fqn.= $master_table.'.`'.$field_name.'`';
if ( (isset($field_defs[$field_name]['format'])) && ($format == 'Y') )
{
$fqn = 'DATE_FORMAT('.$fqn.',\'';
$fqn = $fqn.'%'.$field_defs[$field_name]['format'][0].$field_defs[$field_name]['format'][3];
$fqn = $fqn.'%'.$field_defs[$field_name]['format'][1].$field_defs[$field_name]['format'][3];
$fqn = $fqn.'%'.$field_defs[$field_name]['format'][2].'\')';
}
}
return $fqn;
}
function create_column_list($master_table, $field_defs)
{
$select = 'SELECT DISTINCT';
$kwd = ' ';
for ($k = 0; $k < $this->num_fds; $k++) {
$fd = $this->fds[$k];
$select = $select.$kwd.$this->fully_qualified_name($master_table, $field_defs, $fd, 'Y');
$kwd = ',';
}
return $select;
}
function create_where_clause($master_table, $field_defs)
{
$kwd = ' WHERE (';
for ($k = 0 ; $k < $this->num_fds; $k++) {
$fd = $this->fds[$k];
if (isset($field_defs[$fd]['values']['description'])) {
$where = $where.$kwd.$master_table.'.`'.$this->fds[$k].'`='.$field_defs[$fd]['values']['table'].'.`'.$field_defs[$fd]['values']['column'].'`)';
$kwd = ' AND (';
}
}
return $where;
}
function create_from_clause($master_table, $field_defs)
{
$tbs[] = $master_table;
for ($k = 0 ; $k < sizeof($this->fds); $k++) {
$fd = $this->fds[$k];
if (isset($field_defs[$fd]['values']['description'])) {
if (!in_array($field_defs[$fd]['values']['table'],$tbs)) {
$tbs[]=$field_defs[$fd]['values']['table'];
}
}
}
$kwd = '';
$from = ' FROM ';
for ($k = 0; $k < sizeof($tbs); $k++) {
$from = $from.$kwd.$tbs[$k];
$kwd = ',';
}
return $from;
}
function add_enabled ()
{ return (stristr ($this->options, 'A')); }
function change_enabled ()
{ return (stristr ($this->options, 'C')); }
function delete_enabled ()
{ return (stristr ($this->options, 'D')); }
function filter_enabled ()
{ return (stristr ($this->options, 'F')); }
function initial_sort_suppressed ()
{ return (stristr ($this->options, 'I')); }
function add_operation ()
{ return (($this->operation == $this->labels[1]
or $this->saveadd == $this->labels[7] ) and $this->add_enabled ()); }
function more_operation () /*Add More*/
{ return (($this->moreadd == $this->labels[8]) and $this->add_enabled ()); }
function change_operation ()
{ return (($this->operation == $this->labels[2]
or $this->savechange == $this->labels[7] ) and $this->change_enabled()); }
function delete_operation ()
{ return (($this->operation == $this->labels[3]
or $this->savedelete == $this->labels[7] ) and $this->delete_enabled()); }
function filter_operation ()
{ return (isset($this->filter) and $this->filter_enabled ()); }
function displayed($k)
{
return (!isset($this->fdd[$this->fds[$k]]['options'])
or ($this->add_operation() and stristr($this->fdd[$this->fds[$k]]['options'],'A'))
or ($this->more_operation() and stristr($this->fdd[$this->fds[$k]]['options'],'A'))
or ($this->change_operation() and stristr($this->fdd[$this->fds[$k]]['options'],'C'))
or ($this->delete_operation() and stristr($this->fdd[$this->fds[$k]]['options'],'D')) );
}
function create_javascripts ()
{
if ($this->add_operation() or $this->change_operation() or $this->more_operation()) {
echo '<script type="text/javascript">'."\n";
echo '
function form_control(theForm)
{
'; /* echo */
for ($k = 0; $k < $this->num_fds; $k++)
{
if ($this->displayed($k))
{
if ($this->fdd[$this->fds[$k]]['required'] and !isset ($this->fdd[$this->fds[$k]]['values'])) {
echo "
if ( theForm.".$this->fds[$k].".value.length == 0 ) {
alert( '".$this->labels[13]." ".$this->fdd[$this->fds[$k]]['name']." .' );
theForm.".$this->fds[$k].".focus();
return false;
}
"; /* echo */
}
}
}
echo '
theForm.submit();
return true;
}
</script>' . "\n"; /* echo */
echo '<form action="'.$this->page_name.'" method="POST" onSubmit="return form_control(this);">'."\n";
} else {
echo '<form action="'.$this->page_name.'" method="POST">'."\n";
}
}
function display_add_record ()
{
for ($k = 0; $k < $this->num_fds; $k++)
{
if ( $this->displayed($k) )
{
echo ' <tr>'."\n";
echo ' <td>'.$this->fdd[$this->fds[$k]]['name'].'</td>'."\n";
if (isset ($this->fdd[$this->fds[$k]]['values']['table'])
and isset ($this->fdd[$this->fds[$k]]['values']['column'])
and isset ($this->fdd[$this->fds[$k]]['values']['description'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
$x = $this->set_values_from_table ($this->db,
$this->fdd[$this->fds[$k]]['values']['table'],
'`'.$this->fdd[$this->fds[$k]]['values']['column'].'`',
'`'.$this->fdd[$this->fds[$k]]['values']['description'].'`');
for ($l = 0; $l < count ($x['option']); $l++) {
echo ' <option value="'.$x['option'][$l].'">'.$x['value'][$l].'</option>'."\n";
}
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['values']['table'])
and isset ($this->fdd[$this->fds[$k]]['values']['column'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
$x = $this->set_values_from_table ($this->db,
$this->fdd[$this->fds[$k]]['values']['table'],
'`'.$this->fdd[$this->fds[$k]]['values']['column'].'`');
for ($l = 0; $l < count ($x['option']); $l++) {
echo ' <option>'.$x['option'][$l].'</option>'."\n";
}
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['values'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
for ($l = 0; $l < count ($this->fdd[$this->fds[$k]]['values']); $l++) {
echo ' <option>'.$this->fdd[$this->fds[$k]]['values'][$l].'</option>'."\n";
}
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['textarea'])) {
echo ' <td><textarea ';
if (isset ($this->fdd[$this->fds[$k]]['textarea']['rows'])) {
echo 'rows="'.$this->fdd[$this->fds[$k]]['textarea']['rows'].'" ';
}
if (isset ($this->fdd[$this->fds[$k]]['textarea']['cols'])) {
echo 'cols="'.$this->fdd[$this->fds[$k]]['textarea']['cols'].'" ';
}
echo 'name="'.$this->fds[$k].'"></textarea></td>'."\n";
} else {
/* Simple edit box required */
echo ' <td><input type="text" name="'.$this->fds[$k].'" value="" /></td>'."\n";
} /* if elseif else */
echo ' </tr>'."\n";
}
} /* for k < this->num_fds */
}
function display_change_delete_record ()
{
/*
for delete or change: SQL SELECT to retrieve the selected record
*/
$qry = $this->create_column_list ($this->tb, $this->fdd);
if (!in_array ($this->key, $this->fds)) {
$qry = $qry.','.$this->fully_qualified_name ($this->tb, $this->fdd, $this->key);
}
$qry = $qry.$this->create_from_clause ($this->tb, $this->fdd);
$qry_select = $this->create_where_clause ($this->tb, $this->fdd);
if ($qry_select == '') {
$kwd = ' WHERE ';
} else {
$kwd = ' AND ';
}
$qry = $qry.$qry_select.$kwd.'('.$this->fully_qualified_name($this->tb,$this->fdd,$this->key).' = '.$this->key_delim.$this->rec.$this->key_delim.')';
// echo '<p>'.$qry.'</p>';
$res = mysql_db_query ($this->db, $qry);
if ($row = mysql_fetch_array ($res))
{
for ($k = 0; $k < $this->num_fds; $k++)
{
if ($this->change_operation ())
{
if ( $this->displayed($k) )
{
echo ' <tr>'."\n";
echo ' <td>'.$this->fdd[$this->fds[$k]]['name'].'</td>'."\n";
$this->display_change_field ($row, $k);
echo ' </tr>'."\n";
}
}
if ($this->delete_operation ())
{
if ( $this->displayed($k) )
{
echo ' <tr>'."\n";
echo ' <td>'.$this->fdd[$this->fds[$k]]['name'].'</td>'."\n";
$this->display_delete_field ($row, $k);
echo ' </tr>'."\n";
}
}
} /* for */
} /* if row */
}
function display_change_field($row, $k)
{
if (isset ($this->fdd[$this->fds[$k]]['values']['table'])
and isset ($this->fdd[$this->fds[$k]]['values']['column'])
and isset ($this->fdd[$this->fds[$k]]['values']['description'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
$x = $this->set_values_from_table ($this->db,
$this->fdd[$this->fds[$k]]['values']['table'],
'`'.$this->fdd[$this->fds[$k]]['values']['column'].'`',
'`'.$this->fdd[$this->fds[$k]]['values']['description'].'`');
for ($l = 0; $l < count ($x['option']); $l++) {
if ($x['value'][$l] == $row[$k]) {
$w = ' selected';
} else {
$w = '';
}
echo ' <option'.$w.' value="'.$x['option'][$l].'">'.$x['value'][$l].'</option>'."\n";
} /* for */
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['values']['table'])
and isset ($this->fdd[$this->fds[$k]]['values']['column'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
$x = $this->set_values_from_table ($this->db,
$this->fdd[$this->fds[$k]]['values'] ['table'],
'`'.$this->fdd[$this->fds[$k]]['values'] ['column'].'`');
for ($l = 0; $l < count ($x['option']); $l++) {
if ($x['option'][$l] == $row[$k]) {
$w = ' selected';
} else {
$w = '';
}
echo ' <option'.$w.'>'.$x['option'][$l].'</option>'."\n";
} /* for */
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['values'])) {
echo ' <td><select name="'.$this->fds[$k].'" size="1">'."\n";
for ($l = 0; $l < count ($this->fdd[$this->fds[$k]]['values']); $l++) {
if ($this->fdd[$this->fds[$k]]['values'][$l] == $row[$k]) {
$w = ' selected';
} else {
$w = '';
}
echo $row[$k].' <option'.$w.'>'.$this->fdd[$this->fds[$k]]['values'][$l].'</option>'."\n";
} /* for */
echo ' </select></td>'."\n";
} elseif (isset ($this->fdd[$this->fds[$k]]['textarea'])) {
echo ' <td><textarea ';
if (isset ($this->fdd[$this->fds[$k]]['textarea']['rows'])) {
echo 'rows="'.$this->fdd[$this->fds[$k]]['textarea']['rows'].'" ';
}
if (isset ($this->fdd[$this->fds[$k]]['textarea']['cols'])) {
echo 'cols="'.$this->fdd[$this->fds[$k]]['textarea']['cols'].'" ';
}
echo 'name="'.$this->fds[$k].'">'.htmlentities($row[$this->fds[$k]]).'</textarea></td>'."\n";
} else {
echo ' <td><input type="text" name="'.$this->fds[$k].'" value="'.htmlentities($row[$k]).'" /></td>'."\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -