PHP + MySql + jQuery implements "top" and "Step On" voting functions-PHP source code

Source: Internet
Author: User
The "top" and "stepping on" voting functions implemented by PHP + MySql + jQuery jump to [1] [full screen preview]
Create table if not exists 'votes '('id' int (10) not null AUTO_INCREMENT, 'lik' int (10) not null default '0 ', 'unlik' int (10) not null default '0', primary key ('id') ENGINE = MyISAM default charset = utf8; insert into 'votes '('id ', 'likes ', 'unlikes') VALUES (1, 30, 10); create table if not exists 'votes _ IP' ('id' int (10) not null, 'vid' int (10) not null, 'IP' varchar (40) not null) ENGINE = MyISAM default charset = utf8; --------------- p --------------------------------------

Very good, very powerful!

Too bad!

------------- Css --------------------------. digg {width: 420px; height: 120px; margin: 80px auto 20px auto; position: relative} # dig_up, # dig_down {width: 200px; height: 48px; margin: 10px; position: relative; border: 1px solid # d3d3d3; padding-left: 42px; cursor: pointer }. digup {background: url(diggs.png) no-repeat 4px 2px ;}. digup_on {background: url(diggs.png) no-repeat 4px-49px ;}. digdown {background: url (diggs. p Ng) no-repeat 4px-102px ;}. digdown_on {background: url(diggs.png) no-repeat 4px-154px ;}# num_up, # num_down {position: absolute; right: 6px; top: 18px; font-size: 20px ;} # dig_up p {height: 24px; line-height: 24px; color: #360} # dig_down p {height: 24px; line-height: 24px; color: # f30 }. bar {width: 100px; height: 12px; line-height: 12px; border: 1px solid # f0f0f0; position: relative; text-align: center }. bar span {displa Y: block; height: 12px ;}. bar I {position: absolute; top: 0; left: Pixel ;}# bar_up span {background: #360 }# bar_down span {background: # f60 }# msg {position: absolute; right: 20px; top: 40px; font-size: 18px; color: # f00} ------- jquery ------------------------- $ (function () {// when the mouse slides and leaves the voting button, change the background style $ ("# dig_up "). hover (function () {$ (this ). addClass ("digup_on") ;}, function () {$ (this ). removeClass ("digup_on") ;}); $ ("# dig _ Down "). hover (function () {$ (this ). addClass ("digdown_on") ;}, function () {$ (this ). removeClass ("digdown_on") ;}); // initialize the data getdata ("do. php ", 1); // when you click" top "$ (" # dig_up "). click (function () {getdata ("do. php? Action = like ", 1) ;}); // click $ (" # dig_down "). click (function () {getdata (" do. php? Action = unlike ", 1);}); --------------------------------------- function getdata (url, sid) {$. getJSON (url, {id: sid}, function (data) {if (data. success = 1) {// vote successful $ ("# num_up" ).html (data. like); // display the percentage progress bar effect by controlling the width $ ("# bar_up span" ).css ("width", data. like_percent); $ ("# bar_up I" pai.html (data. like_percent); $ ("# num_down" pai.html (data. unlike); $ ("# bar_down span" ).css ("width", data. unlike_percent); $ ("# bar_down I "Pai.html (data. unlike_percent);} else {// failed to vote $ ("# msg" cmd.html(data.msg0000.show(cmd.css ({'Opacity ': 1, 'top': '40px '}). animate ({top: '-50px', opacity: 0}, "slow") ;}}----------- php ------------------------ include_once ("connect. php "); // connect to the database $ action =$ _ GET ['action']; $ id = 1; $ ip = get_client_ip (); // Obtain ip if ($ action = 'like') {// Top likes (1, $ id, $ ip);} elseif ($ action = 'unlike ') {// Step On likes (0, $ id, $ ip);} else {echo Jsons ($ id) ;}------------------------------------ function likes ($ type, $ id, $ ip) {$ ip_ SQL = mysql_query ("select ip from votes_ip where vid = '$ ID' and ip =' $ IP'"); $ count = mysql_num_rows ($ ip_ SQL ); if ($ count = 0) {// you have not exceeded if ($ type = 1) {// Top $ SQL = "update votes set likes = likes + 1 where id = ". $ id;} else {// Step on $ SQL = "update votes set unlikes = unlikes + 1 where id = ". $ id;} mysql_query ($ SQL); $ SQL _in = "insert into vot Es_ip (vid, ip) values ('$ ID',' $ IP') "; mysql_query ($ SQL _in); if (mysql_insert_id ()> 0) {echo jsons ($ id);} else {$ arr ['success'] = 0; $ arr ['MSG '] = 'Operation Failed. please try again '; echo json_encode ($ arr) ;}} else {$ msg = $ type = 1? 'You have already exceeded ':' You have already stepped on '; $ arr ['success'] = 0; $ arr ['MSG '] = $ msg; echo json_encode ($ arr) ;}----------- php ------------------------- function jsons ($ id) {$ query = mysql_query ("select * from votes where id = ". $ id); $ row = mysql_fetch_array ($ query); $ like = $ row ['likes ']; $ unlike = $ row ['unlikes']; $ arr ['success'] = 1; $ arr ['like'] = $ like; $ arr ['unlike'] = $ unlike; $ like_percent = round ($ like/($ like + $ unlike), 3) * 100; $ arr ['like _ percent '] = $ like_percent. '%'; $ arr ['unlike _ percent '] = (100-$ like_percent ). '%'; return json_encode ($ arr );}

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.