Custom Fields
Re-Envisioned
Support MB Custom Table How to save image data using custom tables?

  • Creator
    Topic
  • #10669
    Resolved makeagency
    Participant

    Hi,

    There isn’t any documentation covering each of the fields using custom tables and was wondering how to store image data?

    Thanks.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Author
    Replies
  • #10679
    Anh Tran
    Keymaster

    Hi,

    Each custom field value is saved in a corresponding column. The field ID = column name.

    What image data do you want to store?

    #10685
    makeagency
    Participant

    Hi,

    So for example:

    
    MB_Custom_Table_API::create( 'my_custom_table', array(
        'address' => 'TEXT NOT NULL',
        'phone'   => 'TEXT NOT NULL',
        'email'   => 'TEXT NOT NULL',
    ) );
    
    

    Would we use 'TEXT NOT NULL' for an image?

    #10687
    Anh Tran
    Keymaster

    I got it. You still can save image in the database by encoding it. Use a tool like this, or base64_encode function to convert the image file into a text string. Then you can save it. However, you need to handle outputting the image by decoding it, because the value stored is the encoded data, not the real image.

    #14875

    Hello, I think, related with this post:
    I save in the custom table of users named “usuarios”, a avatar image of this user:

    $meta_boxes[] = array(
        //'title' => 'Custom avatar',
        'id'        => 'avatar',
            'type'  => 'user', // Specifically for user
        'storage_type'  => 'custom_table',    // Important
            'table'         => 'usuarios', // Your custom table name 
            'fields' => array(
            array(
                'name'            => 'Subir/Upload Avatar',
                'id'              => 'avatar',
                'type'            => 'image_advanced',
                'max_file_uploads' => 1,
            ),
        ),
    );
    

    The value of the image saved is: a:2:{i:0;s:0:””;i:1;i:734;}

    It’s ok? Or I have to make the econding? How can I make this encoding?

    To get the image in a post, I have to make this function:

    1.- First I get the row of the user in $r
    2.- Take the value of the image: $resultado = $r[‘avatar’];
    3.- $resultado = ‘img src=”‘.$r[‘avatar’].'” /> ‘; // don’t put here the initial < because don’t show the code the post…

    But this don’t run, don’t know how to save and get the image to show in frontend…

    In the documentation: https://docs.metabox.io/fields/image-advanced/

    $images = rwmb_meta( 'info', array( 'limit' => 1 ) ); --> I modify the field 'info' for my image field: $r['avatar']
    $image = reset( $images );
    $izquierda .= '<img src="'.$image['url'].'" />';

    But don’t run…

    Can you help me please?

    Thanks,
    Sergio

    #14881
    Anh Tran
    Keymaster

    Hi Sergio,

    The original post was about storing image data, e.g. storing full image in the DB.

    In your case, you are using image_advanced field, which stores image IDs in the DB, which is the default behavior of the plugin and is totally fine. The data is automatically serialized, that’s why you see a:2:{i:0;s:0:””;i:1;i:734;}.

    The reason your helper doesn’t work is because you register the meta box for users. So, you need to set the object_type for the rwmb_meta function, like this:

    $images = rwmb_meta( 'info', array( 'object_type' => 'user', 'limit' => 1 ) );

    #14886

    Hello, sorry for mixing different threads…
    I do not just work:
    I try:

    global $wpdb;
    $r = $wpdb->get_row($wpdb->prepare("SELECT * FROM $bbdd WHERE ID = $ID"), ARRAY_A );
    
    $images = rwmb_meta('info', array( 'object_type' => 'user', 'limit' => 1 ) );
    

    or

    $images = rwmb_meta('avatar', array( 'object_type' => 'user', 'limit' => 1 ) );
    

    or

    $images = rwmb_meta( $r['avatar'] , array( 'object_type' => 'user', 'limit' =>> 1 ) );
    

    and in the 3 options it return null…

    I can’t get the $image[‘url’]; after $image = reset( $images );

    what am I doing wrong? (I use custom table “usuarios” related with “users” and the field name or id of the advanced image is “avatar”).

    Thanks

    #14898
    Anh Tran
    Keymaster

    Hi Sergio,

    I know why. You need to pass the user ID to the function as the 3rd param. By default, the function takes the current post ID. So the code should be:

    $images = rwmb_meta( 'avatar', ['object_type' => 'user', 'limit' => 1], get_current_user_id() );
    
    #14915

    Perfect!!! It run well. Thanks!

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.