PHP, MYSQL, jQuery for "top" and "Stomp" voting function

Source: Internet
Author: User
Tags get ip stomp
    1. CREATE TABLE IF not EXISTS ' votes ' (
    2. ' id ' int (ten) is not NULL auto_increment,
    3. ' Likes ' int (ten) not NULL DEFAULT ' 0 ',
    4. ' Unlikes ' int (ten) not NULL DEFAULT ' 0 ',
    5. PRIMARY KEY (' id ')
    6. ) Engine=myisam DEFAULT Charset=utf8;
    7. INSERT into ' votes ' (' id ', ' likes ', ' Unlikes ') VALUES
    8. (1, 30, 10);
    9. CREATE TABLE IF not EXISTS ' votes_ip ' (
    10. ' id ' int (ten) is not NULL,
    11. ' Vid ' int (ten) is not NULL,
    12. ' IP ' varchar (+) not NULL
    13. ) Engine=myisam DEFAULT Charset=utf8;
Copy Code


2,html page section in the page, there are two "top" and "stepping" buttons, namely #dig_up and #dig_down, the button on the record number of votes and the percentage of each, very intuitive comparison of voting results.


    1. Very good, very powerful!

    2. Oh, that sucks!

Copy Code


The 3,CSS code section must use CSS to beautify the page, use a graph diggs.png to position the different button backgrounds, and set the position to position the relationship between the elements.


  1. . digg{width:420px; height:120px; margin:80px auto 20px auto; position:relative}
  2. #dig_up, #dig_down {width:200px; height:48px; margin:10px; position:relative;
  3. border:1px solid #d3d3d3; padding-left:42px; Cursor:pointer}
  4. . Digup{background:url (diggs.png) no-repeat 4px 2px;}
  5. . Digup_on{background:url (Diggs.png) no-repeat 4px-49px;}
  6. . Digdown{background:url (Diggs.png) no-repeat 4px-102px;}
  7. . Digdown_on{background:url (Diggs.png) no-repeat 4px-154px;}
  8. #num_up, #num_down {position:absolute; right:6px; top:18px; font-size:20px;}
  9. #dig_up p{height:24px; line-height:24px; color: #}
  10. #dig_down p{height:24px; line-height:24px; color: #f30}
  11. . bar{width:100px; height:12px; line-height:12px; border:1px solid #f0f0f0;
  12. position:relative; Text-align:center}
  13. . bar Span{display:block; height:12px;}
  14. . bar I{position:absolute; top:0; left:104px;}
  15. #bar_up span{background: #}
  16. #bar_down span{background: #f60}
  17. #msg {position:absolute; right:20px; top:40px; font-size:18px; color: #f00}
Copy Code


2,jquery part of this example also relies on jquery, so you must not forget to load the jquery library file in the page first. First of all, jquery has to deal with the background image that the mouse slides to two voting buttons, respectively, by AddClass () and Removeclass ().


    1. $ (function () {
    2. Change the background style when the mouse slides and leaves the voting button
    3. $ ("#dig_up"). Hover (function () {
    4. $ (this). addclass ("digup_on");
    5. },function () {
    6. $ (this). Removeclass ("digup_on");
    7. });
    8. $ ("#dig_down"). Hover (function () {
    9. $ (this). addclass ("digdown_on");
    10. },function () {
    11. $ (this). Removeclass ("digdown_on");
    12. });
    13. Initializing data
    14. GetData ("do.php", 1);
    15. When you click "Top"
    16. $ ("#dig_up"). Click (function () {
    17. GetData ("Do.php?action=like", 1);
    18. });
    19. When you click on "Step"
    20. $ ("#dig_down"). Click (function () {
    21. GetData ("Do.php?action=unlike", 1);
    22. });
    23. });
Copy Code


Then, the data is initialized, that is, after the page is loaded, the initial voting results are displayed, including the number of votes and the percentage. The operation to get the data is written in a custom function, GetData (), which completes the loading of the data by passing different request addresses and ID parameters. In function GetData (), an AJAX request is sent to the URL, based on the returned results of the background processing, if the vote succeeds, the corresponding element content in the page is changed, including the number of votes and the percentage.


    1. function GetData (URL,SID) {
    2. $.getjson (url,{id:sid},function (data) {
    3. if (data.success==1) {//vote succeeded
    4. $ ("#num_up"). HTML (data.like);
    5. Show percent progress bar effect by controlling width
    6. $ ("#bar_up span"). CSS ("width", data.like_percent);
    7. $ ("#bar_up i"). html (data.like_percent);
    8. $ ("#num_down"). HTML (data.unlike);
    9. $ ("#bar_down span"). CSS ("width", data.unlike_percent);
    10. $ ("#bar_down i"). html (data.unlike_percent);
    11. }else{//vote failed Bbs.it-home.org
    12. $ ("#msg"). HTML (DATA.MSG). Show (). CSS ({' Opacity ': 1, ' top ': ' 40px '})
    13. . Animate ({top: ' -50px ', opacity:0}, "slow");
    14. }
    15. });
    16. }
Copy Code


4,php code part of the data is obtained through do.php to complete, do.php according to the front-end page passed parameters, connected to the database, according to the criteria to enter the "top", "stepping" and the initial data processing module, the following is the Do.php module code structure:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.