Zend - The PHP Company




Forms

Add Code


Generate options array for select  

Type: code fragment
Added by: ADMIN
Entered: 12/01/2000
Last modified: 08/12/1999
Rating: - (fewer than 3 votes)
Views: 9268
This code produces a list of OPTION tags from an array, array keys being option values and array items being option texts. It will also pre-select items from the second argument


<?php
  
Function array2select($arr,$sel "") {
    while ( list( 
$key$val ) = each($arr)) {
        if(
$sel && ($key == $sel || is_array($sel) && in_array($key,$sel))) {
           
$select_v=" selected";
        } else { 
           
$select_v=""
            }
        echo 
"<option value="$key$select_v>$val</option>n";
    }
  }
?>


Usage Example


<?php 
ereg
("^([0-9]+)-([0-9]+)-([0-9]+)",$when,$ymd_when);
  
$day_when_arr=range(1,31);
  
array2select($day_when_arr,$ymd_when[3]); ?>
</SELECT>
<SELECT NAME="when_mon">
<?php 
  array2select
($months,$ymd_when[2]); ?>
</SELECT>
<SELECT NAME="when_year">
<?php 
  $y_when_arr
=range(1970,2010);
  
array2select($y_when_arr,$ymd_when[1]); ?>
</SELECT>


Rate This Script





Search



This Category All Categories