Average Score of all ratings in comments

i have a rating system in comments, for each comment user can rate 1-5 Stars.

Now , i want the average of all ratings in comments of that post to show.

How can i do that.

Here is the link to the code file related to My rating System. Current Users can rate and comment. But i want the average of all comment ratings in that post. And this is a custom Post type.

check this link for the code related to rating used in the site.

http://pastebin.com/LcRyjSPK

Answer

add this code into your plugin file to register activation hook

// register activation hook
register_activation_hook( __FILE__, 'my_rating_plugin_activation' );

now create a function and add your table creation code into

function my_rating_plugin_activation() {
    global $wpdb, $rating_table_name;
    if($wpdb->get_var("SHOW TABLES LIKE \"$rating_table_name\"") != $rating_table_name) {
        $wpdb->query("CREATE TABLE IF NOT EXISTS $rating_table_name (
            rating_id int(11) NOT NULL AUTO_INCREMENT,
            rating_postid int(11) NOT NULL,
            rating_posttitle text NOT NULL,
            rating_rating int(2) NOT NULL,
            rating_timestamp varchar(15) NOT NULL,
            rating_ip varchar(40) NOT NULL,
            rating_host varchar(200) NOT NULL,
            rating_username varchar(50) NOT NULL,
            rating_userid int(10) NOT NULL DEFAULT '0',
            comment_id int(11) NOT NULL,
            PRIMARY KEY (rating_id)
        ) ENGINE=MyISAM");
    }
}

Attribution
Source : Link , Question Author : luckyrajiv , Answer Author : Anjum

Leave a Comment