Zend - The PHP Company




Forms

Add Code


Simple HTML controls generator library  

Type: class library
Added by: Boba
Entered: 03/03/2000
Last modified: 08/12/1999
Rating: ***** (9 votes)
Views: 13619
Allows to generate HTML controls as SELECT, RADIO, CHECKBOX from PHP arrays.


<?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$textin_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$textin_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





Search



This Category All Categories