Re: Dynamic form generation scripts???

From: Date: Tue, 21 Nov 2000 16:37:13 +0000
Subject: Re: Dynamic form generation scripts???
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-26467@lists.php.net to get a copy of this message
Here is my VERY beta admin form creator. It obviously needs to be cleaned up, documented, etc. so don't flame me. But, it does work :O) --Joe On Tue, Nov 21, 2000 at 09:10:05PM -0800, Dallas Kropka wrote: > Does anyone know of any good form generation scripts out there? Or am I going to have to write > my own? > > Thanks, > Dallas K. ================================================================================ = Joe Stump joestump98@yahoo.com http://www.miester.org = ================================================================================ Need a programmer? http://www.google.com/search?q=joe+stump+resume

<? class AdminForm{ // // // $fields = array( // type => 'submit,checkbox,text,textarea,radio,select', // required => 1 (0 if not), // attrs => array( // checked => 1 (0 if not), // cols => [0-9], // rows => [0-9], // size => [0-9], // selected => 1 (0 if not)), // value => 'This is a value!', // name => 'ThisWords' (will take the form of f[ThisWords] in final form), // help => 'This field is used for ...', // intro => 'User Name' (: will be appended)), // var $form; var $post; var $fields; var $colorA = '#CCCCCC'; var $colorB = '#EEEEEE'; var $bgcolor = '#FFFFFF'; var $header_color = '#33CCFF'; var $status = 'Waiting for user input'; var $table; var $tableID; var $required = array(); var $bg_counter; var $process; var $errors; var $final_sql; var $action; var $update; // Variables for the current field var $type; var $attrs; var $value; var $name; var $help; var $intro; function set_fields($new_fields){ $this->fields = $new_fields; } function set_post($new_post){ $this->post = $new_post; } function AdminForm($fields){ global $PHP_SELF, $HOME_URL, $HOME , $HEADER_TITLE, $HEADER_BLURB, $PROCESS, $TABLE, $TABLE_ID, $UPDATE_ID; $this->header_blurb = $HEADER_BLURB; $this->header_title = $HEADER_TITLE; $this->process = $PROCESS; $this->home_url = $HOME_URL; $this->home = $HOME; $this->errors = 0; $this->assign_table_info($TABLE,$TABLE_ID); $this->updateID = $UPDATE_ID; $this->set_post($PHP_SELF); $this->set_fields($fields); $this->generate_required_fields_array(); } function DumpForm(){ $this->dump_admin_top(); $this->dump_admin_form(); $this->dump_records(); $this->dump_admin_bottom(); } function dump_records(){ global $SHOW, $PHP_SELF, $ORDER_BY, $ORDER_TYPE; $sql = "SELECT ".implode(',',$SHOW)." FROM ".$this->table; if(strlen($ORDER_BY)){ $sql .= ' ORDER BY '.$ORDER_BY.' '.$ORDER_TYPE; } $db0 = new DB(); $db0->set_sql($sql); echo '<TR BGCOLOR="'.$this->dump_bg_color().'"><TD COLSPAN="3">'; echo '<TABLE WIDTH="100%">'; echo '<TR>'; for($i = 0 ; $i < sizeof($SHOW) ; ++$i){ echo '<TD><B>'.$SHOW[$i].'</B></TD>'; } echo '</TR>'."\n"; if($db0->select_query()){ while(list($key,$val) = each($db0->rows)){ echo '<TR>'; while(list($k,$v) = each($val)){ if(ereg("[A-Za-z0-9_-]*",$k) && strlen($k) > 1){ if($k == $this->tableID){ $ID = $v; } echo '<TD>'.$v.'</TD>'; } } echo '<TD NOWRAP>[ <A HREF="'.$PHP_SELF.'?update='.$ID.'">Update</A> ] [ '; echo '<A HREF="'.$PHP_SELF.'?delete='.$ID.'">Delete</A> ]</TD>'; echo '</TR>'."\n"; } }else{ echo '<TR><TD COLSPAN="'.sizeof($SHOW).'" ALIGN="center"><b>NO RECORDS!</B></TD></TR>'; } echo '</TABLE>'; echo '</TD></TR>'; } function generate_required_fields_array(){ for($i = 0 ; $i < sizeof($this->fields) ; ++$i){ $var = $this->fields[$i]; if($var['required'] == 1){ $required[] = $var['name']; } } if(is_array($required)){ $this->required = $required; }else{ $this->required = array(); } } function do_query(){ global $FUNCTIONS, $DO_NOT_INSERT, $f; for($i = 0 ; $i < sizeof($FUNCTIONS) ; ++$i){ $run_me = $FUNCTIONS[$i]; $run_me($$run_me,&$this); } if($f['button'] == 'Update!'){ while(list($key,$val) = each($this->final_sql)){ if(!empty($key) && !in_array($key,$DO_NOT_INSERT)){ $updates[] = $key.'=\''.$f[$key].'\''; } } $sql = "UPDATE ".$this->table." SET ".implode(',',$updates)." WHERE ".$this->tableID."='".$f['hiddenID']."'"; }else{ while(list($key,$val) = each($this->final_sql)){ if(!empty($key) && !in_array($key,$DO_NOT_INSERT)){ $fields[] = $key; $values[] = $f[$key]; } } $sql = "INSERT INTO ".$this->table." (".implode(',',$fields).") VALUES ('".implode('\',\'',$values)."')"; } $db0 = new DB(); $db0->set_sql($sql); $db0->alter_query(); // FOR ERRO CHECKING echo '<br>'.$sql.'<br>'; } function assign_table_info($table,$tableID){ $this->table = $table; $this->tableID = $tableID; return 1; } function dump_bg_color(){ ++$this->bg_counter; if($this->bg_counter % 2 == 0){ return $this->colorA; }else{ return $this->colorB; } } function dump_admin_top(){ $this->dump_form_start(); ?> <HTML> <HEAD> <TITLE><? echo $this->header_title; ?></TITLE> </HEAD> <BODY BGCOLOR="<? echo $this->bgcolor; ?>"> <CENTER> <TABLE WIDTH="90%" BGCOLOR="<? echo $this->bgcolor; ?>" BORDER="0" CELLSPACING="0" CELLPADDING="8"> <TR BGCOLOR="<? echo $this->header_color; ?>"> <TD COLSPAN="3"> <H1><? echo $this->header_title ?></H1> <? echo $this->header_blurb; ?><BR><BR> <CENTER><A HREF="<? echo $this->home_url; ?>"><? echo $this->home; ?></A></CENTER> </TD> </TR> <? } function dump_admin_bottom(){ echo '</TABLE></CENTER></BODY></HTML>'."\n"; } function dump_text_field(){ global $f; if($this->process && $this->errors < 1){ $this->value = ''; } echo '<INPUT TYPE="text" NAME="f['.$this->name.']" VALUE="'.$this->value.'" '; $this->dump_attrs(); echo '>'."\n"; } function dump_hidden_field(){ $foo = explode(',',$this->value); echo $foo[0].'<INPUT TYPE="hidden" NAME="f['.$this->name.']" VALUE="'.$foo[1].'">'; } function dump_select_box(){ if(is_array($this->select_info)){ echo '<SELECT NAME="f['.$this->name.']">'."\n"; while(list($key,$val) = each($this->value)){ echo '<OPTION VALUE="'.$key.'" '; if($key == $this->value && ($this->process && $this->errors < 1)){ echo ' SELECTED '; } echo '>'.$val.'</OPTION>'."\n"; } echo '</SELECT>'."\n"; }else{ $table_info = explode(',',$this->select_info); $sql = "SELECT ".$table_info[1].",".$table_info[2]." FROM ".$table_info[0]; if(strlen($table_info[3])){ $sql .= ' '.$table_info[3]; } $db0 = new DB(); $db0->set_sql($sql); if($db0->select_query()){ echo '<SELECT NAME="f['.$this->name.']">'."\n"; while(list($key,$val) = each($db0->rows)){ echo '<OPTION VALUE="'.$val[$table_info[1]].'" '; if($val[$table_info[1]] == $this->value && ($this->process && $this->errors < 1)){ echo ' SELECTED '; } echo '>'.$val[$table_info[2]].'</OPTION>'."\n"; } echo '</SELECT>'."\n"; }else{ echo 'NO RECORDS ('.$table_info[0].')'; } } } function dump_attrs(){ if(is_array($this->attrs)){ while(list($key,$val) = each($this->attrs)){ echo strtoupper($key); if(strlen($val)){ echo '="'.$val.'" '; } } } } function dump_text_area_field(){ global $f; if($this->process && $this->errors < 1){ $this->value = ''; } echo '<TEXTAREA NAME="f['.$this->name.']" '; $this->dump_attrs(); echo '>'."\n"; echo $this->value."\n"; echo '</TEXTAREA>'; } function dump_submit_button(){ $this->action = $this->value; if($this->updateID){ echo '<INPUT TYPE="hidden" NAME="f[hiddenID]" VALUE="'.$this->updateID.'">'."\n"; } echo '<center><INPUT TYPE="submit" NAME="f[button]" VALUE="'.$this->value.'"></center>'."\n"; } function dump_radio_buttons(){ global $f; $radio_deliminator = '&nbsp;&nbsp;'; $vars = explode(',',$this->value); for($i = 0 ; $i < sizeof($vars) ; ++$i){ $junk = explode(':',$vars[$i]); echo '<INPUT TYPE="radio" NAME="f['.$this->name.']" VALUE="'.$junk[1].'"'; if(($this->attrs['checked'] == 1 || $f[$this->name] == $junk[1]) && ($this->process && $this->errors < 1)){ echo ' CHECKED '; } echo '>'."\n"; echo $junk[0].$radio_deliminator; } } function dump_check_boxes(){ $check_box_deliminator = '<br>'; $vars = explode(',',$this->value); for($i = 0 ; $i < sizeof($vars) ; ++$i){ $junk = explode(':',$vars[$i]); echo '<INPUT TYPE="checkbox" NAME="f['.$this->name.'][]" VALUE="'.$junk[1].'"'; if($this->value || (is_array($f[$this->name]) && in_array($this->value,$f[$this->name]))){ echo ' CHECKED '; } echo '>'."\n"; echo $junk[0].$radio_deliminator; } } function dump_form_start(){ echo '<FORM METHOD="post" ACTION="'.$this->post.'">'."\n"; } function dump_form_end(){ if($this->process && $this->errors < 1){ $this->do_query(); } echo '</FORM>'; } function dump_field_row(){ echo '<TR BGCOLOR="'.$this->dump_bg_color().'">'."\n"; echo '<TD ALIGN="right" VALIGN="top"><STRONG>'; if($this->process && empty($this->value) && in_array($this->name,$this->required)){ echo '<FONT COLOR="red">'.$this->intro.'</FONT>'; $this->errors += 1; }else{ $this->final_sql[$this->name] = $this->value; echo $this->intro; } echo '</STRONG></TD>'."\n"; echo '<TD ALING="left" VALIGN="top">'."\n"; // submit,checkbox,text,textarea,radio,select switch($this->type){ case 'submit': $this->dump_submit_button(); break; case 'checkbox': $this->dump_check_boxes(); break; case 'text': $this->dump_text_field(); break; case 'textarea': $this->dump_text_area_field(); break; case 'radio': $this->dump_radio_buttons(); break; case 'select': $this->dump_select_box(); break; case 'hidden': $this->dump_hidden_field(); break; } echo '</TD>'."\n"; echo '<TD ALIGN="left" VALIGN="top">'.$this->help.'</TD>'."\n"; echo '</TR>'."\n"; } function dump_admin_form(){ $this->dump_admin_top(); for($i = 0 ; $i < sizeof($this->fields) ; ++$i){ $this->assign_current_field($this->fields[$i]); $this->dump_field_row(); } $this->dump_form_end(); } function assign_current_field($field){ $this->type = $field['type']; $this->attrs = $field['attrs']; $this->value = $field['value']; $this->name = $field['name']; $this->help = $field['help']; $this->intro = $field['intro']; $this->select_info = $field['select_info']; } } ?> <? include('../init.inc'); include('./admin.obj'); $TABLE = 'news_information'; $TABLE_ID = 'newsID'; $SHOW = array('newsID','newsTitle','newsDate'); $HOME_URL = './index.php'; $HOME = 'Admin Home'; $HEADER_TITLE = 'News Poster v0.0.1'; $HEADER_BLURB = 'Use this to add news posting to your homepage.'; $FUNCTIONS = array('message_topic'); $DO_NOT_INSERT = array('button'); $ORDER_BY = 'newsDate'; $ORDER_TYPE = 'DESC'; $button = 'Submit!'; if($update){ $sql = "SELECT * FROM ".$TABLE." WHERE ".$TABLE_ID."='$update'"; $db0 = new DB(); $db0->set_sql($sql); if($db0->select_query()){ $f = $db0->rows[0]; }else{ echo 'No Update'; } $button = 'Update!'; $UPDATE_ID = $update; } if($delete){ $sql = "DELETE FROM ".$TABLE." WHERE ".$TABLE_ID."='$delete'"; $db0 = new DB(); $db0->set_sql($sql); $db0->alter_query(); } if($f['button']){ $PROCESS = 1; }else{ $PROCESS = 0; } function message_topic($f,&$class){ global $f; $sql = "INSERT INTO message_topic (categoryID,topicTitle) VALUES ('".SITE_NEWS."','".$f['newsTitle']."')"; $db0 = new DB(); $db0->set_sql($sql); $db0->alter_query(); $class->final_sql['newsTopicID'] = $db0->return_insert_id(); $f['newsTopicID'] = $db0->return_insert_id(); } /* FOR ERROR CHECKING if(is_array($f)){ while(list($key,$val) = each($f)){ echo $key.'-->'.$val.'<br>'; } } */ $fields[] = array( type => 'text', required => 1, value => $f['newsTitle'], attrs => array( size => 40), name => 'newsTitle', help => 'Give your news story a title', intro => 'Title'); /* $fields[] = array( type => 'radio', value => 'Yes:1,No:0', name => 'available', help => 'If you want this category to be shown then check this box.', intro => 'Available'); */ $fields[] = array( type => 'select', value => $f['newsCategory'], select_info => 'news_categories,categoryID,categoryName', name => 'newsCategory', help => 'This is the category which your news falls under', intro => 'Category'); $fields[] = array( type => 'hidden', required => 1, value => date("Y-m-d H:i:s").','.date("Y-m-d H:i:s"), name => 'newsDate', help => 'Date posted', intro => 'Date'); $fields[] = array( type => 'select', value => $f['newsAuthor'], select_info => 'message_users,userID,userName,WHERE userAdminFlag=1', name => 'newsAuthor', help => 'Who\'s writing this anyways?', intro => 'Author'); $fields[] = array( type => 'textarea', attrs => array( cols => 40, rows => 6, wrap => 'virtual'), value => $f['newsText'], name => newsText, help => 'So what is this news?', intro => 'Text'); $fields[] = array( type => 'submit', required => 1, value => $button, help => '&nbsp;', intro => '&nbsp;'); // values for functions (same name as functions) $message_topic = $f; $admin = new AdminForm($fields); $admin->DumpForm(); ?>
« previous php.general (#26467) next »