Jump to content

Recommended Posts

Hi I'm trying to create this. Can anyone help me with this?

In my Wordpress theme I added a custom field into the comment form. In this custom field the user submits a label (a string) for the post (single post) as a comment meta. This user submitted label (string - meta_value) will be added to wp_commentmeta table  as meta_key called 'et_comment_label' and the meta_value would be the label (string) submitted by the user. The label (string) that is submitted by most of the users (through the comment form) will be displayed on the post (single post) next to the post content. I need a function that does such a thing and counts which is the label that is most frequently submitted.

For example:

user 1 submits label: 'awesome'

user 2 submits label: 'worthless'

user 3 submits label: 'awesome'

user 4 submits label: 'boring'

user 5 submits label: 'awesome'

As the label 'awesome' appears 3 times so that label appears next to the post content.

Anyone can help me to create this syntax?

Link to comment
https://forums.phpfreaks.com/topic/250975-comment-meta-handling-in-wordpress/
Share on other sites

Made this simple function up for you.

If needs any more checking you can incorporate this.

 

If your word results are not an array... explode them by it's delimiter and make it an array.

 

<?php
$words = array("awesome","boring","boring","awesome","awesome","boring","awesome","boring","awesome","worthless");


function topLabel($words){
if(is_array($words)){
$unique_count = array_count_values($words);
arsort($unique_count);

while (list ($word, $value) = each ($unique_count)) {
$counted_array[] = $word; // $value
}

return $counted_array[0];
} else {
return $words;
}
}

//sample usage
echo topLabel($words);
?>

 

results:

awesome

Made this simple function up for you.

If needs any more checking you can incorporate this.

 

If your word results are not an array... explode them by it's delimiter and make it an array.

 

 

Wow  :o I never thought that someone would answer my thread, and with a solution. THANK YOU QuickOldCar. I tried it out and it works but the big task begins now is to incorporate it in Wordpress. I tried to create  a simple script which does the same thing: http://www.phpfreaks.com/forums/index.php?topic=347890.msg1641823#msg1641823 and with help it turns out that it can also be solved with MySQL query but I guess your solution is going to work better with a wordpress theme because it doesn't give the result using SQL query.

Well I'm trying here very hard. :'( I'm feeling that I'm so close to the solution but as always happens I can't figure out what's wrong.

Let me detail everything here

The custom comment form field where user submits label (etiqueta means label in Spanish) : comments.php

...<p><?php do_action( 'et_comment_form_etiqueta' ); ?></p>...

Calls function hook in additional_functions.php and rest of the functions that concerns this particular custom comment field (label=etiqueta)

...
<?php }
}
add_action('et_comment_form_etiqueta','et_choose_etiqueta');
function et_choose_etiqueta(){
#adds 'Ponle Precio' to the comment form ( frontend )
if ( !is_page() ) {
<p>Tu etiqueta:<input type="text" name="et_etiqueta" id="precio" value="aqui va tu etiqueta" size="22" tabindex="3" /></p>
}
}
add_action('comment_post','et_add_etiqueta_commentmeta', 10, 2);
function et_add_etiqueta_commentmeta( $comment_id, $comment_approved ){
#when user adds a comment, check if it's approved

$comment_etiqueta = ( isset($_POST['et_etiqueta']) ) ? $_POST['et_etiqueta'] : 0;
add_comment_meta($comment_id,'et_comment_etiqueta',$comment_etiqueta);
if ( $comment_approved == 1 ) {	
	$comment_info = get_comment($comment_id);		
	et_update_post_user_etiqueta( $comment_info->comment_post_ID );
}
}
add_action('comment_post','et_add_etiqueta_commentmeta', 10, 2);
function et_add_etiqueta_commentmeta( $comment_id, $comment_approved ){
#when user adds a comment, check if it's approved

$comment_etiqueta = ( isset($_POST['et_etiqueta']) ) ? $_POST['et_etiqueta'] : 0;
add_comment_meta($comment_id,'et_comment_etiqueta',$comment_etiqueta);
if ( $comment_approved == 1 ) {	
	$comment_info = get_comment($comment_id);		
	et_update_post_user_etiqueta( $comment_info->comment_post_ID );
}
}
add_action('et-comment-meta-etiqueta','et_show_comment_etiqueta');
function et_show_comment_etiqueta( $comment_id ){
#displays user comment etiqueta on single post page ( frontend )

$user_comment_etiqueta = get_comment_meta($comment_id,'et_comment_etiqueta',true) ? get_comment_meta($comment_id,'et_comment_etiqueta',true) : 0;				
if ( $user_comment_etiqueta <> 0 ) { ?>

                                    <?php echo $user_comment_etiqueta; ?>

<?php }
}
function et_get_top_etiqueta($top_etiqueta) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) as countof FROM $wpdb->commentmeta WHERE meta_key = 'et_comment_etiqueta' ORDER BY countof DESC LIMIT 1", $post_id));
}
function et_get_post_user_etiqueta( $post_id ){
#gets under process user (comments) added etiqueta for the post
        #this function adds the most popular etiqueta (label) to the post. (it needs to be calculated)
$approved_comments = et_get_approved_comments( $post_id );
if ( empty($approved_comments) ) return 0;
$user_etiqueta = 0;
$approved_comments_number = count($approved_comments);
foreach ( $approved_comments as $comment ) {
	$comment_etiqueta = get_comment_meta($comment->comment_ID,'et_comment_etiqueta',true) ? get_comment_meta($comment->comment_ID,'et_comment_etiqueta',true) : 0;
	if ( $comment_etiqueta == 0 ) $approved_comments_number--;

	$user_etiqueta += $comment_etiqueta;
}
        //we need a variable where the most times submitted label is stored.
        $result = et_get_top_etiqueta( $top_etiqueta );
# save user rating to the post meta

if ( !get_post_meta($post_id,'_et_inreview_comments_etiqueta',true) ) update_post_meta($post_id,'_et_inreview_comments_etiqueta',$result);

return $result;
}
function et_update_post_user_etiqueta( $post_id ){
#update user added etiqueta for the post
$new_comments_etiqueta = et_get_post_user_etiqueta( $post_id );

if ( get_post_meta($post_id,'_et_inreview_comments_etiqueta',true) <> $new_comments_etiqueta )
	update_post_meta($post_id,'_et_inreview_comments_etiqueta',$new_comments_etiqueta);
}
function et_get_approved_comments($post_id) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' ORDER BY comment_date", $post_id));
}
?>...

And this is the where the label (etiqueta) is supposed to appear in the comment box next to each comment submitted by users but IT DOESN'T  :confused: The label submitted by each user gets inserted in the database correctly but it just doesn't get displayed next to the comment in the comment box

<b>Etiqueta:</b> <?php do_action('et-comment-meta-etiqueta', get_comment_ID());?>

In order to display the most frequently submitted label (etiqueta) next to the post I used this function which can also be found in additional_functions.php above IT DOESNT WORK EITHER!!

<?php add_action('et-comment-meta-etiqueta','et_show_comment_etiqueta');
function et_show_comment_etiqueta( $comment_id ){
#displays user comment etiqueta on single post page ( frontend )

$user_comment_etiqueta = get_comment_meta($comment_id,'et_comment_etiqueta',true) ? get_comment_meta($comment_id,'et_comment_etiqueta',true) : 0;				
if ( $user_comment_etiqueta <> 0 ) {

                                     echo $user_comment_etiqueta;

 }
}
function et_get_top_etiqueta($top_etiqueta) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) as countof FROM $wpdb->commentmeta WHERE meta_key = 'et_comment_etiqueta' ORDER BY countof DESC LIMIT 1", $post_id));
}?>

This are the tables that uses the theme:

-wp_commentmeta which has 4 columns: meta_id; comment_id; meta_key; meta_value

-wp_postmeta which has 4 columns: meta_id; post_id; meta_key; meta_value

I also attached the theme files that the comment form uses.

 

 

 

 

[attachment deleted by admin]

Ok there is a  :) good news and a  :( bad news. The label (etiqueta) appears now in the comment box. The problem was that it passed it as an integer and it is a string (a word) actually. Only the last and the most important problem to solve left is to display the most frequently submitted label (etiqueta) next to the post. I created a function that retrieves all the labels and counts them.

<?php function et_get_post_user_etiqueta( $post_id ){
global $wpdb;    
$result = $wpdb->get_results($wpdb->prepare("SELECT meta_value, count(*) FROM $wpdb->wp_commentmeta WHERE meta_key ='et_comment_etiqueta' GROUP BY meta_value DESC LIMIT 1", $post_id));
if ( !get_post_meta($post_id,'_et_inreview_comments_etiqueta',true) ) update_post_meta($post_id,'_et_inreview_comments_etiqueta',$result);

return $result;
}?>

 

The only problem it outputs: 'Array' so I'm still struggling.

 

[attachment deleted by admin]

You can do a var dump to see what the array is and how to handle it

http://www.php.net/manual/en/function.var-dump.php

 

or print_r the array to see what it contains

http://php.net/manual/en/function.print-r.php

You can do a var dump to see what the array is and how to handle it

http://www.php.net/manual/en/function.var-dump.php

 

or print_r the array to see what it contains

http://php.net/manual/en/function.print-r.php

 

I tried to output:

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;
}

in comment.php inserted:

 <?php do_action( 'show_etiqueta' ); ?>

and it gives me 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 ) )  

As you can see I changed the sql query:

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

and I tested in phpmyadmin to see what result it gives me and it gave me the right result:

 

 

still  :-\ . I don't know what do do.

 

This is the last thing I can come up with:

<?php function et_get_etiqueta_value($post_id) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare("SELECT meta_value FROM $wpdb->commentmeta WHERE meta_key='et_comment_etiqueta'ORDER BY comment_ID", $post_id));
}
add_action('show_etiqueta','et_get_post_user_etiqueta');
function et_get_post_user_etiqueta( $post_id ){
    $approved_comments = et_get_approved_comments( $post_id );
if ( empty($approved_comments) ) return 0;
$user_etiqueta = 0;
$approved_comments_number = count($approved_comments);
        foreach ( $approved_comments as $comment ) {
	$comment_etiqueta = get_comment_meta($comment->comment_ID,'et_comment_etiqueta',true) ? get_comment_meta($comment->comment_ID,'et_comment_precio',true) : 0;
	if ( $comment_etiqueta == 0 ) $approved_comments_number--;
	$user_etiqueta += $comment_etiqueta;
}
        //this where it gets the top 1 word
        $result = et_get_etiqueta_value($post_id);
        if(is_array($result)){
        $unique_count = array_count_values($result );
        arsort($unique_count);

        while (list ($word, $value) = each ($unique_count)) {
        $counted_array[] = $word; // $value
        }

        return $counted_array[0];
                            } else {

        if ( !get_post_meta($post_id,'_et_inreview_comments_precio',true) ) update_post_meta($post_id,'_et_inreview_comments_precio',$result);
        
        }

return $result;
    
}?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.