jQuery PHP實現購物商城常用的星級評分效果,我們在商城平台購買商品後,會有個評分功能,本實例就來說說實現方法。
首先我們在.rate裡面加入顯示的灰星星p#big_rate、亮星星p#big_rate_up、分數span#s及span#g和提示訊息p#my_rate 。
接著我們寫一個獲取評分的方法get_rate() :
function get_rate(rate) { rate = rate.toString(); var s; var g; $("#g").show(); if (rate.length >= 3) { s = 10; g = 0; $("#g").hide(); } else if (rate == "0") { s = 0; g = 0; } else { s = rate.substr(0, 1); g = rate.substr(1, 1); } $("#s").text(s); $("#g").text("." + g); $(".big_rate_up").animate({ width: (parseInt(s) + parseInt(g) / 10) * 14, height: 26 }, 1000); $(".big_rate span").each(function() { $(this).mouseover(function() { $(".big_rate_up").width($(this).attr("rate") * 14); $("#s").text($(this).attr("rate")); $("#g").text(""); }).click(function() { var score = $(this).attr("rate"); $("#my_rate").html("您的评分:<span>" + score + "</span>"); $.ajax({ type: "POST", url: "ajax.php", data: "score=" + score, success: function(msg) { //alert(msg); if (msg == 1) { $("#my_rate").html("<span>您已经评过分了!</span>"); } else if (msg == 2) { $("#my_rate").html("<span>您评过分了!</span>"); } else { get_rate(msg); } } }); }) }) $(".big_rate").mouseout(function() { $("#s").text(s); $("#g").text("." + g); $(".big_rate_up").width((parseInt(s) + parseInt(g) / 10) * 14); }) }
然後直接呼叫該方法即可:
get_rate(<?php echo $aver; ?>);
$score = $_POST['score']; if (isset($score)) { $cookiestr = getip(); $time = time(); if (isset($_COOKIE['person']) && $_COOKIE['person'] == $cookiestr) { echo "1"; } elseif (isset($_COOKIE['rate_time']) && ($time - intval($_COOKIE['rate_time'])) < 60) { echo "2"; } else { $query = mysql_query("update raty set voter=voter+1,total=total+'$score' where id=1"); $query = mysql_query("select * from raty where id=1"); $rs = mysql_fetch_array($query); $aver = 0; if ($rs) { $aver = $rs['total'] / $rs['voter']; $aver = round($aver, 1) * 10; } //设置COOKIE setcookie("person", $cookiestr, time() + 3600 * 365); setcookie("rate_time", time(), time() + 3600 * 365); echo $aver; } }
CREATE TABLE IF NOT EXISTS `raty` ( `id` int(11) NOT NULL auto_increment, `voter` int(10) NOT NULL default '0' COMMENT '评分次数', `total` int(11) NOT NULL default '0' COMMENT '总分', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
以上是jQuery+PHP實現購物商城常用的星級評分效果的詳細內容。更多資訊請關注PHP中文網其他相關文章!