Create dropdown from multi array + PHP class
- by chris
Hi there,
Well, I am writing class that creates a DOB selection dropdown.
I am having figure out the dropdown(), it seems working but not exactly. Code just creates one drop down, and under this dropdown all day, month and year data are in one selection. like:
<label>
<sup>*</sup>DOB</label>
<select name="form_bod_year">
<option value=""/>
<option selected="" value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
<option value="5">6</option>
..
<option value="29">30</option>
<option value="30">31</option>
<option value="1">January</option>
<option value="2">February</option>
..
<option value="11">November</option>
<option value="12">December</option>
<option selected="" value="0">1910</option>
<option value="1">1911</option>
..
<option value="98">2008</option>
<option value="99">2009</option>
<option value="100">2010</option>
</select>
Here is my code, I wonder that why all datas are in one selection. It has to be tree selction - Day:Month:Year.
//dropdown connector
class DropDownConnector
{
var $dropDownsDatas;
var $field_label;
var $field_name;
var $locale;
function __construct($dropDownsDatas, $field_label, $field_name, $locale)
{
$this-dropDownsDatas = $dropDownsDatas;
$this-field_label = $field_label;
$this-field_name = $field_name;
$this-locale = $locale;
}
function getValue(){
return $_POST[$this-field_name];
}
function dropdown(){
$selectedVal = $this-getValue($this-field_name);
foreach($this-dropDownsDatas as $keys=$values){
foreach ($values as $key=$value){
$selected = ($key == $selectedVal ? "selected" : "" );
$options .= sprintf('%s',$key,$value);
};
};
return $select_start = "$this-field_desc".$options."";
}
function getLabel(){
$non_req = $this-getNotRequiredData();
$req = in_array($this-field_name, $non_req) ? '' : '*';
return $this-field_label ? $req . $this-field_label : '';
}
function __toString()
{
$id = $this-field_name;
$label = $this-getLabel();
$field = $this-dropdown();
return 'field_name.'"'.$label.''.$field.'';
}
}
function generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years){
$xx = array(
'form_bod_day' = $days_of_month,
'form_bod_month' = $month_list,
'form_bod_year' = $years);
echo $dropDownConnector = new DropDownConnector($xx,'DOB','bod','en-US');
}
// Call php class to use class on external functionss.
$avInq = new formGenerator;
$lang='en-US';
echo generateForm ($lang,$country_list,$states_list,$days_of_month,$month_list,$years);