$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Dropdown Value from DB Entry|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Dropdown Value from DB Entry

matteradmin7PV0评论

I am capturing a month in my database and the value (integer) is being recorded but when I try to add the selected to the drop down so when the user comes back it reflects the month I can't figure out what I need to set the drop down to reflect the integer value that was previously submitted. If someone can share what i am missing in my selected code I would really appreciate it.

    $months = array(
        1=>'January',
        2=>'February',
        3=>'March',
        4=>'April',
        5=>'May',
        6=>'June',
        7=>'July',
        8=>'August',
        9=>'September',
        10=>'October',
        11=>'November',
        12=>'December'
    );

    $birth_date_month = get_the_author_meta( 'birth_date_month', $user->ID );

    <?php
    foreach ( $months as $num => $month ) {
        printf('<option value="%u">%s</option>', $num, $month, selected( $birth_date_month, $num, false ) );
    }
    ?>

I am capturing a month in my database and the value (integer) is being recorded but when I try to add the selected to the drop down so when the user comes back it reflects the month I can't figure out what I need to set the drop down to reflect the integer value that was previously submitted. If someone can share what i am missing in my selected code I would really appreciate it.

    $months = array(
        1=>'January',
        2=>'February',
        3=>'March',
        4=>'April',
        5=>'May',
        6=>'June',
        7=>'July',
        8=>'August',
        9=>'September',
        10=>'October',
        11=>'November',
        12=>'December'
    );

    $birth_date_month = get_the_author_meta( 'birth_date_month', $user->ID );

    <?php
    foreach ( $months as $num => $month ) {
        printf('<option value="%u">%s</option>', $num, $month, selected( $birth_date_month, $num, false ) );
    }
    ?>
Share Improve this question edited Feb 16, 2019 at 0:46 Tom J Nowell 61.2k7 gold badges79 silver badges150 bronze badges asked Feb 15, 2019 at 23:08 user3692010user3692010 152 bronze badges 1
  • 1 It's not possible to tell what the value of birth_date_month would be from the code you've given, the save code would be necessary. But since you have access to the code, could you not just do echo $birth_date_month and look? – Tom J Nowell Commented Feb 16, 2019 at 0:47
Add a comment  | 

1 Answer 1

Reset to default 0

You're not actually outputting the selected attribute into the <option> tag. Look at your printf() format:

'<option value="%u">%s</option>'

%u is there for the month's number value, and %s is there for month's name, but you're not including the last argument from the result of selected().

Your printf() needs to look like this:

printf(
    '<option value="%u" %s>%s</option>', 
    $num, 
    selected( $birth_date_month, $num, false ), 
    $month
);

Note that I swapped the order of the arguments, because selected needs to be output before the month name.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far