Jump to content

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/123580-making-a-simple-forms-dynamically/
Share on other sites

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;

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

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);

 

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?

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.

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();

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();



?>



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.