Here's an OOP function from an HTML class i wrote to simplify forms
myself -- It might come in handy for ya later :-)

// Single Select dropdown box
/*
  @params
   $name  String Name of this form element
   $items  Array Array of items to place in this select box
 $selected String Single item to select

Example params are:

  $name = "form[xyz]";
  $items = array(
    "one" => "First element",
  "two"   => "Second element",
  "three" => "Third thingy");
  $selected = "two";

Output HTML will be:
  <SELECT MULTIPLE NAME="form[xyz]">
 <OPTION VALUE="one">First element
 <OPTION VALUE="two" SELECTED>Second element
 <OPTION VALUE="three">Third thingy
  </SELECT>

*/
function select($name, $items, $selected='', $misc='')
{
 $text = "<select name=\"{$name}\" {$misc}>";
 $end  = "</select> <!-- HTML: end select \"{$name}\" -->\n";
 // What to indent multi-line items with
 $indent = "&nbsp;&nbsp;&nbsp;&nbsp;";

 if (!is_array($items) || !count($items))
  return ($text . $end);

 while (list($k,$v) = each($items))
 {
  // If there's a linebreak in the value, split the lines into separate
  // OPTION lines, and indent the add'l lines
  $v = preg_replace("/\n/", "\n{$indent}", $v);
  $words = split("\n", $v);

  $already_selected = 0;
  foreach ($words as $word)
  {
   $text .= "\t<option value=\"$k\" ";

   // Using !empty($k) will NOT allow "value=0" to be selected, which is bad
   // For multi-line items, only put SELECTED on the first line
   if ((!$already_selected) && ("$selected" == "$k") && ($selected != '')) {
    $text .= "SELECTED";
    $already_selected = 1;
   }
   $text .= ">{$word}";
   $text .= "</option>\n";
  }
 }

 $text .= $end;

 return $text;
}



--
Scott Hurring
Systems Programmer
EAC Corporation
scott (*) eac.com
--
"Davy Obdam" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
> Hi People,
>
> I ve just started with some OOP programming in PHP and now i have to
> build a set of classes that can generate a html form.. But i have a
> problem with the <select><option></option></select> thing....
> I hope someone can help me here or give me some pointers,... any help is
> apreciated. Here is a part of my code :
>
> class selectField extends formElement {
>
>     //Constructor
>     function selectField($name, $value) {
>         formElement::setName($name);
>         formElement::setValue($value);
>     }
>
>     function generateSelectField() {
>         $theOutput = "<select name=\"".$this->name."\">\n";
>         foreach($this->value as $key=>$val) {
>             $theOutput .= "<option
> value=\"".$this->value."\">".$this->value."</option>\n";
>         }
>         $theOutput .= "</select>\n";
>         Return $theOutput;
>     }
> }
>
> This is how i call the Object:
>
> $test[] = "Test1";
> $test[] = "Test2";
> $test[] = "Test3";
> $test[] = "Test4";
> $test[] = "Test5";
> $select = new selectField("testje", $test);
> echo$select->generateSelectField();
>
> But all i get is a select box with the word Array in it 5 times...Thanks
> for your time...
>
> Best regards,
>
> Davy Obdam
> mailto:[EMAIL PROTECTED]
>
>



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to