Zend - The PHP Company




Forms

Add Code


Very simple option control  

Type: code fragment
Added by: kyong
Entered: 19/02/2001
Last modified: 02/12/2000
Rating: - (fewer than 3 votes)
Views: 8061
I was not so comfortable with this one( http://zend.com/codex.php?id=59&single=1 ) So, I simplified the one enough to feed me.


<?php
// Written by kyong@pauline.or.kr
function print_option($name$options) { // php4 friendly
    
global $$name;  

    echo 
"<select name="$name">n";
    foreach (
$options as $value => $text) {
        
printf ("<option value="%s" %s> %s </option>n"$value$value == $$name "selected":""$text); //selected one goes first
    
}
    echo 
"</select>n";
}
function 
print_option2($name$options) { // php3 friendly
    
global $$name;  

    echo 
"<select name="$name">n";
    while (list (
$value$text) = each ($options)) {
        
printf ("<option value="%s" %s> %s </option>n"$value$value == $$name "selected":""$text); 
    }
    echo 
"</select>n";
}
function 
print_option3($name$value$text) { // your flavor?
    
global $$name;  

    if (
count($value) != count($text)) {
        echo 
"Should be equal in array size";
        exit;
    }
    echo 
"<select name="$name">n";
    for (
$i=0$i<count($value); $i++) {
        
printf ("<option value="%s" %s> %s </option>n"$value[$i], $value[$i] == $$name "selected":""$text[$i]); 
}
?>


Usage Example


// all the same result
// You will have to handle the option control's name to avoid 'Undefined Variable' warning
$options1 = array(
            '1' => 'il',
            '2' => 'ee',
            '3' => 'sam',
            '4' => 'sa',
            'all' => 'su'
);
print_option("status", $options1); //php4

print_option2("status", $options1); //php3

$options1_value = array('1', '2', '3', '4', 'all');
$options1_text  = array('il', 'ee', 'sam', 'sa', 'su');
print_option3("status", $options1_value, $options1_text); // another flavor?


Rate This Script





Search



This Category All Categories