dtwvr26066 2010-08-25 15:29
浏览 36

跟踪“向上”和“向下”投票的方法

I have a website that picks an entry from a database at random and then displays it to the user. I would like to add a "top rated" page on which the best entries as chosen by an aggregate of user opinions are displayed. I want up votes and down votes as seen here on stackoverflow and on many other websites.

I am not very experienced with web development, so I'm at a loss about where to start. The idea I have is that clicking "up" will add 1 to the "votes" field in a table and clicking "down" will subtract one from the same field, and I'll tie it all together with an "id" field that references on which entry is being voted. Is there a more elegant solution? How do I get started?

edit: my server side scripting is in PHP and I'm familiar with jquery.

edit 2: Upon rereading, I realize how broad this question is. Sorry! Specifically, I want to know how to code the PHP and mySQL necessary to add and subtract votes to and from the "votes" table without refreshing the page.

  • 写回答

2条回答 默认 最新

  • dpict99695329 2010-08-25 15:33
    关注

    You'll notice that SO remembers which articles you voted on, and how you voted, so there is a table that ties together the item being voted on, the person doing the voting, and what their vote was. An aggregate function in SQL can be used to tally the number of votes from this joining table.

    Note that if the function you use to calculate your "top items" is expensive (i.e. if you have a lot of items, etc.) then you may need to modify your query and will probably want to cache the results for a reasonable time period before doing the calculation again.

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)