To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 10-31-2009, 04:13 AM   #1
Neville1985
@bored
 
Neville1985's Avatar
 
Join Date: Oct 2009
Location: Wolfcreek
Posts: 58
createForms

PHP Code:
<?php

class createforms {
    
    
//Vars
    
private $error;
    
    
//Start Form, output->$form_array
    
public function startForm ( $name , $method , $action , $css_form = null, $enabled = false) {
        
$this    ->    css_form    =    (string)    $css_form;
        
$this    ->    form_name    =    (string)    $name;
        
$this    ->    method        =    (string)    $method;
        
$this    ->     enabled        =    (bool)        $enabled;
        
$this    ->    form_array['{$this->form_name}'][]    =    "<form ".
                                                            
"name=\"{$this->form_name}\" ".
                                                            
"method=\"{$method}\" ".
                                                            
"action=\"{$action}\" />\n".
                                                            
"<centre>\n ".
                                                            
"<table name=\"{$this->form_name}\" class=\"{$this->css_form}\">\n";
    }
    
    
//Add form element, flow controller executes correct function
    
public function addType ( $type , $name , $title , $data = '' , $view = '' ) {
        
$this    ->    name        =    ( $name        == '' )            ?    (bool)    false        :    (string)    str_replace(" ", "", strtolower($name));//Strip White-space; Force String & Lower-case;
        
$this    ->    type        =     ( $type        == '' )            ?    (bool)    false        :    (string)    str_replace(" ", "", strtolower($type));//Strip White-space; Force String & Lower-case;
        
$this    ->    title        =    ( $title    == '' )            ?    (bool)    false        :    (string)    ucfirst($title);                        //Force String & Upper-case First-Letter;
        
$this    ->    view        =    ( !is_array($view) )        ?    (bool)    false        :    (array)    $view;                                        //True: Force false; False: Force Array;
        
$this    ->    data        =    ( !is_array($data) )        ?    (string)    $data    :    (array)    $data;                                      //True: Force '' String; False: Force Array;
        
        
static $count = 0;
        
        if (
$this->type && $this->name && $this->title ) {
            
$count++;
            switch (
$this->type ) {
                
                
//Text / Password
                
case 'text':
                case
'password':
                    (
$this->set_TextPass() )    
                    ?
                        
null
                    
:
                        
$this->error[]    =    (string)    "Inner Element {$count} Failed\n"    
                    
;
                    break;
                            
                
//Radio / Checkbox
                
case 'radio':
                case
'checkbox':
                    (
$this->set_Optionals() )
                    ?
                        
null
                    
:
                        
$this->error[]    =    (string)    "Inner Element {$count} Failed\n"
                    
;
                    break;
                
                
//TextArea    
                
case 'textarea':
                    (
$this->set_TextArea() )
                    ?
                        
null
                    
:
                        
$this->error[]    =    (string)    "Inner Element {$count} Failed\n"
                    
;
                    break;
                
                
//All Buttons
                
case 'file':
                case
'reset':
                case
'submit':
                case
'button':
                case
'picture':
                    (
$this->set_Buttons() )
                    ?
                        
null
                    
:
                        
$this->error[]    =    (string)    "Inner Element {$count} Failed\n"
                    
;
                    break;

                
//Select Menu
                
case 'dropdown':
                    (
$this->set_DropDown() )
                    ?
                        
null
                    
:
                        
$this->error[]    =    (string)    "Inner Element {$count} Failed\n"
                    
;
                    break;
                
                
//defaulted: pass error.
                
default:
                    
$this->error[]    =    (string)    "Control Element Failed";
                    break;
            }    
        } else {
            
$this->error[]    =    (string)    "Main Element Failed";
        }
    }
    
    
//Execute Form: True Display Erros, False Display Form
    
public function execForm() {
        
$this->isEnabled();
        if (
$this->countErrors() ) {
            print(
"
                    <div "
                    
."id=\"{$this->css_form}Error\" "
                    
."class=\"border\" />\n "
                    
."Sorry! Unable To Display Form"
                    
."<br>\nErrors Found: <br>\n"
                    
."{$this->displayErrors( '<br>' )}"
                    
."</div>\n"
            
);
        } else {
            print(
$this->buildform() );
        }                                    
    }
    
    
//Build form from data array
    
private function buildForm() {
        
$this->form_array['{$this->form_name}'][]    =    (string)    "</center>\n"
                                                                    
."</form>\n";
        
$display_block = "";
        foreach (
$this->form_array['{$this->form_name}'] as $key => $value ) {
            
$display_block    .=    "{$value}";    
        }
        return
$display_block;
    }
    
    
//Form Enabled?
    
private function isEnabled() {
        (
$this->enabled == false  )    ?    $this->error[]    =    (string)    "--- FORM DISABLED --- \n":    null;
    }
    
    
//Confirm Errors Exist
    
private function countErrors() {
        if (
count($this->error) > 0 ) {
            return
true;
        }
    }
    
    
//Display Collected Errors with a spacer
    
private function displayErrors( $spacer = ' ' ) {
        return
implode( $spacer, $this->error );
    }
    
    
//Create TextBox
    
private function set_TextPass() {
        if (
$this->name && $this->title ) {
            
$this->form_array['{$this->form_name}'][]    =    (string)    "<tr>\n"
                                                                        
."<td>"
                                                                        
."{$this->title}:"
                                                                        
."</td>\n"
                                                                        
."<td>"
                                                                        
."<input "
                                                                        
."type=\"{$this->type}\" "
                                                                        
."name=\"{$this->form_name}_{$this->name}\" "
                                                                        
."title=\"{$this->title}\" "
                                                                        
."size=\"{$this->view['width']}\" "
                                                                        
."maxlength=\"{$this->view['maxlength']}\" "
                                                                        
."value=\"{$this->data}\" />"
                                                                        
."</td>\n"
                                                                        
."</tr>\n";
            return
true;
        }
    }
    
    
//Create 'optionals' (check / radio)
    
private function set_Optionals() {
        
$count    =    0;    
        
$span    =    count( $this->data );
            
        if (
is_array( $this->data ) ) {
            
            
$this->form_array['{$this->form_name}'][]            =    (string)    "<td "
                                                                                
."colspan=\"2\"> \n"
                                                                                
."<table "
                                                                                
."class=\"{$this->css_form}\"> \n"
                                                                                
."<tr>\n"
                                                                                
."<td colspan=\"{$span}\">\n"
                                                                                
."<b> "
                                                                                
."{$this->title} "
                                                                                 
."</b>\n"
                                                                                
."</td>\n"
                                                                                
."</tr>\n";                                                    
            
            do {
                
                
$checked    =    ( array_key_exists( 'checked', $this->data[$count] ) && $this->data[$count]['checked'] == true ) ? "checked=\"checked\"" : null;
                
$is_cbox    =    ( $this->type == 'checkbox' )    ?    "[]"    :    null;
                
$this->form_array['{$this->form_name}'][]        =    (string)    "<td> \n"
                                                                                
."<i> "
                                                                                
."{$this->data[$count]['title']} "
                                                                                
."</i> "
                                                                                
."<br> \n"
                                                                                
."<input "
                                                                                
."type=\"{$this->type}\" "
                                                                                
."name=\"{$this->form_name}_{$this->name}{$is_cbox}\" "
                                                                                
."title=\"{$this->data[$count]['title']}\" "
                                                                                
."value=\"{$this->data[$count]['value']}\" {$checked}/>\n"
                                                                                
."</td> \n";
                
$count++;
                
            } while (
array_key_exists( $count, $this->data ) );
            
            
$this->form_array['{$this->form_name}'][]            =    (string)    "</tr>\n"
                                                                                
."</table> \n"
                                                                                
."</td> \n"
                                                                                
."</tr> \n";
            return
true;
            
        }
        
        
    }
    
    
//Create TextArea
    
private function set_TextArea() {
        
$this->form_array['{$this->form_name}'][]                    =    (string)    "<tr> \n"
                                                                                    
."<td>\n"
                                                                                    
."<b>"
                                                                                    
."{$this->title}: "
                                                                                    
."</b>"
                                                                                    
."</td>\n"
                                                                                    
."<td>\n"
                                                                                    
."<textarea "
                                                                                    
."name=\"{$this->form_name}_{$this->name}\" "
                                                                                    
."title=\"{$this->title}\" />\n"
                                                                                    
."{$this->data}"
                                                                                    
."</textarea>\n"
                                                                                    
."</td>\n"
                                                                                    
."</tr>\n";
        return
true;
    }
    
    
//Create All Buttons
    
private function set_Buttons()    {
        
$this->form_array['{$this->form_name}'][]                    =    (string)    "<tr> \n"
                                                                                    
."<td>\n"
                                                                                    
."<input "
                                                                                    
."type=\"{$this->type}\" "
                                                                                    
."name=\"{$this->form_name}_{$this->name}\" "
                                                                                    
."value=\"{$this->title}\" />\n"
                                                                                    
."</td>\n"
                                                                                    
."</tr>\n";
        return
true;
    }

    
    
//Create SelectMenu
    
private function set_DropDown() {
        if (
is_array( $this->data ) && count( $this->data ) > 0 ) {
            
$drop_block                                   =    (string)    "<tr>\n"
                                                                        
."<td>\n"
                                                                        
."<select "
                                                                        
."name=\"{$this->form_name}_{$this->name}\" "
                                                                        
."title=\"{$this->title}\"/>\n".
                                                                        
"<option "
                                                                        
."value=\"\"/>"
                                                                        
."{$this->title}: "
                                                                        
."Select One"
                                                                        
."</option>\n";
            foreach (
$this->data as $key => $value ) {
                
$option_value                             =    (string)    str_replace( " ", "_", $key );
                
$drop_block                             .=    (string)    "<option "
                                                                        
."value=\"{$option_value}\"/>"
                                                                        
."{$value}"
                                                                        
."</option>\n";
            }
            
$drop_block                                    .=    (string)    "</select>\n"
                                                                        
."</td>\n"
                                                                        
."</tr>\n";
            
$this->form_array['{$this->form_name}'][]     =    (string)    $drop_block;
            return
true;
        }
    }
    
}
?>
Neville1985 is offline   Reply With Quote
Old 10-31-2009, 04:14 AM   #2
Neville1985
@bored
 
Neville1985's Avatar
 
Join Date: Oct 2009
Location: Wolfcreek
Posts: 58
This is what I have been working on lately. Just off the bat, I realise there is a pear extension like this already, although I want to create my own. Layout may appear a little off.

Things I haven't completed yet.

- rules system
- data validation prior to form creation (creation data itself [needed ?])
- some conditionals (still works though)

Controlling the object.

Create Object:
new createforms();

Start form:
startForm('name', 'method', 'action', 'css_class', enabled?);

Add Element Type: [view_array optional with all types]
addType('element_type', 'element_name', 'element_title^', data_array/data_string, view_array);

Build and Display form:
execForm();

Element Types (example of use below):

- text, password
-data_string [optional]

- checkbox/radio
- data_array [required]

- textarea
- data_string [optional]

- submit, reset, file, button [picture not functional at this point]

- dropdown (select menu)
- data_array (required)

Example:
PHP Code:
<html>
<head>
<link rel="stylesheet" type="text/css" href="css.forms.css" />
</head>
<body>
<?php
$radio_1
= array(
                    
"title"        =>    "test1",
                    
"value"        =>    "test_1"
                
);
$radio_2 = array(
                    
"title"        =>    "test2",
                    
"value"        =>    "test_2"
                
);
$radio_3 = array(
                    
"title"        =>    "test3",
                    
"value"        =>    "test_3"
                
);
$radio_4 = array(
                    
"title"        =>    "test4",
                    
"value"        =>    "test_4"
                
);
$radio_5 = array(
                    
"title"        =>    "test5",
                    
"value"        =>    "test_5"
                
);

$data_array = array($radio_1, $radio_2, $radio_3, $radio_4, $radio_5);
$login = new createforms();
$login->startForm('test', 'post', ' ', 'Right_Menu', true);
$login->addType('text', 'username', 'username');
$login->addType('password', 'blah', 'password');
$login->addType('checkbox', 'radio_1', 'User Type', $data_array);
$login->addType('radio', 'radio_1', 'User Type', $data_array);
$login->addType('textarea', 'testbox', 'blkaj');
$login->addType('submit', 'submit', 'buttontest');
$login->addType('dropdown', 'tester', 'tester', array("option1" => "Eat Me", "option2" => "Eat Them"));
$login->execForm();
?>
</body>
</html>
Just wondering on peoples views on this, what should change, how/if I need to handle things differently, suggestions are always welcome.

Thanks in advance.

Last edited by Neville1985; 10-31-2009 at 04:48 AM.
Neville1985 is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:33 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.