Grow your CSS skills. Land your dream job.

How to remove the Empty ‘space’ coming from database?

  • # April 21, 2013 at 4:27 pm

    Hi , I m creating theme on wordpress. And I m having a problem in options page. But my problem is not a wordpress problem its just a PHP problem.

    I have created a form so user can upload images/SWF which is working great however when they delete the URL of the image I want the image to disappear however it leaves a whitespace. I have checked the database I have noticed that when the image path value is deleted by the user, it still leave a white space which messes my condition, so if I check if ($value==”) I would have to check if($value==’ ‘).

    < ?php
    //meta_value is the image/swf url which I m having problems with.
    if (has_post_thumbnail() && $meta_value==”) {
    the_post_thumbnail();
    }elseif(!has_post_thumbnail() && $meta_value==”){ ?>

    < ?php the_post_thumbnail(); ?>

    http://mstoicthemes.com/about-us/

    Thanks

    # April 21, 2013 at 4:33 pm

    thats not a php problem its a css one your post thumbnail has a width of 590px and your container has a width of 971px.
    the_post_thumbnail accepts also numeric values so you can decide the width and height of the thumbnail being displayed

    http://codex.wordpress.org/Function_Reference/the_post_thumbnail

    just to see it your self setting the width of the img element to 100% takes the whole space but the image is streched

    # April 21, 2013 at 4:53 pm

    Actually if you look at the condition I have mentioned above, the 590px image will only show when there is swf file available. If

    if(has_post_thumbnail() && !$meta_value==”){
    //then only show the small image so I can adjust the SWF and the image.
    }

    if the SWF file path value is empty then use 971px image

    elseif(!has_post_thumbnail() && $meta_value==”){ ?>

    //show the image which is 971px large.

    < ?php } ?>

    So that’s problem since a white space is coming from the database my condition is being incorrect. because ther is difference between ” and ‘ ‘. So this is PHP problem

    Thanks for replying.

    # April 21, 2013 at 5:02 pm

    make sure your meta value is properly saved to the database and retrieve it with get post meta

    http://codex.wordpress.org/Function_Reference/get_post_meta

    you could also use the empty() php function combined with trim() to remove whitespaces

    # April 21, 2013 at 5:23 pm

    Okay Thanks a lot . Actually I figured something, its not the worpdress’s issue, When I delete the url from the input and if I forget a white space the problem begins because a white space is saved in the database and it spits the same out, so lets forget wordpress. How do I prevent this from happening, or how do I trim it with php? because in wordpress we dont run a query to insert values into the databata, we just give the input a name and wordpress takes care of the rest.
    I dont know how to catch the value to trim it on its way to database.

    # April 21, 2013 at 5:31 pm

    Okay I got it I used

    < ?php $meta_value_final= trim($meta_value) ?>

    and now I m checking the condition on $meta_value_final and its doing what I want it to do
    All thanks to you pixelgrid

    # April 21, 2013 at 5:33 pm

    i suppose you are using a plugin for the metaboxes which adds as many as the user needs.Unless you change teh plugin files you cant but retriving this

    with retrieve the value from the database and in case it is empty or just whitespace it will remove it and will correctly say it has no value.

    $meta_value = get_post_meta(get_the_ID,’meta_value_name’);
    $meta_value = trim($meta_value);

    if(empty($meta_value) && has_post_thumbnail() ){ }
    if(empty($meta_value) && !has_post_thumbnail() ){ }

    if you have written the add meta box just take care when you insert the retrieved meta value to the value attribute of the input to not have any white space its very common

    __
    # April 21, 2013 at 5:38 pm

    nvm @pixelgrid beat me to it

    : )

    # April 22, 2013 at 1:05 am

    I m not using any plugin , I have written the code from a tutorial http://themefoundation.com/wordpress-meta-boxes-guide/
    Where do I use this trim() ??

    Here is the whole code

    function example_custom_meta() {
    add_meta_box( ‘example_meta’, ‘Featured SWF’, ‘example_meta_callback’, ‘page’, ‘side’, ‘default’ );
    }
    add_action( ‘add_meta_boxes’, ‘example_custom_meta’ );

    /* Outputs the content of the meta box */

    function example_meta_callback( $post ) {
    //To verify the input and for security
    wp_nonce_field( basename( __FILE__ ), ‘example_nonce’ );
    $example_stored_meta = get_post_meta( $post->ID );
    ?>


    < ?php }

    function example_meta_save( $post_id ) {

    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ 'example_nonce' ] ) && wp_verify_nonce( $_POST[ 'example_nonce' ], basename( __FILE__ ) ) ) ? ‘true’ : ‘false’;

    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
    return;
    }

    // Checks for input and saves if needed
    if( isset( $_POST[ 'meta-image' ] ) ) {
    update_post_meta( $post_id, ‘meta-image’, $_POST[ 'meta-image' ] );
    }

    } // end example_meta_save()
    add_action( ‘save_post’, ‘example_meta_save’ );

    ?>

    < ?php

    /**
    * Loads the image management javascript
    */
    function example_image_enqueue() {
    global $typenow;
    if( $typenow == ‘page’ ) {
    wp_enqueue_media();

    // Registers and enqueues the required javascript.
    wp_register_script( ‘meta-image’, get_template_directory_uri() . ‘/meta-image.js’, array( ‘jquery’ ) );
    wp_localize_script( ‘meta-image’, ‘meta_image’,
    array(
    ‘title’ => ‘Choose or Upload an Image’,
    ‘button’ => ‘Use this image’,
    )
    );
    wp_enqueue_script( ‘meta-image’ );
    } // End if
    } // End example_image_enqueue()
    add_action( ‘admin_enqueue_scripts’, ‘example_image_enqueue’ );

    ?>

    # April 22, 2013 at 1:46 am

    in the link you provided when you write this line

    Viewing 10 posts - 1 through 10 (of 10 total)

    You must be logged in to reply to this topic.

    Projects

    *May or may not contain any actual "CSS" or "Tricks".