Jump to content

Problem with my voting module


nappyhead

Recommended Posts

This is a node voting module I created and it has a slight problem that I cannot figure out.

It's supposed to be a simple 'Vote Up' or 'Vote Down' feature. You click 'Vote Up' and one appears in your vote tally. You click 'Vote Down' and your original vote tally is subtracted by one back down to zero.

 

When it goes back to zero, it gets stuck there. It stays on zero no matter if I choose 'Vote Up' or 'Vote Down' again.

 

There's something that's happening when I move my vote tally back down to zero. Maybe something isn't setup correctly with the $current_votes variable. I don't know.

 

I've been at this for hours. Here's the code:

 

<?php

function bonnier_vote_perm() {
  return array('vote on content');
}

function bonnier_vote_menu() {
  $items = array();
  $items['vote'] = array(
    'title' => 'vote',
    'page callback' => 'bonnier_vote_vote',
    'access arguments' => array('vote on content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function bonnier_vote_vote() {
  $nid = $_REQUEST['nid'];
  $value = $_REQUEST['votes'];
  
  $current_votes = db_result(db_query("SELECT votes FROM bonnier_vote WHERE nid = $nid"));
  
  if ($current_votes) {
    $new_votes = $current_votes + $value;
    db_query("UPDATE bonnier_vote SET votes = $new_votes WHERE nid = $nid");
  }
  else {
    db_query("INSERT INTO bonnier_vote (nid, votes) VALUES ($nid, $value)");
  }
  
  drupal_set_message('Your vote has been recorded');
  drupal_goto('node/'.$nid);
}

function bonnier_vote_nodeapi(&$node, $op, $teaser = null, $page = null) {
  if ($op == 'view' && !$teaser) {
    $votes = db_result(db_query("SELECT votes FROM bonnier_vote WHERE nid = {$node->nid}"));
    if (!$votes) {
      $votes = 0;
    }
    
    $widget  = '<div>';
    $widget .= l('Vote Up', 'vote', array('query' => array('nid' => $node->nid, 'votes' => 1)));
    $widget .= ' ';
    $widget .= l('Vote Down', 'vote', array('query' => array('nid' => $node->nid, 'value' => -1)));
    $widget .= ' ';
    $widget .= 'Score: '. $votes;
    $widget .= '</div>';
    
    $node->content['vote_widget'] = array(
      '#value' => $widget,
      '#weight' => -10,
    );
  }
}

 

Any bit of advice would be much appreciated.

Link to comment
Share on other sites

You might've wanted to make it clear this was Drupal in the subject--I didn't know until I saw the "l" function, which I had to look up.

 

Also, using $_REQUEST is a bad idea.  You should change it to $_GET.

 

So when you get data back from a database, it's ALWAYS in string form (unless you do prepared statements).  So, with that in mind, the problem is in this code:

 

  if ($current_votes) {
    $new_votes = $current_votes + $value;
    db_query("UPDATE bonnier_vote SET votes = $new_votes WHERE nid = $nid");
  }
  else {
    db_query("INSERT INTO bonnier_vote (nid, votes) VALUES ($nid, $value)");
  }

 

$current_votes is always going to return true (even if it's "0") because it's a string and therefore has value.  What I think you want is this:

 

  $current_votes = (int)$current_votes;
  if ($current_votes + $value >= 0) {
    $new_votes = $current_votes + $value;
    db_query("UPDATE bonnier_vote SET votes = $new_votes WHERE nid = $nid");
  }

 

But that will only prevent it from going below 0.  If you want it to, after it's been voted up and then down again to be prevented from being voted again, you would have to add some sort of check into the database.  So when it's voted up, you would update the "voted" column to true for that vote.  You'd allow it to go up and down, I'm assuming, as much as you want until it was 0 again and "voted" was true.  Then no more voting would be allowed.

Link to comment
Share on other sites

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.