Forms
|
|
|
|
<?php if (!defined ("_STDCTRL_LIB_")):
// ===================================
// Standart HTML controls
// generator library
// Copyleft Softerra LLC
// www.softerra.com
// Author: bobd@softerra.com
// ===================================
// Version 1.1
// ===================================
// --------------------------------
// ------ Declared functions ------
// --------------------------------
//
// makeOptionsFromSimpleArray ($array, $selectedItems=false)
// makeOptionsFromArray ($array, $valueField, $textField, $selectedItems=false)
// makeOptionsFromHashArray ($array, $selectedItems=false, $reverse=false)
// makeRadioFromArray ($ctrlName, $array, $checkedIndex=false)
// makeCheckboxesFromArray ($ctrlName, $array, $checkedItems=false)
//
// --------------------------------
// ==================================
define (_STDCTRL_LIB_, "stdctrl.lib.php");
function in_values ($val, $vals) {
if (is_array ($vals))
return in_array ($val, $vals);
else
return $val == $vals;
}
function outOption ($value, $text, $selected) {
printf ("<option value="%s" %s>%s", $value, $selected ? "selected":"", $text);
}
function outRadio ($name, $value, $text, $checked) {
printf ("<input type=radio name="%s" value="%s" %s>%s", $name, $value, $checked ? "checked":"", $text);
}
function outCheckbox ($name, $value, $text, $checked) {
printf ("<input type=checkbox name="%s" value="%s" %s>%s", $name, $value, $checked ? "checked":"", $text);
}
function makeOptionsFromSimpleArray ($array, $selectedItems=false) {
for ($i=0; $i < count ($array); $i++)
outOption ($array[$i], $array[$i], in_values ($array[$i], $selectedItems));
}
function makeOptionsFromArray ($array, $valueField, $textField, $selectedItems=false) {
for ($i=0; $i < count ($array); $i++) {
$value = $array[$i][$valueField];
$text = $array[$i][$textField];
outOption ($value, $text, in_values ($value, $selectedItems));
}
}
function makeOptionsFromHashArray ($array, $selectedItems=false, $reverse=false) {
if ($reverse) {
$key = "value";
$val = "text";
} else {
$key = "text";
$val = "value";
}
while (list (${$key}, ${$val}) = each ($array))
outOption ($value, $text, in_values ($value, $selectedItems));
}
function makeRadioFromArray ($ctrlName, $array, $checkedIndex=false) {
for ($i=0; $i<=count ($array); $i++)
outRadio ($ctrlName, $i, $array[$i], $i==$checkedIndex);
}
function makeCheckboxesFromArray ($ctrlName, $array, $checkedItems=false) {
for($i=0; $i<count ($array); $i++)
outCheckbox ($ctrlName . $i, $i, $array[$i], in_values ($i, $checkedItems));
}
endif ?>
|
|
|
Usage Example
|
<?
$options[0] = "first"; $options[1] = "second";
$options[2] = "third"; ?> <select>
<? makeOptionsFromSimpleArray ($options, "first") ?> </select>
|
|
|
Rate This Script
|
|
|
|