As in Title. I am struggling by programming but with no luck. Mechanism of voting api is too complicated for me. How can I display current node rank (order number) by votes?

My view now:

Sort critera: (Vote results) Vote results: Function desc

Relationships Node: Vote results

When displaying all results it's have order number thanks to view php field module.

Display position in rank of current, single node is hell for me. Custom sql query doesn't work at all.

I am using voting api and rate module.

Please help.


Since I am using +1/-1 I try to:

  1. Sum +1 Votes
  2. Sum -1 Votes
  3. Subtract sum of -1 votes from sum of +1


$plus_rates = db_query("SELECT count(value) AS sum, content_id AS nid FROM votingapi_vote WHERE tag = 'vote' AND value_type = 'points' AND value = 1 GROUP BY content_id ORDER BY sum DESC, nid ASC");
                                         $minus_rates = db_query("SELECT count(value) AS sum, content_id AS nid FROM votingapi_vote WHERE tag = 'vote' AND value_type = 'points' AND value = -1 GROUP BY content_id ORDER BY sum DESC, nid ASC");

                     $pluses = array();
                                         $minuses = array();

                     while ( $row = db_fetch_object($plus_rates) ) {

                                            $pluses[$row->nid] = $row->sum;         


                                         $plus_rates = NULL;

                                         while ( $row = db_fetch_object( $minus_rates ) ) {

                                            $minuses[$row->nid] = $row->sum;


                                         $minus_rates = NULL;

                                         $keys = array_keys($minuses);

                                         foreach($keys as $k) {

                                                if (array_key_exists($k, $pluses)) {

                                                        $pluses[$k] = $pluses[$k] - $minuses[$k];



                                         arsort($pluses, SORT_NUMERIC);

                                         firep($pluses, 'pluses');

$pluses is sorted DESC but order is totally different from my view display:

  • Sort cirteria: (Vote results) Vote results: Function desc)
  • Relationship Node: Vote results, Aggregation function: total score


I try to query votingapi_cache directly but still order is totally different than in views:

SELECT value, content_id FROM votingapi_cache WHERE value_type = 'points' AND tag = 'vote' AND FUNCTION = 'sum' ORDER BY value DESC LIMIT 0 , 30

3 Answers 3


Check the comments on this question: Problem visualizing the rating value inside a view

It sounds like you probably haven't set an aggregate function on the Node: Vote Results relationship. You cab definitely display the total votes in a View without writing any of your own code.

  • Thanks. I use aggregation function. My problem is to display order number of current node, such as: Rank: 9. If I have to display for example 10 first, I use a view with row number field. In this case I can't use this solution
    – Codium
    Commented Jul 21, 2011 at 1:50
  • oh, so i guess i misunderstood a little. You don't want to show the vote total, you want to show the position within the list based on the total votes. So, you have nodes A, B, C and D with vote counts 5, 10, 6, 9 and you want to show it as A:Rank 4, B:Rank 1, C:Rank 3 and D:Rank 2 (10 being the highest rank, 5 being the lowest). Is that correct? Do you want them sorted by rank as well?
    – Chaulky
    Commented Jul 21, 2011 at 2:01
  • Yes, on a page with single A or B etc. All night fighting and nothing :/
    – Codium
    Commented Jul 21, 2011 at 3:46

votingapi_vote is not a table which you can access directly in views. If you use the data module, you should be able to make it accessable to views and create a view on it directly. It will be at a lower level than what you do through nodes but may be simpler for you.

  • Thanks I'll try it. Unlucky I can't download it right know
    – Codium
    Commented Jul 21, 2011 at 11:09

Ok I made it,

First View settings (View for main rating listing):

Sort criteria:

(Vote results) Vote results: Value desc, Node: Nid asc


$result = db_query("SELECT c.content_id AS nid, c.value AS sum
                                                                FROM votingapi_cache AS c
                                                                INNER JOIN node AS n ON c.content_id = n.nid
                                                                WHERE c.value_type = 'points'
                                                                AND c.tag = 'vote'
                                                                AND c.function = 'sum'
                                                                AND n.type = 'profile'
                                                                AND n.status = 1
                                                                ORDER BY sum DESC , nid ASC
                           $is_rated = FALSE; 

                                     while ($row = db_fetch_object($result)) {


                                         if ($profile->nid == $row->nid) {

                                            $is_rated = TRUE;



In real it should be turn to temp table and cache too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.