开发者

Pull-down select menu works, but won't pre-select

开发者 https://www.devze.com 2023-03-29 13:03 出处:网络
I have a function that take 2 parameters and outputs a select pulldown menu. The first parameter is simply the name, and the second is a parameter ($sel) that holds the selected element (the开发者_如何

I have a function that take 2 parameters and outputs a select pulldown menu. The first parameter is simply the name, and the second is a parameter ($sel) that holds the selected element (the开发者_如何学C data is first populated from a db). This works fine, except the if statement (that determine is used to pre-select an option from the database) doesn't seem to work. I've verified there should be a match, and that both $sel and date('g:i ', $i) are both strings..so in principle this should work..but it doesn't.

Can you please take a look and see if there is something I'm doing wrong?

function selectTime_Hour($timeName,$sel){
$start = strtotime('1:00am');
$end = strtotime('12:00pm');

$menu = '<select  name="'.$timeName.'">';
for ($i = $start; $i <= $end; $i += 1800)
{
    if ($sel==date('g:i ', $i)){
      $menu .=  '<option selected="selected">' . date('g:i ', $i) . '</option>';
      }
      else {


 $menu .=  '<option value="'.date('g:i',$i).'">' . date('g:i ', $i) . '</option>';
}
}
 $menu .= '</select>';
 return $menu;

}


I'm guessing it's the extra space you're adding in some of those date() calls.

You are populating your <option> tags with values of date('g:i',$i), which I presume you are then storing to the database. However, in your if statement, you are comparing this against date('g:i ',$i), with an extra space after the g:i.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号