PHP發(fā)表心情投票功能示例(附源碼)
當(dāng)瀏覽新聞頁面或者其它頁面的時(shí)候會(huì)有閱讀后的感受,比如給力、淡定、打醬油、加油、坑爹等等的表情。讓讀者打分,看看自己的感受是否與其他讀者一樣。很不錯(cuò)的交互!

立即下載:mood_jb51.rar
本文需要熟悉jquery,mysql,ajax相關(guān)的知識(shí),不過用的不多。本文有三個(gè)文件:index.html,mood.php,sql.php
- index.html,頁面展示和請(qǐng)求ajax數(shù)據(jù)
- mood.php,后臺(tái)文件 處理get請(qǐng)求來的數(shù)據(jù),并返回?cái)?shù)據(jù)
- sql.php,數(shù)據(jù)庫文件,存數(shù)據(jù)庫信息
直接進(jìn)入代碼吧。
index.html
首先導(dǎo)入jquery
//cdn.bootcss.com/jquery/1.7.2/jquery.min.js
當(dāng)文檔載入完畢就請(qǐng)求(ajax-get)投票人數(shù)數(shù)據(jù)
$.ajax({
type: 'GET',
url: 'mood.php',
cache: false,
data: 'id=1',
dataType: 'json',
error: function(){
alert('出錯(cuò)了!');
},
success: function(json){
if(json){
$.each(json,function(index,array){
var str = "<li><span>"+array['mood_val']+"</span><div class=\"pillar\" style=\"height:"+array['height']+"px;\"></div><div class=\"face\" rel=\""+array['mid']+"\"><img src=\"images/"+array['mood_pic']+"\"><br/>"+array['mood_name']+"</div></li>";
$("#mood ul").append(str);
});
}
}
});
返回就添加到網(wǎng)頁里,然后就點(diǎn)擊表情邏輯,也ajax到后臺(tái)
$(".face").live('click',function(){
var face = $(this);
var mid = face.attr("rel");
var value = face.parent().find("span").html();
var val = parseInt(value)+1;
$.post("mood.php?action=send",{moodid:mid,id:1},function(data){
if(data>0){
face.prev().css("height",data+"px");
face.parent().find("span").html(val);
face.find("img").addClass("selected");
}else{
alert(data);
}
});
});
這樣整個(gè)前臺(tái)就完成了工作
mood.php
首先要導(dǎo)入sql.php數(shù)據(jù)庫文件
include_once("sql.php");
這個(gè)文件處理的是整個(gè)功能的核心,處理數(shù)據(jù)庫,cookies...
1.處理獲取投票人數(shù)代碼
$mname = explode(',',$moodname);//心情說明
$num = count($mname);
$mpic = explode(',',$moodpic);//心情圖標(biāo)
$id = (int)$_GET['id'];
$query = mysql_query("select * from mood where id=$id");
$rs = mysql_fetch_array($query);
if($rs){
$total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4'];
for($i=0;$i<$num;$i++){
$field = 'mood'.$i;
$m_val = intval($rs[$field]);
$height = 0; //柱圖高度
if($total && $m_val){
$height=round(($m_val/$total)*$moodpicheight); //計(jì)算高度
}
$arr[] = array(
'mid' => $i,
'mood_name' => $mname[$i],
'mood_pic' => $mpic[$i],
'mood_val' => $m_val,
'height' => $height
);
}
echo json_encode($arr);
} else {
for($i=0;$i<$num;$i++){
$arr[] = array(
'mid' => $i,
'mood_name' => $mname[$i],
'mood_pic' => $mpic[$i],
'mood_val' => 0,
'height' => 0
);
}
echo json_encode($arr);
}
2.處理投票功能
$id = (int)$_POST['id'];
$mid = (int)$_POST['moodid'];
if($mid<0 || !$id){
echo "錯(cuò)誤";
exit;
}
$havemood = chk_mood($id);
if($havemood==1){
echo "您已表達(dá)過了";exit;
}
$field = 'mood'.$mid;
//查詢是否有這個(gè)id
$result = mysql_query("select 1 from mood where id='{$id}'");
$row = mysql_fetch_array($result);
if(is_array($row)){
$query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id);
if($query){
setcookie("mood".$id, $mid.$id, time()+3600);
$query2 = mysql_query("select * from mood where id=$id");
$rs = mysql_fetch_array($query2);
$total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4'];
$height = round(($rs[$field]/$total)*$moodpicheight);
echo $height;
}else{
echo -1;
}
} else {
mysql_query("INSERT INTO mood(id,mood0,mood1,mood2,mood3,mood4)VALUES ('{$id}','0','0','0','0','0')");
$query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id);
setcookie("mood".$id, $mid.$id, time()+3600);
echo $moodpicheight;
}
這個(gè)文件很簡單,基本都是在處理數(shù)據(jù)庫,邏輯也不是很復(fù)雜??梢宰约合聛砑?xì)心看。
sql.php
一個(gè)通用的數(shù)據(jù)庫信息儲(chǔ)存文件,數(shù)據(jù)庫的ip、帳號(hào)、密碼、數(shù)據(jù)庫名等等
$host="localhost";
$db_user="root";
$db_pass="";
$db_name="demo";
$timezone="Asia/Shanghai";
$link=mysql_connect($host,$db_user,$db_pass);
mysql_select_db($db_name,$link);
mysql_query("SET names UTF8");
header("Content-Type: text/html; charset=utf-8");
到目前所有的核心代碼都也貼出,大神就跳過,如果有需要就下載來看一看
對(duì)了,還有一個(gè)數(shù)據(jù)庫,行吧DDL也貼出來
CREATE TABLE `mood` ( `id` tinyint(5) NOT NULL, `mood0` int(9) unsigned NOT NULL, `mood1` int(9) unsigned NOT NULL, `mood2` int(9) unsigned NOT NULL, `mood3` int(9) unsigned NOT NULL, `mood4` int(9) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP中使用ElasticSearch最新實(shí)例講解
這篇文章主要介紹了PHP中使用ElasticSearch最新實(shí)例講解,這篇文章的教程是比較詳細(xì),有需要的同學(xué)可以研究下2021-03-03
PHP讀取mssql json數(shù)據(jù)中文亂碼的解決辦法
PHP及網(wǎng)頁使用UTF-8編碼,數(shù)據(jù)庫是sql server2008,使用默認(rèn)編碼,當(dāng)讀取數(shù)據(jù)庫數(shù)據(jù)時(shí),使用php自帶的json_encode()返回到前端,結(jié)果中文不顯示。下面腳本之家小編給大家介紹PHP讀取mssql json數(shù)據(jù)中文亂碼的解決辦法,需要的朋友一起學(xué)習(xí)2016-04-04
淺析PHP 中move_uploaded_file 上傳中文文件名失敗
這篇文章主要介紹了PHP 中move_uploaded_file 上傳中文文件名失敗的原因分析及解決方法 ,需要的朋友可以參考下2019-04-04
用 Composer構(gòu)建自己的 PHP 框架之設(shè)計(jì) MVC
幾乎所有人都是通過學(xué)習(xí)某個(gè)框架來了解 MVC 的,這樣可能框架用的很熟,一旦離了框架一個(gè)簡單的頁面都寫不了,更不要說自己設(shè)計(jì) MVC 架構(gòu)了,其實(shí)這里面也沒有那么多門道,原理非常清晰2014-10-10
php使用event擴(kuò)展的io復(fù)用測(cè)試的示例
這篇文章主要介紹了php使用event擴(kuò)展的io復(fù)用測(cè)試的示例,幫助大家更好的理解和使用php,感興趣的朋友可以了解下2020-10-10
php發(fā)送get、post請(qǐng)求的6種方法簡明總結(jié)
這篇文章主要介紹了php發(fā)送get、post請(qǐng)求的6種方法簡明總結(jié),分別為使用file_get_contents 、fopen、fsockopen、curl來發(fā)送GET和POST請(qǐng)求,需要的朋友可以參考下2014-07-07
淺析php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式
php中的設(shè)計(jì)模式中有很多的各種模式了,在這里我們來為各位介紹一個(gè)不常用的數(shù)據(jù)映射模式吧,感興趣的朋友一起看下吧2016-03-03

