WordPress.org

Ready to get started?Download WordPress

Forums

How to create query of comment_extra database for echo... (5 posts)

  1. hughhempel
    Member
    Posted 6 years ago #

    Greetings,

    I suspect that this SHOULD be simple, but I have scoured everything without luck. I am trying to add a simple "location" metadata line underneath the "<cite>" in each comment.

    I have installed the "comments_extra_fields" plugin and have created a 'new' table that contains city, state, country. The form is working and collecting the commentor location data.

    All that is left is to query the new table and "echo" the results underneath each comment...

    I have a tried wpdb, mysql_query, etc... I can get results back from the "core" WP database schema, but not from the "new" table...

    I am trying to do something close to the following sql query that works fine in MYSQLADMIN:

    <?php
    $sql = 'SELECT <code>commentcity</code> FROM <code>wp_comments_extra</code> WHERE <code>comment_id</code> = 143 LIMIT 0, 30 ';
    $name3 = mysql_query($sql);
    echo $name3;
    ?>

    Please be kind, I am stretching my rubberband here... :~)

    Thanks in advance!

  2. Kafkaesqui

    Posted 6 years ago #

    Looking at the description of that plugin:

    http://www.ideashower.com/solutions/wordpress-plugin-extra-comment-fields/

    It appears the extra data is already merged with the $comments (and so $comment) object. So in your comments loop you could try just:

    <?php echo $comment->commentcity; ?>

    [Moved from the WP-Advanced forum as it's pretty plugin-specific.]

  3. hughhempel
    Member
    Posted 6 years ago #

    Thanks for the quick reply Kafkaesqui!

    I did test your suggestion prior to my previous post without luck. Just tried it again with a couple of variants ( <?php echo $comment->extra_commentcity; ?> )...
    It is partially true that my question is plugin specific, and I would appreciate a plugin specific answer, BUT, the more broad question still remains: "What is required to simply "echo" a particular non-wpdb database record? I would assume that this is done frequently and that there must be a simple description somewhere in the codex or in the forums... Yes?

    It just occured to me to have a look at my SQL logs to see if there was any insight there. I found the following error in my "operations" area:

    PMA Database ... not OK[ Documentation ]
    General relation features Disabled

    Since the "comments_extra" table is a seperate and distinct table in the WPDB (linked by comment_ID) it occurs to me that the above error message may be the culprit. The documentation on the error is poor AND over my head... I will chase this issue directly with my hosting service, but I would still appreciate input from the forum if there is any to be had.

    Thanks in advance!

  4. Kafkaesqui

    Posted 6 years ago #

    "What is required to simply "echo" a particular non-wpdb database record?"

    I can't answer that question because it's too broad. Or rather, I don't have the four hours it would require for me to write up one.

    It's also not a valid one in relation to your issue, because the plugin *does* save its data to the WordPress database -- just not to the same table the rest of the (default) comments data is stored. To build a custom query to collect info from it, you can either join the tables together before the comments loop starts (meaning you have to replace the default comments query with your own), or query that particular table for the comment id of each comment (meaning you'll make a call to the database for every comment, which may get pretty busy on a heavily commented site).

    Anyway, I downloaded and installed the plugin on my test server to check things out (just what I need, another plugin to work over). Note on the plugin's admin page in the "Add New Field" section, it states:

    In the comments array the index key will be extra_VARIABLENAMEYOUENTER.

    A clue! I went to the trouble of adding a new comment field, called 'foo'. Then I modified my comments form to include foo, logged out, went to a post and posted a comment. The section of my comments displaying foo looks like this:

    <?php echo $comment->extra_foo; ?>

    That's it. I've done no extra work here to display the new comment field data. And when I post my comment, it appears.

    So the question you *should* be asking has nothing to do with querying the database. Instead, what is different about your comments template? How are you going about trying to print the data on your page that is different than how I did it?

  5. hughhempel
    Member
    Posted 6 years ago #

    Hi Kafkaesqui,

    WOW! You are such a kind soul to invest so much time and energy to help... I appreciate your efforts so very much!

    You have given me several paths to pursue now that I know the "standard" query works for you...

    My comments template is attached at the bottom of this post. I am using the "paged-comments" plugin which is fantastic and really needed in my usage (guestbook)... I have the appropriate 'echo' statement stuffed into the template without formatting just to see if it will display... No luck... I also added the php 'echo' to the standard WP comment template without success...

    I am going to spend my time chasing the DB error referred to in my previous post. My suspicion is that my template and the php 'echo' call are correct, but the DB is failing to join the new table appropriately and therefore none of the records in the new _extra table can be echo'd...

    Your kindness has warmed my heart on this cold day...

    Thanks!

    -----------
    ' <?php // Do not delete these lines

    if ('comments-paged.php' == basename($_SERVER['SCRIPT_FILENAME']))

    die ('Please do not load this page directly. Thanks!');

    if (!empty($post->post_password)) { // if there's a password

    if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie

    ?>

    <p class="nocomments">This post is password protected. Enter the password to view comments.<p>

    <?php
    return;
    }

    }

    /* This variable is for alternating comment background */

    $oddcomment = 'alt';

    ?>

    <!-- You can start editing here. -->

    <h3 id="respond">Add Your Well Wishes Here:</h3>

    <?php if ( get_option('comment_registration') && !$user_ID ) : ?>

    <p>You must be /wp-login.php?redirect_to=<?php the_permalink(); ?>">logged in to post a comment.</p>

    <?php else : ?>

    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">

    <?php if ( $user_ID ) : ?>

    <p>Logged in as /wp-admin/profile.php"><?php echo $user_identity; ?>. /wp-login.php?action=logout" title="Log out of this account">Logout »</p>

    <?php else : ?>

    <p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" />

    <label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label></p>

    <p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" />

    <label for="email"><small>E-Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>

    <p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />

    <label for="url"><small>Your Website URL</small></label></p>

    <p><input type="text" name="commentcity" id="commentcity" value="<?php echo $comment_extra_commentcity; ?>" size="22" tabindex="4" />

    <label for="commentcity"><small>Your City</small></label></p>

    <p><input type="text" name="commentstate" id="commentstate" value="<?php echo $comment_extra_commentstate; ?>" size="22" tabindex="5" />

    <label for="commentstate"><small>Your State</small></label></p>

    <p><input type="text" name="commentcountry" id="commentcountry" value="<?php echo $comment_extra_commentcountry; ?>" size="22" tabindex="6" />

    <label for="commentstate"><small>Your Country</small></label></p>

    <?php endif; ?>

    <p><textarea name="comment" id="comment" cols="40%" rows="8" tabindex="7"></textarea></p>

    <p><input name="submit" type="submit" id="submit" tabindex="8" value="Send a Wish" />

    <input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />

    </p>

    <?php do_action('comment_form', $post->ID); ?>

    </form>

    <?php endif; // If registration required and not logged in ?>

    <?php if ($comments) : ?>

    <h3 id="comments"><?php comments_number('No Wishes Yet:', 'Only One Wish:', '% Wishes so far:' );?> </h3>

    <!-- Comment page numbers -->

    <?php if ($paged_comments->pager->num_pages() > 1): ?>

    <p class="comment-page-numbers"><?php _e("Pages:"); ?> <?php paged_comments_print_pages(); ?></p>

    <?php endif; ?>

    <!-- End comment page numbers -->

    <ol class="commentlist" style="list-style-type: none;">

    <?php foreach ($comments as $comment) : ?>

    <li class="<?php echo $oddcomment; ?>" id="comment-<?php comment_ID() ?>">

    <div class="comment-number">Wish# <?php echo $comment_number; $comment_number += $comment_delta;?></div>

    <cite><?php comment_author_link() ?></cite> Says:
    <?php if ($comment->comment_approved == '0') : ?>

    Your comment is awaiting moderation.
    <?php endif; ?>

    <small class="commentmetadata">" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?> <?php edit_comment_link('e','',''); ?>

    <?php echo $comment->extra_commentcity; ?></small>

    <?php comment_text() ?>

    <?php /* Changes every other comment to a different class */

    if ('alt' == $oddcomment) $oddcomment = '';

    else $oddcomment = 'alt'

    ?>

    <?php endforeach; /* end for each comment */ ?>

    <!-- Comment page numbers -->

    <?php if ($paged_comments->pager->num_pages() > 1): ?>

    <p class="comment-page-numbers"><?php _e("Pages:"); ?> <?php paged_comments_print_pages(); ?></p>

    <?php endif; ?>

    <!-- End comment page numbers -->

    <?php else : // this is displayed if there are no comments so far ?>

    <?php if ('open' == $post->comment_status) : ?>

    <!-- If comments are open, but there are no comments. -->

    <?php else : // comments are closed ?>

    <!-- If comments are closed. -->

    <p class="nocomments">Comments are closed.</p>

    <?php endif; ?>

    <?php endif; ?>

    <?php if ('open' == $post->comment_status) : ?>

    <?php endif; // if you delete this the sky will fall on your head ?> '

Topic Closed

This topic has been closed to new replies.

About this Topic