$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'); ?>options - Using get_option() for check box field|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)

options - Using get_option() for check box field

matteradmin11PV0评论

I am trying to have a check field saved to database. it works . but in get_option i am passing the default value. but even if disable the checkbox(value 0) the field shows as ticked ..this is my code

<?php     
 $fields_default = array(
                0 => array(
                    'id'             => 0,
                    'checkbox_check' => '1',
                ),
                1 => array(
                    'id'             => 1,
                    'checkbox_check' => '0',
                ));

            $orderdescider = get_option('fields_new', $fields_default);

            foreach ($orderdescider as $checkfieldstatus) {
               if (isset($checkfieldstatus['id'])) {$id = $checkfieldstatus['id'];}
                if (isset($checkfieldstatus['checkbox_check'])) {$checkbox_check = $checkfieldstatus['checkbox_check'];} else { $checkbox_check = '0';}
                ?>

                <li class="sortable-item flexit">
                <div class="form-check">
                <input type="checkbox" class="form-check-input" id="exampleCheck1" name="orderdescider[<?php echo $id; ?>][checkbox_check]" value="1"<?php checked(isset($orderdescider[$id]['checkbox_check']));?> />
                 </div></li>
                <?php

            }

please help thanks

I am trying to have a check field saved to database. it works . but in get_option i am passing the default value. but even if disable the checkbox(value 0) the field shows as ticked ..this is my code

<?php     
 $fields_default = array(
                0 => array(
                    'id'             => 0,
                    'checkbox_check' => '1',
                ),
                1 => array(
                    'id'             => 1,
                    'checkbox_check' => '0',
                ));

            $orderdescider = get_option('fields_new', $fields_default);

            foreach ($orderdescider as $checkfieldstatus) {
               if (isset($checkfieldstatus['id'])) {$id = $checkfieldstatus['id'];}
                if (isset($checkfieldstatus['checkbox_check'])) {$checkbox_check = $checkfieldstatus['checkbox_check'];} else { $checkbox_check = '0';}
                ?>

                <li class="sortable-item flexit">
                <div class="form-check">
                <input type="checkbox" class="form-check-input" id="exampleCheck1" name="orderdescider[<?php echo $id; ?>][checkbox_check]" value="1"<?php checked(isset($orderdescider[$id]['checkbox_check']));?> />
                 </div></li>
                <?php

            }

please help thanks

Share Improve this question edited Nov 7, 2018 at 16:51 asked Nov 7, 2018 at 16:13 user145078user145078 20
  • 1 I'm unable to replicate your code, I get the following error code: Parse error: syntax error, unexpected '$orderdescider' (T_VARIABLE) – Remzi Cavdar Commented Nov 7, 2018 at 16:23
  • Could you paste all your code, so that I can take a look at it? – Remzi Cavdar Commented Nov 7, 2018 at 16:23
  • @RemziCavdar actually the array was not closed, please have a check now – user145078 Commented Nov 7, 2018 at 16:25
  • 1 I also get Notice: Undefined variable: value_new – Remzi Cavdar Commented Nov 7, 2018 at 16:49
  • 1 I don't know the answer, but I get help you out with your coding style, so that it is more readble, see: pastebin/Eimh4RZ5 – Remzi Cavdar Commented Nov 7, 2018 at 17:00
 |  Show 15 more comments

1 Answer 1

Reset to default 0

This may not exactly answer the question, but let's see the issues with your code:

  1. If you want to save the data in the same format as the default value ($fields_default), then:

    1. Start your foreach like so: foreach ( $orderdescider as $i => $item ).

    2. Then inside that foreach, $id and $checkbox should always be set and defined like so:

      $id = isset( $item['id'] ) ? $item['id'] : '';
      $checkbox_check = isset( $item['checkbox_check'] ) ? $item['checkbox_check'] : '';
      
    3. And use the appropriate value in the field's name:

      <input type="hidden" name="orderdescider[<?php echo $i; ?>][id]"
        value="<?php echo $id; ?>">
      
      <input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]"
        value="1" ...>
      
  2. For the checkbox fields, you can use this format when writing the markup:

    <input type="checkbox" value="{value}"<?php // wrapped for clarity
      checked( '{value}', '{current/saved value}' ) ?> ...>
    

    so in your case, it would look like this:

    <input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]"
      value="1"<?php checked( '1', $checkbox_check ) ?> ...>
    

    where 1 is the {value}, and $checkbox_check is the {current/saved value}.

    (See the reference for more details about using the checked() function.)

Hope that helps, and here's the full foreach code I used for testing: (I omitted the field's id, but make sure to use unique values)

foreach ($orderdescider as $i => $item) {
    $id = isset( $item['id'] ) ? $item['id'] : '';
    $checkbox_check = isset( $item['checkbox_check'] ) ? $item['checkbox_check'] : '';
    ?>
    <li class="sortable-item flexit">
        <div class="form-check">
            <input type="hidden" name="orderdescider[<?php echo $i; ?>][id]"
              value="<?php echo $id; ?>">
            <input type="checkbox" name="orderdescider[<?php echo $i; ?>][checkbox_check]"
              value="1"<?php checked( '1', $checkbox_check ) ?> class="form-check-input">
        </div>
    </li>
    <?php
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far