最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Using $WPDB to create simple array to check against user entered value

programmeradmin0浏览0评论

I have a function that checks an entered value against a simple array of values to determine a match (or not).

function validate_unit_type( $result, $value ) {

    $pattern = ["U" ,"SE" ,"SHOP" ,"OFF" ,"APT" ,"CTGE" ,"DUP" ,"FY" ,"F" ,"HSE" ,"KSK" ,"MSNT" ,"MB" ,"PTHS" ,"RM" ,"SHED" ,"SITE" ,"SL" ,"STU" ,"TNHS" ,"VLLA" ,"WARD"];

    if ( !empty( $value ) && !in_array( $value, $pattern ) ) {
        $result['is_valid'] = false;
        $result['message']  = 'Invalid Unit Type.';
    }          

    return $result;
}

This works just fine.

The array values in $pattern are held in a MySQL DB table column too, and I'm trying to get the wpdb class to read these values into an array to suit the function.

    global $wpdb;
    $results = $wpdb->get_results("SELECT unit_type from wpqi_enum_unit_type", ARRAY_A);

Other than adding the above (wpdb) what else do I need to add to the function so it will work?

I have a function that checks an entered value against a simple array of values to determine a match (or not).

function validate_unit_type( $result, $value ) {

    $pattern = ["U" ,"SE" ,"SHOP" ,"OFF" ,"APT" ,"CTGE" ,"DUP" ,"FY" ,"F" ,"HSE" ,"KSK" ,"MSNT" ,"MB" ,"PTHS" ,"RM" ,"SHED" ,"SITE" ,"SL" ,"STU" ,"TNHS" ,"VLLA" ,"WARD"];

    if ( !empty( $value ) && !in_array( $value, $pattern ) ) {
        $result['is_valid'] = false;
        $result['message']  = 'Invalid Unit Type.';
    }          

    return $result;
}

This works just fine.

The array values in $pattern are held in a MySQL DB table column too, and I'm trying to get the wpdb class to read these values into an array to suit the function.

    global $wpdb;
    $results = $wpdb->get_results("SELECT unit_type from wpqi_enum_unit_type", ARRAY_A);

Other than adding the above (wpdb) what else do I need to add to the function so it will work?

Share Improve this question asked Jan 5, 2021 at 4:33 Andy MainwaringAndy Mainwaring 31 bronze badge 1
  • What do you get when you echo out var_dump( $results );? – Tony Djukic Commented Jan 5, 2021 at 12:34
Add a comment  | 

1 Answer 1

Reset to default 1

I'm trying to get the wpdb class to read these values into an array to suit the function

You could manually loop through the $results array to build the $pattern array, but a much simpler way is by using $wpdb->get_col() to get all the values of the unit_type column:

$pattern = $wpdb->get_col( "SELECT unit_type from wpqi_enum_unit_type" );

/* Alternate version (i.e. manually loop through $results):
$pattern = array();

$results = $wpdb->get_results( "SELECT unit_type from wpqi_enum_unit_type" );

foreach ( $results as $row ) {
    $pattern[] = $row->unit_type;
}
*/

Additional Code

This is just a suggestion: Instead of doing in_array() (in validate_unit_type()), you could also do like so, which uses $wpdb->get_var():

if ( ! empty( $value ) ) {
    // This way, there'd be no need for the $pattern.
    $count = (int) $wpdb->get_var( $wpdb->prepare( "
        SELECT COUNT(*) from wpqi_enum_unit_type
        WHERE unit_type = %s
        LIMIT 1
    ", $value ) );

    if ( $count < 1 ) {
        $result['is_valid'] = false;
        $result['message']  = 'Invalid Unit Type.';
    }
}

Additional Note

I'm assuming that wpqi_ is the table prefix, so instead of hard-coding it, I suggest you to use $wpdb->prefix. E.g.

$table = $wpdb->prefix . 'enum_unit_type';
$pattern = $wpdb->get_col( "SELECT unit_type from $table" );
发布评论

评论列表(0)

  1. 暂无评论