Re: [PHP] peer review (was php framework vs just php?)



On Fri, Apr 25, 2008 at 10:59 AM, Robert Cummings <robert@xxxxxxxxxxxxx>
wrote:

On Fri, 2008-04-25 at 11:17 -0500, Jay Blanchard wrote:
I did a quick and dirty of just one of those functions. This function
takes a table and creates a form based on the table. It needs quite a
bit of refining, but I am willing to share and let you guys and gals
throw suggestions;

function formCreate($database, $table, $action, $excludeCols,
$recordID){
/*
* This function is used to create forms on the fly based on
tables within
* 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
* for arguements not included.
*/
/* database connection in global variable */
global $dc;
/* number of arguements sent to function */
$numArgs = func_num_args();
/* test to make sure that you have the minial two arguements */
if(2 > $numArgs){
/* not enough arguments */
$errMsg = "not enough arguements supplied, please supply
database and table name";
return($errMsg);
} else {
/*
* 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;
* <input type="hidden" name="action" value="whatever
action is called">
* That way when the user clicks 'Submit' the programmer
can have a
* switch action in his or her processing script to
handle this form.
*/
if(!isset($action)) { $action = 'read'; }
if(!isset($recordID)) { $recordID = ''; }
if(!isset($excludeCols)){
$excludeCols = '';
} else {
/* create an array of excluded columns */
$arrExcludeCols = explode(",", $excludeCols);
}

/* describe the table */
$sqlDesc = "DESCRIBE `".$database."`.`".$table."` ";
if(!($dbInfo = mysql_query($sqlDesc, $dc))){
return mysql_error();
} else {
while($tableInfo = mysql_fetch_array($dbInfo)){
/*
* 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
"<label>".$tableInfo['Field']."</label>";
echo "<input
type=\"text\" name=\"".$tableInfo['Field']."\" size=\"".$regs[1]."\"
maxlength=\"".$regs[1]."\"><br />\n";
} elseif("text" ==
$tableInfo[1]) {
/* handle text columns
*/
echo
"<label>".$tableInfo['Field']."</label>";
echo "<textarea
name=\"".$tableInfo['Field']."\" cols=\"80\" rows=\"10\"></textarea><br
/>\n";
} elseif("enum" ==
substr($tableInfo[1], 0, 4)){
/* handle enumerated
columns and creat drop downs */
echo
"<label>".$tableInfo['Field']."</label>&nbsp;";
/*
* 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
* values in a drop-down
on the form
*/
preg_match_all(
"/'(.*)'/U", $tableInfo[1], $matches);
echo "<select
name=\"".$tableInfo['Field']."\">\n";
echo
"<option></option>\n";
for($i = 0; $i <
count($matches[1]); $i++){
echo
"<option>".$matches[1][$i]."</option>\n";
}
echo "</select><br
/>\n";
}//end if preg
}//end if in_array
}//end while tableInfo
/* set up the hidden action field */
echo "<input type=\"hidden\" name=\"action\"
value=\"".$action."\">\n";
/* provide a submit and reset button */
echo "<input type=\"submit\" name=\"submit\"
value=\"Submit\">&nbsp;&nbsp;";
echo "<input type=\"reset\" name=\"reset\"
value=\"Clear\"><br />\n";
return;
}// end if dbInfo
}// end if numArgs
}//end function formCreate

With this I need one function call to create the form on the page. I can
exclude columns and set an action to be taken once the form is filled
out.

There are several other functions and what I end up with is a set of
functions that do make up a form handling framework. This can also be
done in the context of OOP, I just want to show some procedural code.
This assumes MySQL as the database.

Let the laughter begin.....

I can't say I've ever had a form that exactly matched a database table
for a user perspective. From an admin perspective that changes, but that
was when I downloaded PHPMyAdmin for the client. It was amazing, every
form matched the database table and you could view all the rows too, and
even create your own queries.

Table discovery is something I use for marshalling data back and forth
from database tables, but not usually for presenting a user form.


to add to this; i dealt w/ a clunky codebase at my last job. working w/
single table form generation was a snap, why you could even tell it which
columns you wanted to display! it would also try to build the appropriate
input type in html and handled creation of select elements and what-not.
the problem came in when you wanted to work w/ more than one table. can you
say nightmare ? thats exactly what it was, so anyway in my exp, its quite
common where a form will be an amalgam of records (or portions thereof) of
several tables.

-nathan

Quantcast