PHP+MySql+jQuery实现的"顶"和"踩"投票功能,mysqljquery
PHP+MySql+jQuery实现的"顶"和"踩"投票功能,mysqljquery
本文实例为大家分享了基于PHP+jQuery+MySql实现红蓝(顶踩)投票代码,供大家参考,具体内容如下
数据库操作:
CREATE TABLE IF NOT EXISTS `votes` ( `id` int(10) NOT NULL AUTO_INCREMENT, `likes` int(10) NOT NULL DEFAULT '0', `unlikes` 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;
div:
<div class="digg"> <div id="dig_up" class="digup"> <span id="num_up"></span> <p>很好,很强大!</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>太差劲了!</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(){ //鼠标滑向和离开投票按钮时,变换背景样式 $("#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"); }); //初始化数据 getdata("do.php",1); //单击“顶”时 $("#dig_up").click(function(){ getdata("do.php?action=like",1); }); //单击“踩”时 $("#dig_down").click(function(){ getdata("do.php?action=unlike",1); }); }); --------------------------------------- function getdata(url,sid){ $.getJSON(url,{id:sid},function(data){ if(data.success==1){//投票成功 $("#num_up").html(data.like); //通过控制宽度来显示百分比进度条效果 $("#bar_up span").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{//投票失败 $("#msg").html(data.msg).show().css({'opacity':1,'top':'40px'}) .animate({top:'-50px',opacity:0}, "slow"); } }); }
php:
include_once("connect.php");//连接数据库 $action = $_GET['action']; $id = 1; $ip = get_client_ip();//获取ip if($action=='like'){//顶 likes(1,$id,$ip); }elseif($action=='unlike'){//踩 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){//还没有顶过 if($type==1){//顶 $sql = "update votes set likes=likes+1 where id=".$id; }else{//踩 $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'] = '操作失败,请重试'; echo json_encode($arr); } }else{ $msg = $type==1?'您已经顶过了':'您已经踩过了'; $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); }
以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
