I seem to be running into a bit of an issue that I could really use the help with. I have probably just missed something simple but I am starting to get frustrated with it so I thought it best to see if anyone can help.
What it is suppose to do:
Return a fully working select box for an html form. It is also suppose to check to see if the index of an array item is 'SELECTED' and if it is then add SELECTED to that option and only that option. It should also only allow for one item to be selected.
What does not work:
The check to see if a SELECTED has already been placed ($selected). If I get rid of that if, there are two SELECTED printed (the if prints the incorrect item).
Code:
Code: Select all
class Page {
public $userSex1 = array('SELECTED' => 'Male','Female','Other');
public $userSex2 = array('Male','SELECTED' => 'Female','Other');
public function renderSelect($name,$options=0,$min=18,$max=60,$action='none') {
$source = '<select name="'.$name.'">';
if(is_array($options)) {
$selected = 0;
foreach($options as $key => $value) {
if($key == 'SELECTED' AND $selected == 0) {
$source .= '<option value="'.$value.'" SELECTED>'.$value.'</option>';
$selected = 1;
}else{
$source .= '<option value="'.$value.'">'.$value.'</option>';
}
}
}else{
$count = $min;
while($count<$max) {
$source .= '<option value="'.$count.'">'.$count.'</option>';
$count++;
}
$source .= '<option value="'.$count.'+">'.$count.'+</option>';
}
$source .= '</select>';
return $source;
}
}When I call this function, is there a way to pass the class's member array and change one of the keys in that array all in one statement? I would like to keep the page coding to a minimum and having a ton of different member arrays inside the class just seems like a waste when most of them will be the same (except for the selected one).
Thanks.