langenf Posted September 10, 2008 Share Posted September 10, 2008 Hi, the idea behind this class is to make a simple form inside a html table. It is working when i test make_form as a function but not working when i put it inside a class. Error message it gives is - Warning: Invalid argument supplied for foreach(). Please say what am i doing wrong here? $form_elements=array("firma","vorname","nachname","strasse","plz","ort","land","url","email","fone","fax","mobile"); class form { private $input_name,$input_type,$form_name,$input_title,$form_title,$form_elements; function make_form($form_title,$form_name,$action,$form_elements_array){ echo "<form name=$form_name action=$action>\n"; echo "<table><tr><td><h3>$form_title</h3></td></tr>\n"; foreach($this->form_elements_array as $val){ $element=$val."_form"; echo "<tr><td>$val</td><td><input name=$val</td></tr>\n"; } echo "<tr><td colspan=2><input type='submit'></td></tr>\n"; echo "</table></form>\n"; } } $f= new form(); $f-> make_form("enter client information:","form_cl","somewhere.php",$form_elements); Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/ Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 You are not setting the class variables and referencing them with $this. You can either add a constructor method or remove $this in your make_form method so: option 1 public function __ construct($form_title,$form_name,$action,$form_elements_array){ $this->form_title = $form_title; // rest of vars } or in you make_form method change the foreach to foreach($form_elements_array as $val){ } You do not need the member variables if you use option 2 so the following can be removed: private $input_name,$input_type,$form_name,$input_title,$form_title,$form_elements; Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638213 Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 Also the way you are tackling this there is no requirement for a class. A general function would be more efficient on its own function make_form($form_title,$form_name,$action,$form_elements_array){ echo "<form name=$form_name action=$action>\n"; echo "<table><tr><td><h3>$form_title</h3></td></tr>\n"; foreach($form_elements_array as $val){ $element=$val."_form"; echo "<tr><td>$val</td><td><input name=$val</td></tr>\n"; } echo "<tr><td colspan=2><input type='submit'></td></tr>\n"; echo "</table></form>\n"; } will do Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638217 Share on other sites More sharing options...
langenf Posted September 10, 2008 Author Share Posted September 10, 2008 thanks, i decided to go with your first suggestion - making a constructor, since im making OO blocks that can be reused . However, it gives 5 errors Warning: Missing argument 1 for form::__construct(), called in W:\www\liz\form.class.php for each of the 4 arguments and this error too Warning: Invalid argument supplied for foreach() in W:\www\liz\form.class.php class form { public $input_name,$input_type,$form_name,$input_title,$form_title,$form_elements,$action, $form_elements_array; public function __construct($form_title, $form_name, $action, $form_elements_array){ $this->form_title = $form_title; $this->form_name = $form_name; $this->action = $action; $this->form_elements_array = $form_elements_array; } function make_form($form_title,$form_name,$action,$form_elements_array){ echo "<form name=$form_name action=$action>\n"; echo "<table><tr><td><h3>$form_title</h3></td></tr>\n"; foreach($this->form_elements_array as $val){ $element=$val."_form"; if(stristr($val,"id")){ echo "<tr><td>id: </td><td>$val</td></tr>\n"; } else{ echo "<tr><td>$val</td><td><input name=$val</td></tr>\n"; } } echo "<tr><td colspan=2><input type='submit'></td></tr>\n"; echo "</table></form>\n"; } } $form_elements=array("firma","vorname","nachname","strasse","plz","ort","land","url","email","fone","fax","mobile"); $f= new form(); $f-> make_form("enter client information:","form_cl","somewhere.php",$form_elements); Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638238 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 you haven't passed the required arguments for your constructor $f= new form(); // missing args for constructor: //public function __construct($form_title, $form_name, $action, $form_elements_array){ Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638257 Share on other sites More sharing options...
langenf Posted September 10, 2008 Author Share Posted September 10, 2008 thanks again, its working as expected and i removed the parameters of the function $f= new form("enter client information:","form_cl","somewhere.php",$form_elements); $f->make_form(); as you can see, iam quite new to oop..could you pls suggest the better option between the 2: sending parameters to a function inside the object? or sending it to constructor? Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638275 Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 If you have more methods in the class that use the same variables then set them in the constructor so you dont have to keep passing them into class methods Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638280 Share on other sites More sharing options...
BlueSkyIS Posted September 10, 2008 Share Posted September 10, 2008 if the parameters will not change during the life of the object, i'd send them to constructor so i can use the same object more than once. if the parameters will change, i would send them to the function so i can use the same object more than once. Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638281 Share on other sites More sharing options...
langenf Posted September 10, 2008 Author Share Posted September 10, 2008 thank you very much again. I dont understand what constructor does by $this->action = $action;.since $action is already defined, why define it constructor again?! Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638370 Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 Because a class constructor is a magic method that is automatically called when you create an instance of a class (an object). You do not need to call it like regular methods so it can set the class variables straight away for use by any of the class methods. Example class dog { private $name; public function __construct($name) { $this->name = $name; } public function getDogName() { return "The dogs name is ".$this->name; } } $myDog = new dog("Rover"); print $myDog->getDogName(); Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-638407 Share on other sites More sharing options...
langenf Posted September 11, 2008 Author Share Posted September 11, 2008 thanks for the illustration. The forms would be generated based on the database table. can you please say how to make a drop downmenu based on the enum values in the mysql table? <?php include('db2.php'); class form { public $input_name,$input_type,$form_name,$input_title,$form_title,$form_elements,$action, $form_elements_array; public function __construct($form_title, $form_name, $action, $form_elements_array){ $this->form_title = $form_title; $this->form_name = $form_name; $this->action = $action; $this->form_elements_array = $form_elements_array; } function make_form(){ echo "<form name=$this->form_name action=$this->action>\n"; echo "<table><tr><td><h3>$this->form_title</h3></td></tr>\n"; foreach($this->form_elements_array as $key=>$val){ $element=$val."_form"; if(stristr($key,"id")){ echo "<tr><td>id: </td><td>$key</td></tr>\n"; } else{ if(stristr($val,"enum")) { echo "<tr><td>$key</td><td><select name=$val></select></td></tr>\n"; } else{ echo "<tr><td>$key</td><td><input name=$val></td></tr>\n"; } } } echo "<tr><td colspan=2><input type='submit'></td></tr>\n"; echo "</table></form>\n"; } } //usage after connecting to database $query = "describe testtable"; $result = $myDB->query($query); if($result) { //while($row = mysql_fetch_assoc($result)) while($row =mysql_fetch_array($result, MYSQL_NUM)) { echo "#{$row[0]} {$row[1]}#<br />"; $fields_array[$row[0]] = $row[1];//associative array with field as key and datatype as member } //$row =mysql_fetch_array($result, MYSQL_NUM); } else { die("norecords from database <br />"); } global $str; echo $str; //end usage $f= new form("enter client information:","form_cl","somewhere.php",$fields_array); $f->make_form(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/#findComment-639015 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.