473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Simple thumb up / down vote system

HaLo2FrEeEk
404 256MB
I'm working on a system where users will submit comments about a topic, and that comment can be voted on with a thumb up or down. I've got a table for comments, each comment will have it's own unique ID, and I've got a table to track votes as well. I'm just wondering what would be the best way to accomplish this. I'm thinking using a boolean as the actual vote, so that a 1 is a thumb up and a 0 is a thumb down. Is there a better way though?

Also, when I'm displaying the votes on the page, should I run two separate queries to count the rows where the votes equal 0 and then 1, or is there a better way to do it so that I only have to do one query?

Edit: Just thought of this. Would a better way be to get rid of the votes table and add two new columns to the comments table: votes_up and votes_down, that way I can get the comment itself, plus the vote status all in one query. With this method, what would be the best way to order the comments, with higher rated comments coming first (the rating being the ratio of votes up to votes down.) Is there a way to do THAT wth MySQL of would it have to be done with PHP?
Mar 19 '11 #1
2 2433
dgreenhouse
250 Expert 100+
I can definitely see the efficiency of adding the votes columns to the comments table.

If you keep the votes table separate, this query should work:
Expand|Select|Wrap|Line Numbers
  1. select 
  2. count(case when vote = 1 then 1 else null end) as upvotes,
  3. count(case when vote = 0 then 1 else null end) as dnvotes
  4. from votes
  5.  
Mar 26 '11 #2
HaLo2FrEeEk
404 256MB
I actually worded this a little wrong to avoid the obligatory long explanation of what I was trying to do. There are challenges, which people will submit tips for. Others can comment on the tips, or vote on them, but they don't have to do either one together, you can vote without commenting and vice versa. I did get a great query that would get the votes and tips in a single query though, it uses the same CASE WHEN code to count the up and down votes, then subtracts the up fro the down votes to get a rating and sorts it by that, highest first. The comments still have to be gotten separately though, but I figured it all out. Thanks.
Mar 26 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: max | last post by:
Hi! I'm having a problem with a very simple drop-down list since the page comes out with no elements in the drop-down but giving no errors. This is the code: <form name="year_search_form"...
0
by: yatharth | last post by:
Hi Mike I have Datagrid and i am fetching data around about 1 million into it , i think which is not good deal this makes my system slow.Please tell the way so that i can fetch data in my...
3
by: BrianDH | last post by:
Hi I have a VB.Net windows application that for some reason when running will not allow the user to shut-down/reboot the Operating System without first closing my application. Has anyone had the...
2
by: vna | last post by:
Hello Everyone, Please send me the code for a system to automatically shutting down the system by specifying the time.Can use either C# or ASP.Net. Please help me asap. Regards, Vna
1
by: Hardik Shah | last post by:
Hi, I want to shut down my xp/server 2003 system throu code, can any one help me. Hardik Shah
9
by: snipersix | last post by:
Anyone know how to set up a simple ID and password system in access. So that when someone login, they either have access to the database window or not. like administrators, and managers login do, and...
4
by: Thomas Magma | last post by:
I must be blind, I am developing in MS Visual Studio 2008 Beta2 and can't seem to find a simple drop down menu in the tool box. I see a ComboBox, a MenuStrip and a ContextMenuStrip but no simple...
41
by: sukatoa | last post by:
This is for my friend. He is a HRM student and was assigned to demo on how to operate a Simple Hotel front-desk system. I've searched on the net to help him, but all of those freeware softwares...
5
by: dmj07 | last post by:
Can anybody help me with a simple file uploading and retrieving idea? What I need is a simple system that uploads the files to be stored in a SQL database which are then shown on the front end in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.