Php+mysql+jquery implementation of the "Top" and "step" voting function _php skills

Source: Internet
Author: User
Tags get ip learn php

This article for everyone to share the implementation of PHP+JQUERY+MYSQL based on the red-blue (top step) voting code for your reference, the specific contents are as follows

Database operations:

CREATE TABLE IF not EXISTS ' votes ' ( 
 ' id ' int ') NOT NULL auto_increment, 
 ' likes ' int (a) NOT null DEFAULT ' 0 ', 
    ' unlikes ' int () not NULL default ' 0 ', 
 PRIMARY KEY (' id ') 
) Engine=myisam DEFAULT Charset=utf8; 
 
 
INSERT into ' votes ' (' id ', ' likes ', ' Unlikes ') VALUES 
(1, a); 
 
CREATE TABLE IF not EXISTS ' votes_ip ' ( 
 ' id ' int (+) not null, 
 ' vid ' int (a) not NULL, 
 ' IP ' varchar ' NULL 
) Engine=myisam DEFAULT Charset=utf8; 

Div:

<div class= "Digg" > 
 <div id= "dig_up" class= "Digup" > 
 <span id= "num_up" ></span> 
 <p> very good, very strong! </p> 
 <div id= "bar_up" class= "bar" ><span></span><i></i></div> 
 </div> 
 <div id= "Dig_down" class= "Digdown" > 
 <span id= "Num_down" ></span> 
 <p > It's so lame! </p> 
 <div id= "Bar_down" class= "Bar" ><span></span><i></i></div> 
 </div> 
 <div id= "msg" ></div> 
</div> 

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.png) 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{display:block; height:12px;} 
. bar I{position:absolute; top:0; left:104px;} #bar_up span{background: #360} #bar_down span{background: #f60} #msg {position:absolute; right:20px; top:40px; font-size : 18px; 

 Color: #f00}

jquery:

$ (function () {//mouse to slide and exit 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"); 
 
 }); 
 
 Initialization data GetData ("do.php", 1); 
 Click $ ("#dig_up") at the top of the button. Click (function () {GetData ("Do.php?action=like", 1); 
 }); 
 When you click "Step" $ ("#dig_down"). Click (function () {GetData ("Do.php?action=unlike", 1); 
}); 
 
 
 
}); ---------------------------------------function GetData (url,sid) {$.getjson (url,{id:sid},function (data) {if ( 
  Data.success==1) {//voted successfully $ ("#num_up"). HTML (data.like); 
  Displays the percentage progress bar effect $ ("#bar_up span") by controlling the width. css ("width", data.like_percent); 
  $ ("#bar_up i"). html (data.like_percent); 
  $ ("#num_down"). HTML (data.unlike); 
  $ ("#bar_down span"). CSS ("width", data.unlike_percent); 
 $ ("#bar_down i"). html (data.unlike_percent); }else{//vote failed $ ("#msg"). HTML (DATA.MSG). Show (). CSS ({' OpAcity ': 1, ' top ': ' 40px '}. Animate ({top: ' -50px ', opacity:0}, "slow"); 
} 
 }); 
 }

PHP:

Include_once ("connect.php");/Connect Database $action = $_get[' action ']; 
$id = 1; 
$ip = GET_CLIENT_IP ()///Get IP if ($action = = ' like ') {//top likes (1, $id, $IP); 
}elseif ($action = = ' unlike ') {//step likes (0, $id, $IP); 
}else{Echo jsons ($id); ------------------------------------function likes ($type, $id, $ip) {$ip _sql=mysql_query ("Select IP from VOTES_IP WH 
 ere vid= ' $id ' and ip= ' $ip '); 
 $count =mysql_num_rows ($ip _sql); 
 if ($count ==0) {//has not been topped if ($type ==1) {//Top $sql = "update votes set likes=likes+1 where id=". $id; 
 }else{//Step $sql = "update votes set unlikes=unlikes+1 where id=". $id; 
  
 } mysql_query ($sql); 
 $sql _in = "INSERT into VOTES_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've already topped ': ' You've 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); 
 }

The above is the entire content of this article, I hope that you learn PHP program help.

Related Article

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.