nappyhead Posted January 29, 2011 Share Posted January 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226023-problem-with-my-voting-module/ Share on other sites More sharing options...
l4nc3r Posted January 29, 2011 Share Posted January 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226023-problem-with-my-voting-module/#findComment-1166972 Share on other sites More sharing options...
nappyhead Posted January 29, 2011 Author Share Posted January 29, 2011 Thank you. I'll remember to label it a Drupal module next time. I'll use your advice to make the fix and report back if my problem is resolved. Quote Link to comment https://forums.phpfreaks.com/topic/226023-problem-with-my-voting-module/#findComment-1166990 Share on other sites More sharing options...
nappyhead Posted January 29, 2011 Author Share Posted January 29, 2011 That helped. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/226023-problem-with-my-voting-module/#findComment-1167001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.