• Resolved fejanos

    (@fejanos)


    I want to display values from sql query next to a single post.
    This is the function I use:

    add_action('show_etiqueta','et_get_post_user_etiqueta');
    function et_get_post_user_etiqueta( $post_id ){
    global $wpdb;
        $result = $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) AS countof FROM $wpdb->commentmeta WHERE meta_key='et_comment_etiqueta' GROUP BY meta_value ORDER BY countof DESC LIMIT 5", $post_id));
       print_r($result);
    return $result;
    }

    I call it here:
    <?php do_action( 'show_etiqueta' ); ?>
    But all I get as output is this:
    Array ( [0] => stdClass Object ( [meta_value] => test [countof] => 3 ) [1] => stdClass Object ( [meta_value] => inprediscible [countof] => 1 ) [2] => stdClass Object ( [meta_value] => interesante [countof] => 1 ) [3] => stdClass Object ( [meta_value] => aqui va tu etiqueta [countof] => 1 ) [4] => stdClass Object ( [meta_value] => hurtest [countof] => 1 ) )
    I tested the query in phpmyadmin to see if I get the result I want and it really gets the right result.
    Can Anyone help me to have the right result displayed? tHank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi fejanos. The data that you’re seeing is the results from the database. What you need to do is format the data. What you’re doing is printing the raw data, and now you need to print the data.

    It may look a bit better if you print it:
    INSTEAD OF USING:
    print_r($result);
    TRY USING:
    echo "<pre>"; print_r($result); echo "</pre>";

    If you want to print it, try looping through the data using php’s foreach function.

    For example, this *may* work (not actually tested):

    echo "<table>";
    foreach($result as $key => $value)
    {
    	echo "	<tr>
    			<td>$key</td>
    			<td>" . $value->meta_value . "</td>
    			<td>" . $value->countof . "</td>
    		</tr>
    	";
    }
    echo "</table>";
    Thread Starter fejanos

    (@fejanos)

    Thank you very much.

    Hi fejanos, I hope I was able to help! I’m not sure how much experience you have with PHP, but feel free to let me know if you have any follow up questions, I’m more than happy to help further.

    Thread Starter fejanos

    (@fejanos)

    Well I’ve been learning PHP for 2 years, it’s mostly tutorial following and script modifying but sometimes I have a hard time creating scripts if you know what I mean. Now I’m facing with another issue that I try to solve and I’m working on: These values that I wanted to display before are words that users submit through a custom comment field. The function above makes a top list where the top 5 are the most submitted words appear. I want to make that post specific. For each individual single post must have a top 5 words list. Now it makes a top list from all the submitted words at all the posts.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Retrieve values from WordPress database’ is closed to new replies.