We often use the sign-in function to reward user points or do some other activities during the development of the website. This project was developed in the process of doing a calendar check-in, because no experience all took a lot of detours, recording the process and the steps again.
1. Calendar sign-in style:
2. This check-in only records the number of sign-in this month, want to query can write other pages, check all registration records. (function has, very troublesome, ancient did not do.) )
3. Foreground code
<include file= "Public:menu"/><style type= "text/css" > *{margin:0;padding:0;font:14px/1.8 "Helvetica Neue "," Microsoft Yahei ";} </style><p class= "SER_BX" > <p class= "ser_bxc" > <span style= "color: #5381B5;" > Registration record </span> <if condition= "$res [' 0 '] [' points '] eq ' 5 '" > <p class= "already Btn_center" > checked in < ;/p> <else/> <p class= "ser_mbx btn_center" > Immediate sign in </p> </if> <p class= "already b Tn_center "style=" Display:none; " > Sign in </p> <!--<p class= "Minein" > Credits: <span style= "color: #b81d25" >{$poin .points}</span> </p>--> </p></p><p class= "Check_box" ><p style= "width:500px;height:400px;margin:0 auto; " > <p style= "width:300px;height:300px;margin-left:50px;" id= "Calendar" ></p></p> </p>< Script type= "Text/javascript" > $ (document). Ready (function () {$ (". Ser_mbx"). Click (function () {$.ajax ({ URL: "{: U ('Index/checkin ')} ", type: ' POST ', datatype:" JSON ", Success:function (msg) {$ (". Already "). Show (); $ (". Ser_mbx"). Hide (); Monthsign (); } }); }); });</script> <script type= "Text/javascript" language= "JavaScript" > $ (document). Ready (function () {Mon Thsign (); }); function Monthsign () {//ajax Gets the calendar JSON data $.ajax ({URL: "{: U (' Index/monthsign ')}", type: ' POST ', DataType: "JSON", Success:function (msg) {//alert (msg); /*var signlist=[{"Signday": "Ten"},{"Signday": "One"},{"Signday": "A"},{"Signday": "13"}]; */Calutil.init (Json.parse (msg)); } }); } </script> <script type= "Text/javascript" > var calutil = {//The current calendar displays the year showyear:2015,//The current calendar displays the month SHOWM Onth:1,//The current calendar displays the number of days showdays:1, EventName: "Load",//Initialize calendar init:function (signlist) {calutil.setmonthandday (); Calutil.draw (signlist); Calutil.bindenvent (); }, DRaw:function (signlist) {//bind calendar var str = calutil.drawcal (calutil.showyear,calutil.showmonth,signlist); $ ("#calendar"). html (str); Bind Calendar Header var calendarname=calutil.showyear+ "year" +calutil.showmonth+ "month"; $ (". Calendar_month_span"). HTML (calendarname); },//Bind event Bindenvent:function () {//Bind last month Event $ (". Calendar_month_prev"). Click (function () {//ajax get calendar JSON data /*var signlist=[{"Signday": "Ten"},{"Signday": "One"},{"Signday": "A"},{"Signday": "13"}]; Calutil.eventname= "Prev"; Calutil.init (signlist); */}); Bind next month Event $ (". Calendar_month_next"). Click (function () {//ajax Get the calendar JSON data/*var signlist=[{"Signday": "},{" sig Nday ":" One by One "},{" Signday ":" "},{" Signday ":" 13 "}]; Calutil.eventname= "Next"; Calutil.init (signlist); */}); },//Gets the current selected month setmonthandday:function () {switch (calutil.eventname) {case "load": var = new D Ate (); Calutil.showyear=current.getfullyear (); Calutil.showmonth=current.getmoNth () + 1; Break Case "prev": Var nowmonth=$ (". Calendar_month_span"). html (). Split ("Year") [1].split ("month") [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 ("Year") [1].split ("month") [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] = "Day"; AMONTH[0][1] = "one"; AMONTH[0][2] = "two"; AMONTH[0][3] = "three"; AMONTH[0][4] = "four"; AMONTH[0][5] = "five"; AMONTH[0][6] = "six"; 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 ("<p class= ' sign_main ' id= ' Sign_layer ' >"); Htmls.push ("<p class= ' Sign_succ_cAlendar_title ' > "); Htmls.push ("<p class= ' Calendar_month_span ' ></p>"); Htmls.push ("</p>"); Htmls.push ("<p class= ' sign ' id= ' sign_cal ' >"); Htmls.push ("<table>"); Htmls.push ("<tr>"); Htmls.push ("<th>" + mymonth[0][0] + "</th>"); Htmls.push ("<th>" + mymonth[0][1] + "</th>"); Htmls.push ("<th>" + mymonth[0][2] + "</th>"); Htmls.push ("<th>" + mymonth[0][3] + "</th>"); Htmls.push ("<th>" + mymonth[0][4] + "</th>"); Htmls.push ("<th>" + mymonth[0][5] + "</th>"); Htmls.push ("<th>" + mymonth[0][6] + "</th>"); Htmls.push ("</tr>"); var d, W; for (w = 1; w < 7; w++) {Htmls.push ("<tr>"); for (d = 0; d < 7; d++) {var ifhassigned = calutil.ifhassigned (Signlist,mymonth[w][d]); Console.log (ifhassigned); if (ifhassigned) {Htmls.push ("<td class= ' on ' >" + (!isnan (Mymonth[w][d])? Mymonth[w][d]: "") + "</td>"); } else {Htmls.push ("<td>" + (!isnan (Mymonth[w][d]) mymonth[w][d]: "") + "</td>"); }} htmls.push ("</tr>"); } htmls.push ("</table>"); Htmls.push ("</p>"); Htmls.push ("</p>"); Return Htmls.join ("); }};</script><include file= "Public:footer"/>
4. Background code: Check whether to sign in today:
$points = M (' Points_log '); $userid =session (' user.id '); $begintime =date ("y-m-d h:i:s", Mktime (0,0,0,date (' m '), date (' d '), date (' Y '))); $endtime =date ("y-m-d h:i:s", Mktime (0,0,0,date (' m '), date (' d ') +1,date (' Y '))-1); $where =array ( ' points ' = ' 5 ', ' user_id ' and ' = ' $userid, ' createtime ' = = ' Array (' GT ', $ begintime), array (' LT ', $endtime)), ); $res = $points->where ($where)->order ("Createtime desc")->select (); Var_dump ($res [' 0 '] [' points ']); $this->assign (' res ', $res);
5. Check your points:
/* Query points * /$jfen =m (cuser); $list = $jfen->where (array (' id ' = = $userid))->field (' Points ')->find (); $preg = '/[0]*/'; $poin = Preg_replace ($preg, ", $list, 1); $this->assign (' Poin ', $poin);
6. Sign in to the database:
/* Sign In * /if (is_ajax) { $userid =session (' user.id '); $type = ' sign in '; $typename = ' checkin '; $id _status= ' up '; $date =date (' y-m-d h:i:s '); $dataList =array ( ' user_id ' = $userid, ' type ' + $type, ' TypeName ' + $typename, ' id_ Status ' = ' = $id _status, ' points ' + ' 5 ', ' createtime ' + $date, ' remark ' + ' rewards 5 points ' ); $points = M (' Points_log '); if ($points->add ($dataList)) { $log =session (' user.id '); $user =m (' Cuser '); $user->where (Array (' id ' = $log))->setinc (' Points ', 5); } $this->ajaxreturn ($status); }
7./* Check the number of sign-in days this month and return in JSON format */
Public Function Monthsign () { $userid =session (' user.id '); $points = M (' Points_log '); $res = $points->where (Array (' user_id ' = + $userid))->select (); $sign = ' ['; foreach ($res as $key = + $value) { $first =explode (', $value [' createtime ']); $second =explode ('-', $first [' 0 ']) [' 2 ']; if ($key ==0) { $sign. = ' {' Signday ': '. $second. ' "} '; } else{ $sign. = ', {"Signday": "'. $second. '"} '; } } $sign. = '] '; $this->ajaxreturn ($sign, ' json '); }