一個(gè)PHP+Ajax實(shí)現(xiàn)文章心情投票功能實(shí)例,可以學(xué)習(xí)了解實(shí)現(xiàn)投票的基本流程:通過(guò)ajax獲取心情圖標(biāo)及柱狀圖相關(guān)數(shù)據(jù),當(dāng)用戶點(diǎn)擊其中的一個(gè)心情圖標(biāo)時(shí),向Ajax.php發(fā)送請(qǐng)求,PHP驗(yàn)證用戶cookie防止重復(fù)提交,然后將mysql中對(duì)應(yīng)的數(shù)據(jù)心情字段內(nèi)容加1,成功后返回前端頁(yè)面,并更新柱狀圖和統(tǒng)計(jì)數(shù)據(jù)。
發(fā)表心情:
$id = (int)$_POST['id']; //文章或帖子id $mid = (int)$_POST['moodid']; //心情id(配置文件中提供8種心情) if(!$mid || !$id){ echo "此鏈接不存在";exit; } $havemood = chk_mood($id); //驗(yàn)證cookie if($havemood==1){ echo "您已經(jīng)表達(dá)過(guò)心情了,保持平常心有益身心健康!";exit; } $field = 'mood'.$mid; //數(shù)據(jù)表中的心情字段,分別用mood0,mood1,mood2...表示不同的心情字段 $query = mysql_query("update mood set ".$field."=".$field."+1 where id=".$id); //對(duì)應(yīng)的心情字段值+1 if($query){ setcookie("mood".$id, $mid.$id, time()+300); //設(shè)置cookie,為了測(cè)試我們?cè)O(shè)置cookie過(guò)期時(shí)間為300s $query2 = mysql_query("select * from mood where id=$id"); $rs = mysql_fetch_array($query2);//獲取該文章的心情數(shù)據(jù) $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4']+$rs['mood5']+ $rs['mood6']+$rs['mood7']; $height = round(($rs[$field]/$total)*$moodpicheight); //得到總量,并計(jì)算當(dāng)前對(duì)應(yīng)心情的柱狀圖的高度 echo $height; //返回當(dāng)前心情柱狀的高度 }else{ echo -1; //數(shù)據(jù)出錯(cuò) }
獲取心情:
$mname = explode(',',$moodname);//心情說(shuō)明 $num = count($mname); $mpic = explode(',',$moodpic);//心情圖標(biāo) $id = (int)$_GET['id']; //文章或帖子id $query = mysql_query("select * from mood where id=$id"); //查詢對(duì)應(yīng)的心情數(shù)據(jù) $rs = mysql_fetch_array($query); if($rs){ //得到發(fā)表心情的總量 $total = $rs['mood0']+$rs['mood1']+$rs['mood2']+$rs['mood3']+$rs['mood4']+ $rs['mood5']+$rs['mood6']+$rs['mood7']; for($i=0;$i<$num;$i++){ $field = 'mood'.$i; //字段名 $m_val = intval($rs[$field]); //心情對(duì)應(yīng)的值(次數(shù)) $height = 0; //柱圖高度 if($total && $m_val){ $height=round(($m_val/$total)*$moodpicheight); //計(jì)算高度 } $arr[] = array( 'mid' => $i, //對(duì)應(yīng)心情id 'mood_name' => $mname[$i], //心情名稱 'mood_pic' => $mpic[$i], //圖標(biāo) 'mood_val' => $m_val, //次數(shù) 'height' => $height //柱狀圖高度 ); } echo json_encode($arr); //返回JSON數(shù)據(jù) }
獲取心情列表信息,并展示在頁(yè)面中:
$(function(){ $.ajax({ type: 'GET', //通過(guò)get方式發(fā)送請(qǐng)求 url: 'ajax.php', //目標(biāo)地址 cache: false, //不緩存數(shù)據(jù),注意文明發(fā)表心情的數(shù)據(jù)是實(shí)時(shí)的,需將cache設(shè)置為false,默認(rèn)是true data: 'id=1', //參數(shù),對(duì)應(yīng)文章或帖子的id,本例中固定為1,實(shí)際應(yīng)用中是獲取當(dāng)前文章或帖子的id dataType: 'json', //數(shù)據(jù)類型為json error: function(){ alert('出錯(cuò)了!'); }, success: function(json){ //請(qǐng)求成功后 if(json){ $.each(json,function(index,array){ //遍歷json數(shù)據(jù)列 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); //將數(shù)據(jù)加入到#mood ul列表中 }); } } }); ... });
數(shù)據(jù)庫(kù)表建立直接運(yùn)行以下代碼:
CREATE TABLE IF NOT EXISTS `mood` ( `id` int(11) NOT NULL, `mood0` int(11) NOT NULL DEFAULT '0', `mood1` int(11) NOT NULL DEFAULT '0', `mood2` int(11) NOT NULL DEFAULT '0', `mood3` int(11) NOT NULL DEFAULT '0', `mood4` int(11) NOT NULL DEFAULT '0', `mood5` int(11) NOT NULL DEFAULT '0', `mood6` int(11) NOT NULL DEFAULT '0', `mood7` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; INSERT INTO `mood` (`id`, `mood0`, `mood1`, `mood2`, `mood3`, `mood4`, `mood5`, `mood6`, `mood7`) VALUES(1, 8, 6, 20, 16, 6, 9, 15, 21);