Jump to content

Recommended Posts

When I execute my create_form in my Form class it omits the first value of my $list array. It should print a select box with the options 1,2,3. Thanks for any help you can give.

 

<?php
#Form Class 6/20/08
#Last Edit


class Form 
{
var $data;
var $input=array();


public function Form ($name, $id, $method, $action) 
{
	$this->data['name']= $name;
	$this->data['id']= $id;
	$this->data['method']= $method;
	$this->data['action']= $action;
}

public function add_Input($label, $name, $id, $type, $value) 
{
	$this->input[]= new Text($label, $name, $id, $type, $value);
}

public function add_Select($label, $name, $id, $option, $default)
{
	$this->input[]= new Select($label, $name, $id, $option, $default);
}

public function create_Form() 
{

	foreach($this->input as $value){

		if(get_class($value) == "Select"){
			echo "<label>$value->label</label><select name='$value->name'
			id='$value->id'";
			foreach($value->option as $option){
				echo "<option value='$option'";
				if($value->default == $option){
					echo "selected='selected'";
				}
				echo ">$option</option>";
			}
			echo "</select>";
		}

		if(get_class($value) == "Text"){

			echo "<label>$value->label</label><input type='$value->type'
			id='$value->id name='$value->name' value='$value->value'>";
		}
	}
}
}

class Text extends Form
{
var $label;
var $name;
var $id;
var $type;
var $value;

public function Text($label, $name, $id, $type, $value)
{
	$this->label= $label;
	$this->name= $name;
	$this->id= $id;
	$this->type= $type;
	$this->value= $value;

}
}

class Select extends Form
{
var $label;
var $name;
var $id;
var $option= array();
var $default;


public function Select($label, $name, $id, $option, $default)
{
	$this->label= $label;
	$this->name= $name;
	$this->id= $id;
	$this->option= $option;
	$this->default= $default;
}
}
$list= array(1,2,3);


$registration_form= new Form("Name", "ID", "post", $_SERVER['PHP_SELF']);
$registration_form->add_Input("Name", "name", "ID", "text","Name");
$registration_form->add_Input("Age", "age", "ID", "text","Age");
$registration_form->add_Select("Select", "test", "ID", $list, 1);
$registration_form->create_Form();

?>

Link to comment
https://forums.phpfreaks.com/topic/111390-solved-my-array-omits-the-first-value/
Share on other sites

So is this actually solved?  =/

 

Anyway, why are you using old constructors and no visibility keywords? =/  Also, you shouldn't have the Text, Select, etc. classes extend Form.  You should actually have an Element class which can be extended by those form elements.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.