'index', 'Text' => 'area', 'Author' => 'char'); $fields_titles = array('ID' => 'ID', 'Text' => 'Qutes text', 'Author' => 'Quote author'); $fields_sizes = array('ID' => '10%', 'Text' => '60%', 'Author' => '30%'); $fields_align = array('ID' => 'center', 'Text' => 'left', 'Author' => 'left'); $class_titles = "panel"; $class_data = "table"; $class_error = "table_err"; $table_width = "90%"; // ==================================================================================================== // put common top HTML code TopCodeAdmin (); ContentBlockHead("Daily Quotes"); echo "
Add new
\n\n"; if ('update' == $_POST['action']) { $result = update_db(); } else if ('insert' == $_POST['action']) { $result = insert_db(); } else if ( 0 < strlen ($_POST['rows_form_submit'])) { $result = delete_records(); } if ( strlen($result) ) echo "
$result

"; if ('edit' == $_GET['action']) { echo get_edit_new_form (1); } else if ('insert' == $_GET['action']) { echo get_edit_new_form (0); } PrintData(); // put bottom top HTML code BottomCode(); // public functions =================================================================================== /** * print list of the table date */ function PrintData() { global $fields_types; global $fields_titles; global $fields_sizes; global $fields_align; global $class_titles; global $class_data; global $class_error; global $table_width; global $use_table; global $allow_delete; global $allow_edit; $query = "SELECT * FROM $use_table"; $res = db_res ($query); if ( !mysql_num_rows($res) ) { echo "
No data available
"; return; } echo "\n\n"; // print titles echo "\n"; if ( $allow_delete ) { echo ""; echo "\n"; } foreach ($fields_titles as $k => $v) { echo "\t\n"; } if ( $allow_edit ) { echo "\n"; } echo "\n\n"; // print data while ( $row = mysql_fetch_array ($res) ) { if ( $row[get_index_fieldname()] == $_GET['index_field'] || $row[get_index_fieldname()] == $_POST['index_field']) { echo "\n"; } else { echo "\n"; } // checkboxes if ( $allow_delete ) { echo "\n"; } // data foreach ( $fields_sizes as $k => $v ) { echo "\t\n"; } // edit button if ( $allow_edit ) { echo "\n"; } echo "\n\n"; } echo "
 $v 
". process_text_output($row[$k]) ."Edit
\n\n"; if ( $allow_delete ) { echo get_checkbox_menu() . ""; } ContentBlockFoot(); } // private functions ================================================================================== /** * get fields list */ function get_fields_list () { global $fields_types; $ret = ""; foreach ($fields_types as $k) { $ret += "`$k`,"; } return substr ($ret, 0, -1); } /** * get index field name */ function get_index_fieldname () { global $fields_types; $ret = ""; foreach ($fields_types as $k => $v) { if ($v == 'index' ) return $k; } return $fields_types[0]; } /** * get HTML code for delete records menu */ function get_checkbox_menu () { global $table_width; return <<  Check all / Uncheck all  with selected: EOS; } /** * get HTML code for delete records menu */ function get_edit_new_form ($edit_form) { global $table_width; global $fields_types; global $fields_titles; global $use_table; global $class_data; if ( $edit_form ) { $query = "SELECT * FROM $use_table WHERE ". get_index_fieldname() ."=". (int)$_GET['index_field']. " LIMIT 1"; $quote_arr = db_arr ($query); } $ret = ""; $ret .= "\n\n"; $ret .= "\n\n"; if ( $edit_form ) { $ret .= "\n"; $ret .= "\n"; } else { $ret .= "\n"; } foreach ($fields_types as $k => $v) { switch ($v) { case 'index': break; case 'area': $ret .= "\n\n"; $ret .= "\n\n\n"; break; case 'char': $ret .= "\n\n"; $ret .= "\n\n\n"; break; } } $ret .= "\n\n"; $ret .= "\n"; $ret .= "
{$fields_titles[$k]}
{$fields_titles[$k]}
\n"; $ret .= ""; $ret .= "
\n\n"; return $ret; } /** * insert new record to the database */ function insert_db () { global $use_table; global $fields_types; $query = "INSERT INTO $use_table SET "; foreach ($fields_types as $k => $v) { switch ($v) { case 'index': break; case 'area': case 'char': $query .= "$k = '". process_db_input( $_POST[$k] ) ."', "; break; } } $query = substr ($query, 0, -2); if (db_res ($query)) { return "
New data was successfully added
"; } else { return "
New data was NOT successfully added
"; } } /** * update record to the database */ function update_db () { global $use_table; global $fields_types; $query = "UPDATE $use_table SET "; foreach ($fields_types as $k => $v) { switch ($v) { case 'index': break; case 'area': case 'char': $query .= "$k = '". process_db_input( $_POST[$k] ) ."', "; break; } } $query = substr ($query, 0, -2); $query .= " WHERE ".get_index_fieldname()." = ". (int)$_POST['index_field'] ." LIMIT 1"; if (db_res ($query)) { return "
Data was successfully updated
"; } else { return "
Data was NOT updated
"; } } /** * delete the market records from the database */ function delete_records () { global $use_table; global $fields_types; global $MySQL; $num = 0; foreach ($_POST as $k => $v) { $k = (int)$k; if ( $k > 0 && $v == 'on' ) { $query = "DELETE FROM $use_table WHERE ". get_index_fieldname() ." = $k"; if ( db_res ($query) ) $num += mysql_affected_rows( $MySQL->link ); } } return "
$num rows was deleted
"; } ?>