Converting MySQL into Form

Converting MySQL into Form

am 27.10.2009 21:19:30 von Ben C

--000e0cd5c74277ecdc0476f0668e
Content-Type: text/plain; charset=ISO-8859-1

Anyone know of a way to can take Mysql tables/fields from phpMyAdmin or .sql
file and quickly make into HTML forms?

--000e0cd5c74277ecdc0476f0668e--

RE: Converting MySQL into Form

am 28.10.2009 13:39:27 von Jay Blanchard

[snip]
Anyone know of a way to can take Mysql tables/fields from phpMyAdmin or
..sql
file and quickly make into HTML forms?
[/snip]

Search the archive of this list. I posted a function that does this a
long while back.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Converting MySQL into Form

am 28.10.2009 13:48:18 von Jay Blanchard

[snip]
Anyone know of a way to can take Mysql tables/fields from phpMyAdmin or
..sql file and quickly make into HTML forms?
[/snip]

Here is that function...(there are many mods that can be made to this,
but it works)

/*
* SQL Extactor
* Author: jblanchard
*
* Business Rule:
*
* REVISION: Jun 19, 2006=20
* USAGE: used to create form based on sql database info.
* dis be da ugly version, needs to be funkionized
*=20
*/


/* database connection */=20
if(!$dc =3D mysql_connect('yourdatabase', 'username', 'password')){
echo mysql_error();
exit();
}


function formCreate($database, $table, $action, $excludeCols,
$recordID){
/*=20
* This function is used to create forms on the fly based on
tables within=20
* the database. The minimal arguments are database name and
table name.
* Additional arguements may be supplied to indicate columns to
be excluded
* from form and an action (CRUD) to be performed once the form
* is filled out. If wanting to do an update, read or delete you
can specify
* a record to retrieve to populate the form. Default values
will be provided=20
* for arguements not included.
*/
/* database connection in global variable */
global $dc;
/* number of arguements sent to function */
$numArgs =3D func_num_args();
/* test to make sure that you have the minial two arguements */
if(2 > $numArgs){
/* not enough arguments */
$errMsg =3D "not enough arguements supplied, please supply
database and table name";
return($errMsg);
} else {
/*=20
* Supply default values for optional arguements if they
are not set.
* An interesting note here: the action can be anything
that the user
* specifies, it is not strictly limited to CRUD and it
will be output
* in a hidden form field;=20
* action is called">=20
* That way when the user clicks 'Submit' the programmer
can have a=20
* switch action in his or her processing script to
handle this form.=20
*/
if(!isset($action)) { $action =3D 'read'; }
if(!isset($recordID)) { $recordID =3D ''; }
if(!isset($excludeCols)){
$excludeCols =3D '';
} else {
/* create an array of excluded columns */
$arrExcludeCols =3D explode(",", $excludeCols);
}
=09
/* describe the table */
$sqlDesc =3D "DESCRIBE `".$database."`.`".$table."` ";
if(!($dbInfo =3D mysql_query($sqlDesc, $dc))){
return mysql_error();
} else {
while($tableInfo =3D mysql_fetch_array($dbInfo)){
/*=20
* regular expression - we need the data
that exists between the
* parentheses in the Type column of the
database being described
* so that we can use for length values
of form fields
*/
if(!(in_array($tableInfo['Field'],
$arrExcludeCols))){
if(preg_match (
"/\((\d{1,}.*?)\)/", $tableInfo[1], $regs )){
/* handle numerical
values in parentheses to create form element lengths */
echo
"";
echo " type=3D\"text\" name=3D\"".$tableInfo['Field']."\" =
size=3D\"".$regs[1]."\"
maxlength=3D\"".$regs[1]."\">
\n";
} elseif("text" ==
$tableInfo[1]) {
/* handle text columns
*/
echo
"";
echo " name=3D\"".$tableInfo['Field']."\" cols=3D\"80\" =
rows=3D\"10\"> />\n";
} elseif("enum" ==
substr($tableInfo[1], 0, 4)){
/* handle enumerated
columns and creat drop downs */
echo
" ";
/*
* regular expression -
we need the data that
* exists between the
single quotes in the Type column of the
* database being
described so that we can use for option=20
* values in a drop-down
on the form
*/
preg_match_all(
"/'(.*)'/U", $tableInfo[1], $matches);
echo " name=3D\"".$tableInfo['Field']."\">\n";
echo
"\n";
for($i =3D 0; $i <
count($matches[1]); $i++){
echo
"\n";
}
echo " />\n";
}//end if preg
}//end if in_array
}//end while tableInfo
/* set up the hidden action field */
echo " value=3D\"".$action."\">\n";
/* provide a submit and reset button */
echo " value=3D\"Submit\">  ";
echo " value=3D\"Clear\">
\n";
return;
}// end if dbInfo
}// end if numArgs
}//end function formCreate



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php