當瀏覽新聞頁面或者其它頁面的時候會有閱讀后的感受赊堪,比如給力面殖、淡定、打醬油哭廉、加油脊僚、坑爹等等的表情。讓讀者打分遵绰,看看自己的感受是否與其他讀者一樣辽幌。很不錯的交互!
查看演示:http://www.weibut.com/demo/201701/mood/
立即下載:http://www.weibut.com/download/17
本文需要熟悉jquery
,mysql
,ajax
相關的知識椿访,不過用的不多乌企。本文有三個文件:index.html
,mood.php
,sql.php
- index.html,頁面展示和請求ajax數(shù)據(jù)
- mood.php成玫,后臺文件 處理get請求來的數(shù)據(jù)加酵,并返回數(shù)據(jù)
- sql.php,數(shù)據(jù)庫文件哭当,存數(shù)據(jù)庫信息
直接進入代碼吧猪腕。
index.html
首先導入jquery
//cdn.bootcss.com/jquery/1.7.2/jquery.min.js
當文檔載入完畢就請求(ajax-get)投票人數(shù)數(shù)據(jù)
$.ajax({
type: 'GET',
url: 'mood.php',
cache: false,
data: 'id=1',
dataType: 'json',
error: function(){
alert('出錯了!');
},
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);
});
}
}
});
返回就添加到網頁里钦勘,然后就點擊表情邏輯陋葡,也ajax到后臺
$(".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);
}
});
});
這樣整個前臺就完成了工作
mood.php
首先要導入sql.php數(shù)據(jù)庫文件
include_once("sql.php");
這個文件處理的是整個功能的核心,處理數(shù)據(jù)庫
彻采,cookies
...
1.處理獲取投票人數(shù)代碼
$mname = explode(',',$moodname);//心情說明
$num = count($mname);
$mpic = explode(',',$moodpic);//心情圖標
$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); //計算高度
}
$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 "錯誤";
exit;
}
$havemood = chk_mood($id);
if($havemood==1){
echo "您已表達過了";exit;
}
$field = 'mood'.$mid;
//查詢是否有這個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;
}
這個文件很簡單脖岛,基本都是在處理數(shù)據(jù)庫朵栖,邏輯也不是很復雜〔癜穑可以自己下來細心看。
sql.php
一個通用的數(shù)據(jù)庫信息儲存文件,數(shù)據(jù)庫的ip终惑、帳號绍在、密碼、數(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");
到目前所有的核心代碼都也貼出雹有,大神就跳過偿渡,如果有需要就下載來看一看
對了,還有一個數(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;