如何检查是否至少标记了2个复选框才能继续?

时间:2016-09-16 06:26:39

标签: php checkbox

如何要检查至少2个复选框才能继续?我有以下php:

/**
     * check_required_addons function.
     *
     * @access private
     * @param mixed $product_id
     * @return void
     */
    private function check_required_addons( $product_id ) {
        $addons = get_product_addons( $product_id, false, false, true ); // No parent addons, but yes to global

        if ( $addons && ! empty( $addons ) ) {
            foreach ( $addons as $addon ) {
                if ( '1' == $addon['required'] ) {
                    return true;
                }
            }
        }

        return false;
    }

其中$ addons = array();在这个数组中有复选框。如何检查至少2个要标记的复选框以继续?

2 个答案:

答案 0 :(得分:3)

private function check_required_addons( $product_id ) {
                $addons = get_product_addons($product_id, false, false, true ); // No parent addons, but yes to global

                if ( $addons && ! empty( $addons ) ) {
                  $count = 0;
                    foreach ( $addons as $addon ) {
                        if ( '1' == $addon['required'] ) {
                            $count++;
                        }
                    }
                   if($count >= 2){
                       return true;
                   }
                }

                return false;
            }

答案 1 :(得分:2)