Thread: [phpwebapp-commits] CVS: web_app/webobjects/form formWebObj.php,1.10,1.11 formWebObj.txt,1.4,1.5
Brought to you by:
dashohoxha
From: Dashamir H. <das...@us...> - 2006-01-04 08:32:48
|
Update of /cvsroot/phpwebapp/web_app/webobjects/form In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9661/webobjects/form Modified Files: formWebObj.php formWebObj.txt Log Message: Index: formWebObj.php =================================================================== RCS file: /cvsroot/phpwebapp/web_app/webobjects/form/formWebObj.php,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** formWebObj.php 4 Nov 2005 06:54:14 -0000 1.10 --- formWebObj.php 4 Jan 2006 08:32:38 -0000 1.11 *************** *** 65,68 **** --- 65,178 ---- return $form_name; } + + /** + * $record is an associative array where keys are named after + * the fields of the table. It is assumed that $table has + * a primary key that is an autoincrement number. + * The function builds and executes a query that inserts a row + * in the table. + */ + function insert_record($record, $table) + { + $record = $this->fix_record($record, $table); + $record = $this->pad_record($record, $table); + $values = $this->get_query_values($record); + $query = "INSERT INTO $table\n SET\n $values\n"; + WebApp::execQuery($query); + } + + /** + * $record is an associative array where keys are named after + * the fields of the table. $key is the name of the primary key. + * The function builds and executes a query that updates the + * fields given in the $record (which includes the primary key as well). + */ + function update_record($record, $table, $key) + { + $record = $this->fix_record($record, $table); + $values = $this->get_query_values($record); + $val = $record[$key]; + $query = "UPDATE $table\n SET\n $values\n WHERE $key = '$val'"; + WebApp::execQuery($query); + } + + /** + * $record is an associative array with values for some fields of + * the given table. This function removes any fields that are not + * in the table. + */ + function fix_record($record, $table_name) + { + //get an array of the field names of the table + $rs = WebApp::execQuery("SHOW FIELDS FROM $table_name"); + $arr_fields = $rs->getColumn('Field'); + + //remove from the record any fields that does not exist in the table + while (list($fld_name,$fld_value) = each($record)) + { + if (!in_array($fld_name,$arr_fields)) unset($record[$fld_name]); + } + + return $record; + } + + /** + * $record is an associative array with values for some fields of + * the given table. This function adds empty values for the missing fields. + */ + function pad_record($record, $table_name) + { + //get an array of the field names of the table + $rs = WebApp::execQuery("SHOW FIELDS FROM $table_name"); + $arr_fields = $rs->getColumn('Field'); + + //pad with empty values any missing fields + for ($i=0; $i < sizeof($arr_fields); $i++) + { + $fld_name = $arr_fields[$i]; + if (!isset($record[$fld_name])) $record[$fld_name] = ''; + } + + return $record; + } + + /** + * $record is an associative array of field names and their values. + * The function constructs and returns a list of values that can be + * used in an insert or update query, like this: + * fld1='val1', fld2='val2', fld3='val3' + */ + function get_query_values($record) + { + while (list($fld_name, $fld_value) = each($record)) + { + if ($fld_value=='NULL') + { + $arr_values[] = "$fld_name = NULL"; + } + else + { + $arr_values[] = "$fld_name = '$fld_value'"; + } + } + $values = implode(",\n ", $arr_values); + return $values; + } + + /** + * From the items of the given array create a listbox recordset + * (with the fields 'id' and 'label'), and insert it in the $webPage. + */ + function add_listbox_rs($rs_id, $arr_labels) + { + $rs = new EditableRS($rs_id); + for ($i=0; $i < sizeof($arr_labels); $i++) + { + $label = $arr_labels[$i]; + $rs->addRec(array('id' => $label, 'label' => $label)); + } + global $webPage; + $webPage->addRecordset($rs); + } } ?> \ No newline at end of file Index: formWebObj.txt =================================================================== RCS file: /cvsroot/phpwebapp/web_app/webobjects/form/formWebObj.txt,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** formWebObj.txt 15 Jul 2004 14:46:03 -0000 1.4 --- formWebObj.txt 4 Jan 2006 08:32:38 -0000 1.5 *************** *** 57,58 **** --- 57,96 ---- function setFormData(form, formData) //fills the form with the given data + + In the PHP code of the webbox, these functions can be used: + + + function insert_record($record, $table) + /** + * $record is an associative array where keys are named after + * the fields of the table. It is assumed that $table has + * a primary key that is an autoincrement number. + * The function builds and executes a query that inserts a row + * in the table. + */ + + + function update_record($record, $table, $key) + /** + * $record is an associative array where keys are named after + * the fields of the table. $key is the name of the primary key. + * The function builds and executes a query that updates the + * fields given in the $record (which includes the primary key as well). + */ + + + function fix_record($record, $table_name) + /** + * $record is an associative array with values for some fields of + * the given table. This function removes any fields that are not + * in the table. + */ + + + function pad_record($record, $table_name) + /** + * $record is an associative array with values for some fields of + * the given table. This function adds empty values for the missing fields. + */ + + + function add_listbox_rs($rs_id, $arr_labels) + /** + * From the items of the given array create a listbox recordset + * (with the fields 'id' and 'label'), and insert it in the $webPage. + */ |