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>';           

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

4 Answers 4

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.

2
  • float comparison is so farked in php
    – wogsland
    Feb 28, 2017 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). Feb 28, 2017 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>'; 
1
  • Not sure why, but it didn't work.
    – input
    Feb 25, 2017 at 21:47
0

I don't have any better answers, but I feel I have a similar-enough issue to add to this question instead of making a new one.

I started with https://www.youtube.com/watch?v=hbJiwm5YL5Q&t=3969s and got as far as 1:05:00 and this is my current version is

function selectpageHTML() {?>
            <select name="lp_actPage"> 
                <?
                    $pages = get_pages();                       foreach ( $pages as $page ) {                               
                                $title = $page->post_title;
                                echo "<option value='$title'" . selected(get_option('lp_actPage', $title)) .">$title</option>";
                                } ?>
            </select>
        <?}

however, instead of comparing floats I was thinking I could compare string values instead, but so far no dice.

I also tried assigning a variable as the page ID and using that variable as the option value as well as the 2nd argument in the get_option() function, but still no joy.

1

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.