php 签到积分,日历签到和积分累计的php实现方法
在網站開發過程中我們會經常用到簽到功能來獎勵用戶積分,或者做一些其他活動。這次項目開發過程中做了日歷簽到,因為沒有經驗所有走了很多彎路,再次記錄過程和步驟。
1.日歷簽到樣式:使用的是calendar日歷插件
前臺代碼
日歷簽到
本月簽到
簽到累計
累計積分
全部積分
" >
{include file="footer"}
插件calendar.js 修改如下:
var calUtil = {
//當前日歷顯示的年份
showYear:2015,
//當前日歷顯示的月份
showMonth:1,
//當前日歷顯示的天數
showDays:1,
eventName:"load",
//初始化日歷
init:function(signList,s=''){
calUtil.setMonthAndDay();
if (typeof(s) == 'undefined'){
}else{
signList.splice('','',s);
}
calUtil.draw(signList);
calUtil.bindEnvent(signList);
},
draw:function(signList){
//綁定日歷
//alert(signList.length);
// console.log(signList);
if(signList.length > 21){
//alert(21);
$("#sign_note").empty();
$("#sign_note").html('已達標,獲取1次抽獎');
}
var str = calUtil.drawCal(calUtil.showYear,calUtil.showMonth,signList);
$("#calendar").html(str);
//綁定日歷表頭
var calendarName=calUtil.showYear+"/"+calUtil.showMonth+"";
$(".calendar_month_span").html(calendarName);
},
//綁定事件
bindEnvent:function(signList){
// //綁定上個月事件
// $(".calendar_month_prev").click(function(){
// //ajax獲取日歷json數據
// //var signList=[{"signDay":"10"},{"signDay":"11"},{"signDay":"12"},{"signDay":"13"}];
// calUtil.eventName="prev";
// calUtil.init(signList);
// });
// //綁定下個月事件
// $(".calendar_month_next").click(function(){
// //ajax獲取日歷json數據
// //var signList=[{"signDay":"10"},{"signDay":"11"},{"signDay":"12"},{"signDay":"13"}];
// calUtil.eventName="next";
// calUtil.init(signList);
// });
$(".calendar_record").click(function(){
//ajax獲取日歷json數據
// console(typeof(signList)+"yxy");
//var signList=[{"signDay":"10"},{"signDay":"11"},{"signDay":"12"},{"signDay":"13"}];
//var tmp = {"signDay":$(this).html()};
//if (typeof(signList) == 'undefined'){
//不做處理
//}else{
// signList.splice('','',tmp);
// console.log(signList);
// calUtil.init(signList);
// }
//alert($(this).html());
var tmp = {"signDay":$(this).html()};
console.log(tmp.signDay)
// if(tmp.signDay==11){
//執行簽到
$.ajax({
type:'POST',
url:"checksign.html",
data:{day:tmp.signDay},
dataType:'json',
success:function(res){
// if(res.result==1){
// calUtil.init(signList,tmp);
// }else{
alert(res.msg);
location.reload(true);
// }
}
})
// }else{
// alert("請簽到當天日期")
// }
});
},
//獲取當前選擇的年月
setMonthAndDay:function(){
switch(calUtil.eventName)
{
case "load":
var current = new Date();
calUtil.showYear=current.getFullYear();
calUtil.showMonth=current.getMonth() + 1;
break;
case "prev":
var nowMonth=$(".calendar_month_span").html().split("年")[1].split("月")[0];
calUtil.showMonth=parseInt(nowMonth)-1;
if(calUtil.showMonth==0)
{
calUtil.showMonth=12;
calUtil.showYear-=1;
}
break;
case "next":
var nowMonth=$(".calendar_month_span").html().split("年")[1].split("月")[0];
calUtil.showMonth=parseInt(nowMonth)+1;
if(calUtil.showMonth==13)
{
calUtil.showMonth=1;
calUtil.showYear+=1;
}
break;
}
},
getDaysInmonth : function(iMonth, iYear){
var dPrevDate = new Date(iYear, iMonth, 0);
return dPrevDate.getDate();
},
bulidCal : function(iYear, iMonth) {
var aMonth = new Array();
aMonth[0] = new Array(7);
aMonth[1] = new Array(7);
aMonth[2] = new Array(7);
aMonth[3] = new Array(7);
aMonth[4] = new Array(7);
aMonth[5] = new Array(7);
aMonth[6] = new Array(7);
var dCalDate = new Date(iYear, iMonth - 1, 1);
var iDayOfFirst = dCalDate.getDay();
var iDaysInMonth = calUtil.getDaysInmonth(iMonth, iYear);
var iVarDate = 1;
var d, w;
aMonth[0][0] = "日";
aMonth[0][1] = "一";
aMonth[0][2] = "二";
aMonth[0][3] = "三";
aMonth[0][4] = "四";
aMonth[0][5] = "五";
aMonth[0][6] = "六";
for (d = iDayOfFirst; d < 7; d++) {
aMonth[1][d] = iVarDate;
iVarDate++;
}
for (w = 2; w < 7; w++) {
for (d = 0; d < 7; d++) {
if (iVarDate <= iDaysInMonth) {
aMonth[w][d] = iVarDate;
iVarDate++;
}
}
}
return aMonth;
},
ifHasSigned : function(signList,day){
var signed = false;
$.each(signList,function(index,item){
if(item.signDay == day) {
signed = true;
return false;
}
});
return signed ;
},
drawCal : function(iYear, iMonth ,signList) {
var myMonth = calUtil.bulidCal(iYear, iMonth);
var htmls = new Array();
htmls.push("
");
htmls.push("
");
//htmls.push("
下月");
//htmls.push("
上月");
htmls.push("
");
htmls.push("");
htmls.push("
");
htmls.push("
");
htmls.push("
" + myMonth[0][0] + "");
htmls.push("
" + myMonth[0][1] + "");
htmls.push("
" + myMonth[0][2] + "");
htmls.push("
" + myMonth[0][3] + "");
htmls.push("
" + myMonth[0][4] + "");
htmls.push("
" + myMonth[0][5] + "");
htmls.push("
" + myMonth[0][6] + "");
htmls.push("");
var d, w;
for (w = 1; w < 6; w++) {
htmls.push("
");
for (d = 0; d < 7; d++) {
var ifHasSigned = calUtil.ifHasSigned(signList,myMonth[w][d]);
console.log("001:"+ifHasSigned);
if(ifHasSigned && typeof(myMonth[w][d]) != 'undefined'){
htmls.push("
" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "");
} else {
htmls.push("
" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "");
}
}
htmls.push("");
}
htmls.push("");
htmls.push("");
htmls.push("");
return htmls.join('');
}
};
PHP代碼的實現
//簽到(status=1)
public function sign(){
//當月累計簽到
$sign = $this->model->table('praise')->where('uid='.$_SESSION['user']['uid'].' and time>'.strtotime(date("Y-m-01",time())))->select();
if($sign){
foreach($sign as $k=>$v){
$sign[$k]['day']=date('d',$v['time']);
}
}
//所有簽到
$allsign = $this->model->table('praise')->where('uid='.$_SESSION['user']['uid'].' and status=1')->select();
$this->assign('allsign', $allsign);
$this->assign('sign', $sign);
$this->assign('user', $_SESSION['user']);
$this->display('member_sign');
}
//點擊簽到
public function checksign(){
if($_POST['day']){
$day=intval($_POST['day']);
}else{
$day=date("d",time());
}
if($day!=date("d",time())){
$data['msg']="請在當前日期點擊簽到";
echo json_encode($data);
return;
}
$condition2 = 'uid='.$_SESSION['user']['uid'];
$condition2 .= " AND DATE_FORMAT(FROM_UNIXTIME(time),'%Y-%m-%d') = '".date("Y-m-d",time())."'";
$sign = $this->model->table('praise')->where($condition2)->find();
//判斷是否已經簽到
if (empty($sign)) {
//新增積分
$this->model->table('member')->data('integral=integral+'.$this->config['site_praise'].',allintegral=allintegral+'.$this->config['site_praise'])->where('uid='.$_SESSION['user']['uid'])->update();//增加積分
$arr['subject']="簽到贈送積分";
$arr['uid'] = $_SESSION['user']['uid'];
$arr['integral'] = $this->config['site_praise'];
$arr['time']=time();
$this->model->table('member_integral')->data($arr)->insert();
$updateuser = $this->model->table('member')->where('uid='.$_SESSION['user']['uid'])->find();//購物后更新session積分
$_SESSION['user']['integral'] = $updateuser['integral'];
$_SESSION['user']['allintegral'] = $updateuser['allintegral'];
$data['uid'] = $_SESSION['user']['uid'];
//$data['pid'] = $item;
$data['status'] = 1;
$data['time'] = time();
$this->model->table('praise')->data($data)->insert();
//$this->model->table('post')->data("digg=digg+1")->where('id='.$id)->update();
//$this->model->table('member_comment')->data("praise=praise+1")->where('id='.$item)->update();
$this->jssuccess('簽到成功!');
} else {
$this->jserror('已經簽過到了。');
}
}
//判斷是否已經簽到
function is_sign_now(){
$condition2 = 'uid='.$_SESSION['user']['uid'];
$condition2 .= " AND DATE_FORMAT(FROM_UNIXTIME(time),'%Y-%m-%d') = '".date("Y-m-d",time())."'";
$sign = module('common')->model->table('praise')->where($condition2)->find();
//判斷是否已經簽到
if (empty($sign)) {
return true;
}else{
return false;
}
}
總結
以上是生活随笔為你收集整理的php 签到积分,日历签到和积分累计的php实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASCII 、GB2312、GBK、GB
- 下一篇: avada functions.php,