This article mainly introduces thinkPHP's method of implementing the sign-in function. The example analyzes the construction of mysql data tables and the specific implementation skills of the thinkPHP sign-in function, for more information about how to sign in using thinkPHP, see the next article. The example shows how to construct a mysql data table and how to implement the thinkPHP sign-in function. For more information, see
This article describes how thinkPHP implements the sign-in function. We will share this with you for your reference. The details are as follows:
Data table:
Create table 'members _ sign' ('id' int (11) unsigned not null AUTO_INCREMENT, 'uid' int (11) unsigned not null comment 'user ID ', 'days 'tinyint (2) unsigned not null default '0' comment' consecutive sign-in days ', 'Is _ share' tinyint (1) unsigned not null default '0' comment' whether shared ', 'Is _ sign' tinyint (1) unsigned not null default '0' comment' whether checked in ', 'stime' int (11) unsigned not null default '0' comment' check-in time', 'atime' int (11) unsigned not null default '0' comment' add time ', primary key ('id'), KEY 'index _ uid' ('uid') using btree) ENGINE = InnoDB AUTO_INCREMENT = 162 default charset = utf8 COMMENT = 'sign in to the sharing table ';
Controller:
GetMonthSign (); $ dayList = $ this-> showDays ($ monthSign); // sign in today $ data = $ this-> todayData (); if ($ data ['is _ sign'] = 1) {$ this-> assign ('issign', true) ;}$ this-> display ();} /*** sign-in on execution day * @ return json sign-in success return {status: 1, info: 'Checked in '} */public function sign () {$ todayData = $ this-> todayData (); if ($ todayData ['is _ sign'] = 1) {$ this-> successMsg ('signed in ');} else {$ data = $ this-> getInsertData ($ this-> uid); // none today Data if ($ todayData = NULL) {$ data ['uid'] = $ this-> uid; $ data ['atime '] = time (); $ id = M ('members _ sign')-> add ($ data);} else {$ save = M ('members _ sign ') -> where ("id = {$ todayData ['id']}")-> save ($ data);} if ($ id or $ save) {$ score = $ this-> getTodayScores ($ data ['Days']); // add points to the user addScore ($ this-> uid, $ score ); $ this-> successMsg ('signed in ', array ('score' => $ score, 'Days' => $ data ['Days']);} else {$ this-> errorMsg ('Sign-in failed. please refresh and try again! ') ;}}/*** Return the data to be inserted in each sign-in ** @ param int $ uid user id * @ return array (* 'Days' => 'Days ', * 'is _ sign' => 'indicates whether to sign in. use 1 to indicate that you have already signed in. * 'stime' => 'sign in time ',*); */protected function getInsertData ($ uid) {// consecutive days of signing in Yesterday $ start_time = strtotime (date ('Y-m-d 0: 0 ', time ()-86400 )) -1; $ end_time = strtotime (date ('Y-m-d 23:59:59 ', time ()-86400) + 1; $ days = M ('members _ sign ') -> where ("uid = $ uid and atime> $ start_time and atime <$ end_time")-> getField ('days'); if ($ days) {$ days ++; if ($ days> 30) {$ days = 1 ;}} else {$ days = 1 ;}return array ('days' =>$ days, 'is _ sign' => 1, 'stime' => time ();}/*** data that the user signs in on the current day * @ return array sign in information is_sign, stime and so on */protected function todayData () {$ time = time (); $ start_stime = strtotime (date ('Y-m-d 0: 0 ', $ time)-1; $ end_stime = strtotime (date ('Y-m-d 23:59:59 ', $ time) + 1; return M ('members _ sign')-> field ('atime ', true)-> where ("uid = {$ this-> uid} and atime> $ start_stime and atime <$ end_stime")-> find ();} /*** credit rule, returns the credits corresponding to the consecutive days of sign-in ** @ param int $ days the expected score for the current day * @ return int credits */protected function getTodayScores ($ days) {if ($ days = 30) {return 50;} else if ($ days> 19) {return 8;} else if ($ days> 9) {return 5 ;} else {return 3 ;}/ *** display sign-in list ** @ param array $ signDays the date of sign-in for a month array (1, 2, 3, 4, 5, 12, 13) * @ param int $ year (optional), year * @ param int $ month (optional), month * @ return string date list
1... */Protected function showDays ($ signDays, $ year, $ month) {$ time = time (); $ year = $ year? $ Year: date ('Y', $ time); $ month = $ month? $ Month: date ('M', $ time); $ daysTotal = date ('t', mktime (0, 0, 0, $ month, 1, $ year )); $ now = date ('Y-m-D', $ time); $ str = ''; for ($ j = 1; $ j <= $ daysTotal; $ j ++) {$ I ++; $ someDay = date ('Y-m-D', strtotime ("$ year-$ month-$ j ")); // less than today's date style if ($ someDay <= $ now) {// today's date style tdc = todayColor if ($ someDay = $ now) {// if (in_array ($ j, $ signDays) checked in on the current day) {$ str. ='
'. $ J .'';} Else {$ str. ='
'. $ J .'';}} Else {// date style checked in current bfc = beforeColor, fw = font-weight if (in_array ($ j, $ signDays) {$ str. ='
'. $ J .'';} Else {$ str. ='
'. $ J .'';}} Else {$ str. ='
'. $ J .'';}} Return $ str;}/*** get the number of days of sign-in the current month. use it with $ this-> showDays () * @ return the date of sign-in the current month array (1, 2, 3, 4, 5, 12, 13) */protected function getMonthSign () {$ time = time (); $ year = date ('Y', $ time ); $ month = date ('M', $ time); $ day = date ("t", strtotime ("$ year-$ month ")); $ start_stime = strtotime ("$ year-$ month-1 0: 0")-1; $ end_stime = strtotime ("$ year-$ month-$ day 23:59:59") + 1; $ list = M ('members _ sign')-> where ("uid = {$ this-> uid} and stime> $ start_stime and stime <$ end_stime ") -> order ('stime asc ')-> getField ('stime', true); foreach ($ list as $ key => $ value) {$ list [$ key] = date ('J', $ value);} return $ list ;}}
The above is the sample code for thinkPHP to implement the sign-in function. For more information, see other related articles in the first PHP community!