2

I'm using Wordpress Settings API. Everything works as it should except this select dropdown. When I select an option, the value echoed is correct but in the dropdown it displays the default first value i.e 6 and not the selected one. Where am I going wrong?

 public function someplugin_select() {
            $options = get_option( 'plugin_252calc');
            echo $options; //shows the correct value selected
            $items = array();
            for ($i=6; $i <=10; $i+= 0.1) 
            { 
                $items[] = $i;
            }

            echo '<select id="cf-nb" name="cf-nb">';
            foreach ( $items as $item )
            {
                echo '<option value="'. $item .'"';
                if ( $item == $options ) echo' selected="selected"';
                echo '>'. $item .'</option>';
            }

            echo '</select>';           

        }
  • 1
    its selected="selected" being echoed? – David Lee Feb 25 '17 at 21:17
  • No. It's not echoing selected=selected even if the value equals options – input Feb 25 '17 at 21:18
  • 1
    do a var_dump('$item == $options') to check if its being true at least 1 time – David Lee Feb 25 '17 at 21:20
  • it's returning false – input Feb 25 '17 at 21:42
2

The reason your $item == $option condition is always failing is because of the way PHP compares floats!

Try the following instead:

echo "<option value='$item'" . selected (abs ($item - $options) <= 0.01, true, false) . ">$item</option>" ;

See Comparing floats for more info.

  • float comparison is so farked in php – wogsland Feb 28 '17 at 2:21
  • for anyone who's interested: the problem is not PHP, per se, it's the binary representation of floats. see Comparing Floating Point Numbers, 2012 Edition for a good discussion of why the epsilon method above is not ideal in all cases (but certainly good enough for the OP). – Paul 'Sparrow Hawk' Biron Feb 28 '17 at 12:36
0

I went through this, and I could tell that there's a WP function called selected, which you can see in this link. Use this function instead of doing: if ( $item == $options ). And your code could look like this:

foreach ( $seconds as $second => $time ) {
   ?>
      <option value="<?php echo $second; ?>" <?php selected( $browser_cache_ttl, $second ); ?>><?php echo $time; ?></option>
   <?php
}

So that function loops through $seconds and get the $second and $time, put te $second as value, then call the selected function which the first parameter is the value to compare and the second one is the current value, if those values are the same, it'll be selected.

I think that's what you're looking for. Tell me if it worked.

0

Replace your select with this... Hope it works...

        echo '<select id="cf-nb" name="cf-nb">';
        foreach ( $items as $item ){
            $if_selected = $item == $options ? "selected='selected'" : "";
            echo "<option value='{$item}' {$if_selected}>$item</option>";

        }

        echo '</select>'; 
  • Not sure why, but it didn't work. – input Feb 25 '17 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.