0

我正在尝试在 Wordpress 中保存复选框的值。我面临的问题很简单,if( isset( $used_checkboxes_csv ) )即使没有选中任何复选框也总是返回 true,因此总是创建数据库条目。任何想法如何解决这个问题或使用什么来代替 isset?

//Listing details features and services meta checkboxes
add_action( 'add_meta_boxes', 'cd_meta_checklist_add' );
function cd_meta_checklist_add() {add_meta_box( 'checklist-id', 'Listing Icons', 'cd_meta_checklist_cb', 'post', 'normal', 'high' );}

function cd_meta_checklist_cb() 
{ 

        global $post;
        $values = get_post_custom( $post->ID );

        //check of checkbox should be active or not
        $pricelistline = explode(",", get_post_meta($post->ID, 'meta_features_checklist', true));

        // We'll use this nonce field later on when saving.
        wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

?>

        <p>
            <label for="meta_box_check_bar">bar</label>
            <input type="checkbox" id="meta_box_check_bar" name="meta_box_check_bar" value="bar" <?php if ($pricelistline[0]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_parking">parking</label>
            <input type="checkbox" id="meta_box_check_parking" name="meta_box_check_parking" value="parking" <?php if ($pricelistline[1]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_accessible-for-disabled">accessible-for-disabled</label>
            <input type="checkbox" id="meta_box_check_accessible-for-disabled" name="meta_box_check_accessible-for-disabled" value="accessible-for-disabled" <?php if ($pricelistline[2]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_air-conditioning">air-conditioning</label>
            <input type="checkbox" id="meta_box_check_air-conditioning" name="meta_box_check_air-conditioning" value="air-conditioning" <?php if ($pricelistline[3]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_frigo-bar">frigo-bar </label>
            <input type="checkbox" id="meta_box_check_frigo-bar" name="meta_box_check_frigo-bar" value="frigo-bar" <?php if ($pricelistline[4]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_pets">pets</label>
            <input type="checkbox" id="meta_box_check_pets" name="meta_box_check_pets" value="pets" <?php if ($pricelistline[5]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_phone">phone</label>
            <input type="checkbox" id="meta_box_check_phone" name="meta_box_check_phone" value="phone" <?php if ($pricelistline[6]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_tv">tv</label>
            <input type="checkbox" id="meta_box_check_tv" name="meta_box_check_tv" value="tv" <?php if ($pricelistline[7]) {echo "checked";} ?> />
        </p>
        <p> 
            <label for="meta_box_check_local-dishes">local-dishes</label>
            <input type="checkbox" id="meta_box_check_local-dishes" name="meta_box_check_local-dishes" value="local-dishes" <?php if ($pricelistline[8]) {echo "checked";} ?> />
        </p>
<?php }

//Saving checkbox states
add_action( 'save_post', 'cd_meta_checkbox_save' );  
function cd_meta_checkbox_save( $post_id )  
{  
    // Bail if we're doing an auto save  
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 

    // if our nonce isn't there, or we can't verify it, bail 
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return; 

    // if our current user can't edit this post, bail  
    if( !current_user_can( 'edit_post' ) ) return;


    //grabing all checkbox values and combining them

                                /* make an array for all used checkbox */
                                $used_checkboxes = array();

                                /* make an array whit all options */
                                $avaible_checkboxes = explode(',', "bar,parking,accessible-for-disabled,air-conditioning,frigo-bar,pets,phone,tv,local-dishes");


                                /* loop troguht all avaible checkboxes */
                                foreach($avaible_checkboxes as $current_key)
                                {
                                   /* check if the checkbox was sent */
                                   if(isset($_POST["meta_box_check_{$current_key}"]))
                                   {
                                      /* if sent, add key to list */
                                      $used_checkboxes[$current_key] = $current_key;
                                   }
                                   else
                                   {
                                      /* if not sent, add empty value to list */
                                      $used_checkboxes[$current_key] = '';
                                   }
                                }

                                /* convert list to csv */
                                $used_checkboxes_csv = implode(',', $used_checkboxes);


    //saving to DB
    if( isset( $used_checkboxes_csv ) )
    update_post_meta( $post_id, 'meta_features_checklist', esc_attr( $used_checkboxes_csv ) );


}
4

4 回答 4

0

听起来您想使用emptywhich 检查变量中是否包含任何内容,或者可能is_null检查变量是否设置为 null。

就您的代码而言:

$used_checkboxes = array();
// You SET the variable here

if( isset( $used_checkboxes_csv ) )
// So the isset function will return true

// To remove empty elements in an array, you can do this in your implode statement:
implode(',', array_filter($used_checkboxes));

<?php
$expected_array_got_string = 'somestring';
var_dump(empty($expected_array_got_string['some_key']));
var_dump(empty($expected_array_got_string[0]));
var_dump(empty($expected_array_got_string['0']));
var_dump(empty($expected_array_got_string[0.5]));
var_dump(empty($expected_array_got_string['0.5']));
var_dump(empty($expected_array_got_string['0 Mostel']));
?>

Output of the above example in PHP 5.3:

bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)

Output of the above example in PHP 5.4:

bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)

一片空白

<?php

error_reporting(E_ALL);

$foo = NULL;
var_dump(is_null($inexistent), is_null($foo));

?>

Notice: Undefined variable: inexistent in ...
bool(true)
bool(true)
于 2012-08-31T09:40:26.600 回答
0

您在此处设置变量:

$used_checkboxes_csv = implode(',', $used_checkboxes);

不管它是否有正确的内容,它被设置是因为它确实存在。

这意味着:

if( isset( $used_checkboxes_csv ) )

将始终返回 true。

你可以做的是:

$used_checkboxes_csv = NULL;
if( count($used_checkboxes) > 0){
    $used_checkboxes_csv = implode(',', $used_checkboxes);
}

然后,这样检查:

if( strlen($used_checkboxes_csv) > 0 ){
于 2012-08-31T09:41:14.333 回答
0

因为 $_POST 包含您的值的空字符串,并且 isset() 返回 true。在 if 条件中使用 empty() 或添加 != '' 。

于 2012-08-31T09:41:25.503 回答
0

如果声明了变量,仍会设置 Null 和/或空字符串。试试这个:

if(isset($used_checkboxes_csv ) && $used_checkboxes_csv == 'somevalue')
于 2012-08-31T09:45:59.587 回答