Zend Certified Engineer

phpguru.org

Quality PHP, Javascript and C# code
Download RGraph: HTML5 canvas graph library... Home ~ Downloads ~ FAQs ~ Licensing
RSS Feed RSS Feed
Follow me on Twitter Follow me on Twitter

Articles

HTML5

Javascript

PHP5

PHP4


Bookmark with delicious Stumble! this site tweet this site

MySQL Table Editor - allows you to easily
and safely manage your MySQL data

Introduction

A PHP 4 & 5 library to allow easy and user-friendly editing of MySQL tables. Note this is a library and not an application. It will be of no use unless you do some coding (not exactly a great deal though - see below).

Note: This is not meant to be an alternative or replacement to phpMyAdmin. This is something you can provide to users to manage tables without them happily destroying your database.

Features

Demo

There's a demo here, and the second code listing below is the script that drives it.

Example Usage

The following example is used in my admin area to allow administration of comments:

<?php
/**
* Edits news table
*/
require_once('TableEditor.php');

$editor = new TableEditor($database->connection'comments');

$editor->noDisplay('cm_parentid');
$editor->noEdit('cm_parentid');

$editor->setDisplayNames(array('cm_id'       => 'ID',
                              
'cm_neid'     => 'Article',
                              
'cm_datetime' => 'Date Added',
                              
'cm_author'   => 'Author',
                              
'cm_text'     => 'Text',
                              
'cm_email'    => 'Email'));

$editor->setDefaultOrderby('cm_datetime'0);
$editor->setConfig('searchableFields', array('cm_author''cm_text''cm_email'));

$editor->setInputType('cm_neid''select');
$editor->setValuesFromQuery('cm_neid'"SELECT ne_id, ne_title FROM news");

$editor->addDisplayFilter('cm_text'create_function('$v''return str_curtail($v, 100);'));

$editor->display();
?>

This is the demo script:

<?php

/**
* Demo of TableEditor class. Uses the following table:

* CREATE TABLE `TableEditorDemo` (
*   `te_id` int(10) unsigned NOT NULL auto_increment,
*   `te_name` varchar(32) NOT NULL default '',
*   `te_password` varchar(32) NOT NULL default '',
*   `te_email` varchar(32) NOT NULL default '',
*   `te_datetime` datetime NOT NULL default '0000-00-00 00:00:00',
*   `te_age` tinyint(3) unsigned NOT NULL default '0',
*   `te_live` enum('LIVE','NOT LIVE') default NULL,
*   `te_desc` mediumtext NOT NULL,
*   PRIMARY KEY  (`te_id`)
* ) TYPE=MyISAM;
*/

require_once('TableEditor.php');

$editor = new TableEditor($conn'TableEditorDemo');
    
// $editor->setConfig('allowView', false);
// $editor->setConfig('allowAdd', false);
// $editor->setConfig('allowEdit', false);
// $editor->setConfig('allowCopy', false);
// $editor->setConfig('allowDelete', false);

$editor->setConfig('perPage'15);

$editor->setDisplayNames(array('te_id'       => 'ID',
                               
'te_name'     => 'Name',
                               
'te_password' => 'Password',
                               
'te_email'    => 'Email',
                               
'te_datetime' => 'Date Added',
                               
'te_age'      => 'Age',
                               
'te_live'     => 'Live',
                               
'te_desc'     => 'Description'));

// $editor->noDisplay('te_password');
$editor->noEdit('te_live');

$editor->setInputType('te_password''password');
$editor->setInputType('te_email''email');

$editor->setSearchableFields('te_name''te_age''te_id''te_desc''te_live');
$editor->setRequiredFields('te_name''te_email''te_datetime''te_age''te_desc');

$editor->setDefaultOrderby('te_id');
$editor->setDefaultValues(array('te_id'   => '0',
                                
'te_live' => 'NOT LIVE'));

//$editor->addAdditionCallback(create_function('$data', 'foreach($data as $k => $v) {$body[] = "$k => $v";} mail("joe@example.com", "Row added", implode("\n", $body));'));
//$editor->addEditCallback(create_function('$data', 'foreach($data as $k => $v) {$body[] = "$k => $v";} mail("joe@example.com", "Row edited", implode("\n", $body));'));
//$editor->addCopyCallback(create_function('$data', 'foreach($data as $k => $v) {$body[] = "$k => $v";} mail("joe@example.com", "Row copied", implode("\n", $body));'));
//$editor->addDeleteCallback(create_function('$data', 'foreach($data as $k => $v) {$body[] = "$k => $v";} mail("joe@example.com", "Row deleted", implode("\n", $body));'));

function validateAge(&$obj$data)
{
    
$data = (int)$data;

    if (
$data 18 OR $data 80) {
        
$obj->addError('Invalid age! Please enter an age between 18 and 80');
    }

    return 
$data;
}

$editor->addValidationCallback('te_age''validateAge');

$editor->addDisplayFilter('te_desc'create_function('$v''return substr($v, 0, 100) . "...";'));

$editor->display();

?>

Public Method List

The following are the public methods of the object which can be used to manipulate the gui:

Dependencies

Translations

There's a german translation in the archive that someone has kindly provided (sorry - I don't have your name any more) I haven't tested this, so you have been warned!

ChangeLog

16th June 2005 (1.0.6)
==============

 o Fixed bug when not displaying primary key field.
 o Fixed javascript bug in FireFox when selecting records.
 o Added CSV download option. Can download either the selected records, the current
   page or the entire table. Works well with searching too (ie. you can download the
   current page even if it's a search result). There's a callback available to
   override which escapes a single field of CSV data. The built in escape function
   replaces carriage returns and line feeds with \r and \n respectively. It also
   escapes commas with a backslash.
 o Added advanced search capabilities


26th April 2005 (1.0.5)
===============

 o Added support for join tables. Using these you can join the main table onto other
   tables to retrieve columns from the "other" tables. When using join tables add
   copy and delete functionality is automatically disabled, however edits can still
   take place.
 o Added two new methods, onlyDisplay() and onlyEdit(). As the names suggest these
   methods set all fields to be hidden except those specified. Useful if you join
   onto tables and end up having to hide lots of columns.
 o Backported to PHP4. Be careful with references when using data validation callbacks.


19th April 2005 (1.0.4)
===============

 o Added support for a "bitmask" type. Descriptive values need to be set using the
   setValuesFrom*() methods. On the add/edit pages, this type is shown as a multiple
   select. The corresponding type in MySQL is a SET, however you could just as easily
   use an int.
 o Added a "Set blank password" checkbox to the "password" pseudo input, to allow
   setting of a blank password.
 o Changed field specific errors on add/editcopy page to be next to their respective
   fields. There's now a new method called setContextualError() to allow setting of
   these errors from data validation callbacks.


17th April 2005 (1.0.3)
===============

 o Fixed a bug which made multiple display filters on a column not work correctly.
 o Search now highlights found terms. Does this by applying a display filter to each
   searchable colum, which is applied *after* any user defined display filters.


15th April 2005 (1.0.2)
===============

 o No longer classed as beta, (well, by me at least... ).
 o When using the view button, fields with preset values are now shown
   as they are on the main table page.
 o Fixed a bug which meant edit was disabled when view was, and couldn't be
   disabled by itself.
 o Fixed redirection bug with urls.
 o Fixed leading zeros being removed when inserting purely numeric values into
   a text based field type.
 o Added row copying functionality.
 o Added data filters via addDataFilter() method to facilitate the
   showing/viewing/editing/deleting of only certain rows
 o Table structure is now no longer determined on each and every page refresh, but
   is cached in the session.
 o New method: setSearchableFields(). To be used instead of setConfig('searchableFields').
   Supply one or more field names which are allowed to be searched.
 o Added support for required fields when adding/editing/copying. If a required field is
   not filled in, an error is displayed.
 o Added support for pseudo input types:
    o password  This shows two password inputs on the add/edit page. If when
                submitted the two do not match, an error is displayed. However,
                if the two are left blank/empty, the field is not updated. This
                is to allow updates to rows without changing the password field.
                You can alter this behaviour by stipulating the field to be
                required.
    o email     This shows a regular text input, however it automatically checks
                whatever contents are supplied and ensures it matches the form of
                an email address. If however the field is empty, no error is
                raised. You can alter this behaviour by stipulating the field to
                be required.
 o Added support for data validation callbacks. These are run after functions, defaults,
   required field checks and pseudo field validation checks. Using these you can add your
   own validation checks before data is added/updated in the database. These callbacks
   may also modify the data (eg. md5()ing a password). Now these are added, functions
   default to off since along with default values functions are pretty much redundant.
 o Added handling for magic_quotes_gpc


14th April 2005 (1.0.1-beta)
===============

 o Changed aesthetics for shameful self promotion
 o noDisplay() method now only hides a field from display, not editing. Use
   noEdit() in combination to hide a field from editing too.
 o Renamed defaultOrderby() to setDefaultOrderby()
 o Fixed a number of E_NOTICE errors
 o Added setDefaultValues(), which sets default values when adding rows. Can
   be used with fields that aren't editable.
 o Added add/edit/delete callback function support via addAdditionCallback(),
   addEditCallback() and addDeleteCallback() methods. The callbacks are run when
   each appropriate action is taken.
 o Added ability to search fields which have their values preset with the
   setValuesFrom* methods.
 o Changed to using a single checkbox per row, and a single set of add/edit/delete
   buttons. Can now delete multiple rows at once.
 o Added View button, for viewing a row in full on it's own page.
 o Added support for external header and footer files. Changing the header will lose
   all CSS so you'll have to define your own.
 o You can now disable any/all of the view/add/edit/delete buttons.


11th April 2005 (1.0.0-beta)
===============

 o Initial release


RGraph: HTML5 canvas graph library

Example graph made using RGraph

If you're interested in web development, then you may also be interested in RGraph: HTML5 canvas graph library. It uses HTML5 features to produce a wide variety of graph types. Because it moves the creation of graphs from the server to the client, it can significantly reduce the load on your server and your bandwidth usage.