Thanks scott,

This might definatly come in handy later as i am new too OOP in PHP. I am now trying to generate a set of radio boxes... but without succes yet...

Best regards,

Davy Obdam

Scott Hurring wrote:

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
$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:
<OPTION VALUE="one">First element
<OPTION VALUE="two" SELECTED>Second element
<OPTION VALUE="three">Third thingy

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 (*)
"Davy Obdam" <[EMAIL PROTECTED]> wrote in message

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 {

function selectField($name, $value) {

function generateSelectField() {
$theOutput = "<select name=\"".$this->name."\">\n";
foreach($this->value as $key=>$val) {
$theOutput .= "<option
$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);

But all i get is a select box with the word Array in it 5 times...Thanks
for your time...

Best regards,

Davy Obdam

PHP General Mailing List (
To unsubscribe, visit:

Reply via email to