How to save image data using custom tables?
Support › MB Custom Table › How to save image data using custom tables?Resolved
- This topic has 8 replies, 3 voices, and was last updated 5 years, 11 months ago by
[email protected].
-
AuthorPosts
-
July 23, 2018 at 8:04 PM #10669
makeagency
ParticipantHi,
There isn't any documentation covering each of the fields using custom tables and was wondering how to store image data?
Thanks.
July 24, 2018 at 10:40 AM #10679Anh Tran
KeymasterHi,
Each custom field value is saved in a corresponding column. The field ID = column name.
What image data do you want to store?
July 24, 2018 at 1:56 PM #10685makeagency
ParticipantHi,
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?July 24, 2018 at 2:21 PM #10687Anh Tran
KeymasterI 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.
June 8, 2019 at 12:05 AM #14875[email protected]
ParticipantHello, 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,
SergioJune 8, 2019 at 8:56 AM #14881Anh Tran
KeymasterHi 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 seea: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 therwmb_meta
function, like this:$images = rwmb_meta( 'info', array( 'object_type' => 'user', 'limit' => 1 ) );
June 8, 2019 at 3:43 PM #14886[email protected]
ParticipantHello, 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
June 10, 2019 at 9:55 AM #14898Anh Tran
KeymasterHi 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() );
June 10, 2019 at 8:18 PM #14915[email protected]
ParticipantPerfect!!! It run well. Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.