method = $method; $this->action = $action; $this->name = $name; $this->class = $class; $elements = array(); $this->getStatus = 0; } /* add new element ** purpose: adds an entity to the container */ function addElement( $element ) { $element['id'] = $this->nextElementID(); $location = $element['id']; $this->elements[$location] = $element; return $element['id']; } /* update element ** purpose: updates an existing form element */ function updateElement( $updatedElement ) { $id = $updatedElement['id']; $this->elements[$id] = $updatedElement; } function nextElementID() { return count( $this->elements ); } function getElement($id) { return $this->elements[$id]; } function numElements() { return count( $this->elements ); } /* get the next element ** purpose: each time this is called it gets the next element until the end is reached */ function getNext() { if( $this->getStatus == $this->numElements() ) { // is this the end of the elements? $this->getStatus = 0; // reset the sentinal return false; // indicate ending } $element = $this->getElement( $this->getStatus ); $this->getStatus++; return $element; } /* add a select box entity to the form ** purpose: adds a select box to the container */ function addSelect($name,$description) { $element['type'] = 'select'; $element['name'] = $name; $element['selections'] = array(); $element['description'] = $description; $element['beginCode'] = ""; return $this->addElement($element); } /* add item to a select box instance ** purpose: adds an option to a select box entity */ function addSelect_item($id,$val,$description) { // get the element from the array of elements $element = $this->getElement($id); if( $element['type'] != 'select' ) { return false; } $selections = $element['selections']; $location = count( $selections ); $selections[$location]['value'] = $val; $selections[$location]['description'] = $description; $selections[$location]['code'] = "\t\t\n"; $element['selections'] = $selections; $this->updateElement($element); } function getSelectItems( $element ) { if( $element['type'] != 'select' ) { return false; } $selections = $element['selections']; for( $i = 0; $i < count( $selections ); $i++ ) { $outp .= $selections[$i]['code']; } return $outp; } /* add a text box ** purpose: adds a text box to the container */ function addTextBox($name,$value,$description) { $element['type'] = 'text'; $element['name'] = $name; $element['value'] = $value; $element['description'] = $description; $element['code'] = ""; return $this->addElement($element); } /* add a check box ** purpose: adds a check box to the container */ function addCheckBox($name,$value,$description) { $element['type'] = 'checkbox'; $element['name'] = $name; $element['value'] = $value; $element['description'] = $description; $element['code'] = ""; return $this->addElement($element); } /* add a radio button ** purpose: adds a radio button to the container */ function addRadioButton($name,$value,$description) { $element['type'] = 'radio'; $element['name'] = $name; $element['value'] = $value; $element['description'] = $description; $element['code'] = ""; return $this->addElement($element); } /* add a textarea ** purpose: adds a textarea to the container */ function addTextArea($name,$value,$description) { $element['type'] = 'textarea'; $element['name'] = $name; $element['value'] = $value; $element['description'] = $description; $element['code'] = ""; return $this->addElement($element); } /* add a submit button ** purpose: add a submit button to the container */ function addSubmitButton($name,$value) { $element['type'] = 'submit'; $element['name'] = $name; $element['value'] = $value; $element['code'] = ""; return $this->addElement($element); } /* add a hidden field ** purpose: add a hidden field to the container */ function addHiddenField($name,$value) { $element['type'] = 'hidden'; $element['name'] = $name; $element['value'] = $value; $element['code'] = ""; return $this->addElement($element); } /* get HTML output as a table ** purpose: returns the HTML data from the container with the descriptions on the left and the form elements on the right */ function getTableOfElements_1() { $outp .= "
\n"; $outp .= "\n"; $element = $this->getNext(); while( $element ) { $outp .= "\t"; switch($element['type']) { case 'select': $selectItems = $this->getSelectItems( $element ); $outp .= "" . "\n\t\n" . "\t\n\t"; break; case 'text': $outp .= ""; break; case 'checkbox': $outp .= ""; break; case 'radio': $outp .= ""; break; case 'textarea': $outp .= ""; break; case 'submit': $outp .= ""; break; case 'hidden': $outp .= $element['code']; } $outp .= "\n"; $element = $this->getNext(); } $outp .= "
{$element['description']}\n" . "\t" . $element['beginCode'] . "\n" . $selectItems . "\t" . $element['endCode'] . "\n" . "\t
{$element['description']}{$element['code']}
{$element['description']}{$element['code']}
{$element['description']}{$element['code']}
{$element['description']}{$element['code']}
 {$element['code']}
\n"; $outp .= "
\n"; return $outp; } /* get HTML output as a table ** purpose: returns the HTML data from the container with the descriptions on the right and the form elements on the left */ function getTableOfElements_2() { $outp .= "
\n"; $outp .= "\n"; $element = $this->getNext(); while( $element ) { $outp .= "\t"; switch($element['type']) { case 'select': $selectItems = $this->getSelectItems( $element ); $outp .= "\n" . "\t\n" . "\t\n" . "\t"; break; case 'text': $outp .= ""; break; case 'checkbox': $outp .= ""; break; case 'radio': $outp .= ""; break; case 'textarea': $outp .= ""; break; case 'submit': $outp .= ""; break; } $outp .= "\n"; $element = $this->getNext(); } $outp .= "
\n" . "\t" . $element['beginCode'] . "\n" . $selectItems . "\t" . $element['endCode'] . "\n" . "\t{$element['description']}
{$element['code']}{$element['description']}
{$element['code']}{$element['description']}
{$element['code']}{$element['description']}
{$element['code']}{$element['description']}
{$element['code']} 
\n"; $outp .= "
\n"; return $outp; } } /** test code $form = new form_HTML('post','index.php','mainBody','formClass'); $selectId = $form->addSelect('select test','description of the select box'); $form->addSelect_item($selectId,'item1val','item1description'); $form->addSelect_item($selectId,'item2val','item2description'); $form->addTextBox('text_test','text_value_test','description of the text box'); $form->addCheckBox('cb_name','cb_value','cb_description'); $form->addRadioButton('rb_name','rb_value','rb_description'); $form->addTextArea('ta_name','ta_value','description of the textarea'); echo $form->getTableOfElements_1(); echo $form->getTableOfElements_2(); /**/ ?>