Reputation: 272
Hopefully somebody can help with this problem
My objective: to create a class that creates HTML forms.
$cont->individual_fields("text", "feedName", "feed-name", null, "The name of the feed:", "200", "YES");
The above uses the individual_fields() method to add values for the various HTML attributes to be found in a text input field.
The method:
public function individual_fields($type, $name, $id, $class = null, $desc, $maxlength = null, $value = null, $select = null) { }
Within the method I create a multidimensional array:
$fields_essentials = array(
'type' => $type,
'name' => $name,
'id' => $id,
'desc' => $desc,
'class' => $class,
'maxlength' => $maxlength,
'value' => $value,
'select' => $select);
So I now have a multidimensional array with attribute labels and values. Great.
Now I use a foreach to loop through array creating the HTML form field.
So it ends up with:
$text = '<tr>';
$text .= "<th><label for=\"$id\">$desc</label></th>";
$text .= '<td><'.$form.' ></td>';
$text .= '</tr>';
return $text;
The HTML is as follows:
<label for="feed-name">The name of the feed:</label>
<input type="text" value="" maxlength="200" class="regular-text code " id="feed-name"name="affiliate_hoover_plugin_options[feedName]"/>
All super smashing so far.
Now the problem I have is taking the returned results of the individual_fields() method and placing it into another method called create_form():
echo $cont->create_form("post", "#action", "optionForm", "option-form", "multipart/form-data");
public function create_form($method, $action = null, $name = null, $id = null, $enctype = null) {
extract($this->config_settings());
$form = null;
$form .= "<form method=\"$method\" ";
($action !== null) ? $form .= " action=\"$action\"" : null;
($name !== null) ? $form .= " name=\"$name\"" : null;
($id !== null) ? $form .= " id=\"$id\"" : null;
($enctype !== null) ? $form .= " enctype=\"$enctype\"" : null;
$form .= ">";
$form .= '<fieldset>';
$form .= "<legend>$page_title</legend>";
$form .= '<table class="form-table">';
$form .= '<tbody>';
$form .= '</tbody>';
$form .= '</table>';
// input fields here
// INDIVIDUAL FIELDS FROM individual_fields() method here
// permenant settings for every form
$form .= $this->perm_fields();
$form .= '<input type="submit" id="submit" name="submitForm" class="button-primary" value="Save Changes">';
$form .= '</fieldset>';
$form .= '</form>';
return $form;
}
How do I save the returned values from the individual_fields() method and use it in the create_form() method?
I've tried so many different approaches and spent a hell of a lot of time on this issue that I wonder if the problem lies in how I am structuring my class methods rather than an issue of scope.
If you have read this post from top to bottom, I thank you
Edit:
Reading the responses below and thinking about it a bit more I may just add the field details as any array to the create_form function so it looks like this:
$cont->create_form("post", "#action", "optionForm", "option-form", "multipart/form-data", array("text", "feedName", "feed-name", null, "The name of the feed:", "200", "YES"), array("text", "urlName", "url-name", null, "The URL of the feed:", "300", "YES"));
It's a bit more of a complex lump of code that I wanted though. It would have been nice to be able to separate it.
Upvotes: 1
Views: 77
Reputation: 21007
You may create function (in $cont
) like this:
protected $fields = array();
function push_field( $field){
$this->fields[] = $field;
}
// And usage:
$cont->push_field( $text);
// In create form:
foreach( $this->fields as $field){
echo $field . "\n";
}
However I'd create class like this:
class Field {
$attrbites = array(); // HTML attributes
$properties = array(); // Like field title or so
public function __construct($type, $name, $id, $class = null, $desc, $maxlength = null, $value = null, $select = null) { }
public function getHtml(){ ...
return $text;
}
}
And generate all html just when displaying form, than you could add validation to Field
, add Decorators and manipulation with them would be easier.
Upvotes: 1