View Single Post
Old 03-14-2005, 08:27 AM   #13 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
ok, i spent a little time and wrote a couple functions to dynamically draw forms from a PHP array. it was done quickly more for demonstration purposes, .. so you can probably modify it to your needs. check it out. http://codenewbie.com/test.php
PHP Code:
<?
function getFormHTML($form,$table,$fields){
  
$html .= "<form";
  foreach(
$form as $key => $value){
    
$html .= " ".$key."=\"".$value."\"";
  }
  
$html .= ">\n";
  
  if(
is_array($fields)){
    
    
$html .= "<table";
    foreach(
$table as $key => $value){
      
$html .= " ".$key."=\"".$value."\"";
    }
    
$html .= ">\n";
   
   foreach(
$fields as $field){
      
$html .= "<tr><td align=\"right\" valign=\"top\"><b>".$field['title']."</b></td><td valign=\"top\">";
      
$html .= getInputHTML($field);
      
$html .= "</td></tr>\n";
    }
    
    
$html .= "</table>\n";
  }
  
  
$html .= "</form>\n";
  
  return 
$html;
}

function 
getInputHTML($field){
  switch(
$field['type']){
    
    case 
"textarea":
      
$html .= "<textarea";
      foreach(
$field as $key => $value){
        if(
$key!="value")
          
$html .= " ".$key."=\"".$value."\"";
      }
      
$html .= ">".$field['value']."</textarea>\n";
    break;
    
    case 
"radio":
      foreach(
$field['options'] as $key => $value){
        
$html .= "<input type=\"radio\" name=\"".$field['name']."\" value=\"".$key."\" ";
        
$html .= (($field['value']==$key)?"checked":"")."> ".$value." &nbsp;";
      }
    break;
    
    case 
"checkbox":
      foreach(
$field['options'] as $key => $value){
        
$html .= "<input type=\"checkbox\" name=\"".$field['name']."\" value=\"".$key."\" ";
        
$html .= (( in_array($key,$field['values']) )?"checked":"")."> ".$value." &nbsp;";
      }
    break;
    
    case 
"select":
      
$html .= "<select".(($field['multiple'])?" MULTIPLE":"");
      foreach(
$field as $key => $value){
        if(
$key!="values" && $key!="options" && $key!="multiple")
          
$html .= " ".$key."=\"".$value."\"";
      }
      
      foreach(
$field['options'] as $key => $value){
        
$html .= "<option value=\"".$key."\"";
        
$html .= (( in_array($key,$field['values']) )?" selected":"")."> ".$value."</option>\n";
      }
      
      
$html .= "</select>\n";
    break;
    
    default:
      
$html .= "<input";
      foreach(
$field as $key => $value){
        
$html .= " ".$key."=\"".$value."\"";
      }
      
$html .= ">";
      break;
  }
  return 
$html;
}

$form['name']="my_form";
$form['method']="POST";
$form['action']="somepage.php";

$table['border']="0";
$table['cellspacing']="1";
$table['cellpadding']="2";
$table['style']="border-style:solid;border-width:1px;font-family:arial;font-size:9pt;";

$fields[0]['name']="username";
$fields[0]['type']="text";
$fields[0]['value']="sde";
$fields[0]['size']="35";
$fields[0]['maxlength']="35";
$fields[0]['title']="User Name";

$fields[1]['name']="email";
$fields[1]['type']="text";
$fields[1]['value']="myemail@somedomain.com";
$fields[1]['size']="35";
$fields[1]['maxlength']="64";
$fields[1]['title']="Email Address";

$fields[2]['name']="gender";
$fields[2]['type']="radio";
$fields[2]['options'] = array("M"=>"Male","F"=>"Female");
$fields[2]['value']="M";
$fields[2]['title']="Gender";

$fields[2]['name']="languages";
$fields[2]['type']="checkbox";
$fields[2]['options'] = array("html"=>"HTML","javascript"=>"Java Script","php"=>"PHP","cpp"=>"C++");
$fields[2]['values'] = array("html","javascript","php");
$fields[2]['title']="Languages";

$fields[3]['name']="favorite_sites";
$fields[3]['type']="select";
$fields[3]['multiple']=true;
$fields[3]['size']=4;
$fields[3]['options'] = array("google.com"=>"google.com","codenewbie.com"=>"codenewbie.com","cnn.com"=>"cnn.com","php.net"=>"php.net");
$fields[3]['values'] = array("codenewbie.com","php.net");
$fields[3]['title']="Favorite Sites";

$fields[4]['name']="favorite_car";
$fields[4]['type']="select";
$fields[4]['options'] = array("nissan"=>"Nissan","toyota"=>"Toyota","subaru"=>"Subaru");
$fields[4]['values'] = array("subaru");
$fields[4]['title']="Favorite Car Company";

$fields[5]['name']="bio";
$fields[5]['type']="textarea";
$fields[5]['value']="This is my bio. I like to work with PHP.";
$fields[5]['rows']="5";
$fields[5]['cols']="55";
$fields[5]['maxlength']="64";
$fields[5]['title']="Biography";


echo 
getFormHTML($form,$table,$fields);
?>
__________________
Mike
sde is offline   Reply With Quote