Title: [Plugin: NextGEN Gallery Voting] Database Values &amp; Clear Votes function
Last modified: August 20, 2016

---

# [Plugin: NextGEN Gallery Voting] Database Values & Clear Votes function

 *  [Wookiee](https://wordpress.org/support/users/wookiee/)
 * (@wookiee)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/)
 * Hey Shauno.
 * I’m trying to run some queries and i’d just like to double check something.
 * **Question 1:**
    wp_nggv_votes: – Does the values of images that has been voted
   for get set to 100? – Or is the value dependent on the settings? Example -if 
   you can only vote once, vote = 100 -if you can vote more than once vote = 100-
   if you cant vote at all, vote = 0
 * What does the vote value of 100 mean?
 * The amount of times an image appears with vote set to 100, is clearly the amount
   of votes that image has for it. (based on my voting settings)
 * **Question 2:**
    When I clear all votes (via admin), there is still instances
   left of previous votes from deleted galleries.
 * Is there a way to delete votes on previous galleries that doesn’t exist anymore
   without deleting them manually from the db?
 * **Question 3:**
    How hard would it be to add an extra feature to enable users
   to change their vote once it has been cast from the front end?
 * Example, undo your vote, clickable link, and enables them to vote again. Not 
   the same as being able to vote as many times as you like.
 * Thanks man
    Wookiee
 * [http://wordpress.org/extend/plugins/nextgen-gallery-voting/](http://wordpress.org/extend/plugins/nextgen-gallery-voting/)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  Thread Starter [Wookiee](https://wordpress.org/support/users/wookiee/)
 * (@wookiee)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876491)
 * Hey Shauno
 * I am trying to create a dynamic button to delete the vote I cast on an image 
   for a specific gallery from the front end, according to some info I found here:
   
   [http://wordpress.org/support/topic/wpdb-how-can-i-make-text-link-to-delete-rows-in-two-tables?replies=2](http://wordpress.org/support/topic/wpdb-how-can-i-make-text-link-to-delete-rows-in-two-tables?replies=2)
 * I have done the following:
    In my templates functions.php
 *     ```
       function delete_my_vote_func($user, $gid)
       	{
       	global $wpdb;
       	$wpdb->query( " DELETE votes.* FROM wp_nggv_votes AS votes
       					LEFT JOIN wp_ngg_pictures AS pics
       					ON votes.pid = pics.pid
       					WHERE votes.user_id = ".$wpdb->escape($user)."
       					AND pics.galleryid = ".$wpdb->escape($gid));
       	}
       ```
   
 * Then from another function, dynamically, the user_id and gallery_id he currently
   is viewing is sent to the function as follow:
 * `$link_remove = '<a href=""><input type="submit" name="delete_vote_button" value
   =" Delete Vote " onclick="<?php delete_my_vote_func('.$user.','.$i.'); ?>" />
   </a>';`
 * Where $i is the gallery_id he is currently viewing.
    Where $user is the current
   user_id. The parameters are sent & received correctly (I double checked)
 * The link is then displayed on the front, i just didn’t copy my whole function
   in here.
 * Link does not delete any records though. Do you know how this would be possible
   to fix and how safe would this be to enable users to delete their votes from 
   the front end?
 *  Plugin Author [shauno](https://wordpress.org/support/users/shauno/)
 * (@shauno)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876513)
 * Let me try answer these as best as possible:
 * **Question 1:** Each time some one votes, a row is added to `wp_nggv_votes`. 
   The `vote` column represents what the user voted (out of 100). So, if the user
   chose 3/5 stars, the vote=60. If the user voted 7 out of 10 drop down, vote=70.
   But, for like/dislike, a like=100, and a dislike=0.
 * **Question2:** Clearing votes is per image or gallery. There currently isn’t 
   a global clear-all-votes-ever feature. I will add it to the todo list.
 * **Question 3:** Pretty hard, and outside of the scope of the plugin I think (
   at least for now)
 * **Second post:** The first thing I notice is you are trying to call a php function
   via a javascript click event. That’s just not going to happen. You need to call
   a specific page and run your func, or call the page via ajax. You need to understand
   how PHP interacts with the server, and JS with the client.
 * It seems like you really are pushing the limits of what this plugin was built
   to do. As far as I see it, you need to scale back what you are trying to achieve
   to fit the plugin, or get a proper developer to write the functionality you are
   looking for (that might mean editing the plugin, or possibly starting from scratch
   with your spec).
 * I wish I could offer some better solutions, but this is the best I can do right
   now 🙂
 * PS. Feel free to keep asking questions, there might be something you need that’s
   already possible (or easy to add).
 *  Thread Starter [Wookiee](https://wordpress.org/support/users/wookiee/)
 * (@wookiee)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876515)
 * Thanks for explaining the vote system database end. It makes sense now, I did
   not run any star rating related tests (thats my bad).
 * You have done an awesome job with the plugin. It’s got all the basics(and more)
   to be the best voting/rating plugin out there. I sincerely mean that.
 * The delete function.
    I just thought that would be good in most situations. Like
   when a new image gets uploaded, but you only want a user to be able to have one
   vote in a gallery. Then those users that already voted in that gallery can not
   vote for any new images. Unless… he can change his vote.
 * Anyway. Im not giving up, I was clearly the one sketching images on my php handbook
   back in UNI… but im sure my brain must have heard something that was going on
   in class!
 * Thanks Shaono!
 * PS. It seems you have something in mind I havent thought about :p
 *  Thread Starter [Wookiee](https://wordpress.org/support/users/wookiee/)
 * (@wookiee)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876539)
 * Hi Shauno
 * I see the gid (gallery id) in ngg_votes is inserted only when you vote on a gallery.
   If you are voting on an image, it is set to ‘0’ (zero)?
 * This actual gallery id $config[“gid”] is probably only set for gallery voting
   for running other queries to see when a gallery has been voted for?
 * Is this the only way to check if a gallery has any votes? If there is actually
   a gid thats not == 0?
 * I know this requires some tweaks, but wouldn’t it be “better” for lack of a better
   term, if the gid got inserted whether it was a vote for an image or gallery, 
   and having an extra database column like type, where type = 1 means its a gallery
   vote, and type = 0 means its an image vote?
 * That way running queries on what you actually have in your ngg_votes can be easier?
 * The reason im asking is because i actually managed to get a very basic delete
   button working, but it only works on the user_id. So it deletes ALL votes from
   that specific user in the ngg_votes table.
 *  Plugin Author [shauno](https://wordpress.org/support/users/shauno/)
 * (@shauno)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876540)
 * Ya, the db schema isn’t ideal. It’s because the plugin was written with image
   voting only originally, and then updated to include gallery voting.
    The problem
   is legacy data now. I can’t just change the way something works without updating
   all the data to conform for old users. That’s more effort than it’s worth for
   me right now 🙂
 * If you are trying to delete a users vote (for an image) in a gallery, you first
   need to get all the image id’s from that gallery (I’m sure there’s a random NGG
   API for that). Then you can `delete from wp_ngg_votes where userid = [their id]
   AND pid IN (list, of, image, ids)`.
 *  Thread Starter [Wookiee](https://wordpress.org/support/users/wookiee/)
 * (@wookiee)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876569)
 * Thanks Shauno.
 * Got it all set up and working, boy was it a mission.
 * If anyone wants to see the query, i got it working like this:
 *     ```
       if($wpdb->query("DELETE FROM ".$wpdb->prefix."nggv_votes WHERE user_id = ".$wpdb->escape($user_id)." AND pid IN ( SELECT pid FROM ".$wpdb->prefix."ngg_pictures WHERE galleryid = ".$wpdb->escape($gallery_id)." )") !== false)
   
       {
        //It has been deleted, celebrate!
       }
   
       else
       {
        //It has not been deleted, oh no!
       }
       ```
   
 * Where the galleryid and user_id is both sent to my function that deletes the 
   vote. This is only the query part of things… lots of other code left out. The
   query only executes with $wpdb->prefix. I have no idea why as manually using 
   the prefix just results in it not being deleted…
 * Big *sigh*
 * Thanks again for the query tips! I just hope this is secure enough! lol
 *  Plugin Author [shauno](https://wordpress.org/support/users/shauno/)
 * (@shauno)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876570)
 * Nicely done, I’m glad you’ve got it working.
    Using the prefix is the right thing
   to do, so be happy it works with it 🙂 Also, it seems secure enough, as you are
   running the variables through escape(), so I wouldn’t stress about it.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘[Plugin: NextGEN Gallery Voting] Database Values & Clear Votes function’
is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/nextgen-gallery-voting.svg)
 * [NextGEN Gallery Voting](https://wordpress.org/plugins/nextgen-gallery-voting/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/nextgen-gallery-voting/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/nextgen-gallery-voting/)
 * [Active Topics](https://wordpress.org/support/plugin/nextgen-gallery-voting/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/nextgen-gallery-voting/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/nextgen-gallery-voting/reviews/)

 * 7 replies
 * 2 participants
 * Last reply from: [shauno](https://wordpress.org/support/users/shauno/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/plugin-nextgen-gallery-voting-database-values-clear-votes-function/#post-2876570)
 * Status: not resolved