Thinkphp sample code sharing for implementing the check-in function method

Source: Internet
Author: User
Tags add time
This article mainly introduces the method of thinkphp to realize the check-in function, and analyzes the construction of MySQL data table and the concrete realization technique of thinkphp check-in function, which can be referenced by friends.

In this paper, the method of thinkphp realization of check-in function is described. Share to everyone for your reference, as follows:

Data sheet:


CREATE TABLE ' members_sign ' (' id ' int (one) unsigned not null auto_increment, ' uid ' int (one) unsigned not null COMMENT ' user ID ', ' Days ' of tinyint (2) unsigned NOT null default ' 0 ' COMMENT ' consecutive sign-on ', ' Is_share ' tinyint (1) unsigned NOT null default ' 0 ' COMMENT ' shared ', ' is_sign ' tinyint (1) unsigned not null DEFAULT ' 0 ' COMMENT ' checked in ', ' stime ' int (one) unsigned NOT null DEF Ault ' 0 ' COMMENT ' sign in time ', ' atime ' int (one) 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 sharing table ';

Controller:


<?phpnamespace member\controller;use Member\controller\mcontroller;class Indexcontroller extends MController {/**        * User Center * @param */Public Function index () {$pre = C (' Db_prefix ');    Calendar list $monthSign = $this->getmonthsign ();    $dayList = $this->showdays ($monthSign);    Sign in today $data = $this->todaydata ();    if ($data [' is_sign '] = = 1) {$this->assign (' issign ', true);  } $this->display (); }/** * Perform the same day check-in * @return JSON check-in successfully returned {status:1,info: ' Checked-in '} */Public function sign () {$todayData = $this->today    Data ();    if ($todayData [' is_sign '] = = 1) {$this->successmsg (' checked in ');      }else{$data = $this->getinsertdata ($this->uid);        No 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 Addscore ($this->uid, $score) for the user;      $this->successmsg (' checked in ', Array (' score ' = ' + $score, ' days ' + $data [' days '])); }else{$this->errormsg (' Sign in failed, please refresh and try again!      ');   }}}/** * Returns the data to be inserted for each check-in * * @param int $uid User ID * @return Array (* ' days ' = ' # '), * ' is_sign ' =  ' Check-in, sign in with 1 ', * ' stime ' = ' sign-in time ', *); */protected function Getinsertdata ($UID) {//yesterday consecutive sign-in days $start _time = strtotime (Date (' y-m-d 0: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 >) {$days = 1;    }}else{$days = 1;  } return Array (' days ' = $days, ' is_sign ' = 1, ' stime ' + Time ()); }/** * User registration data on the same day * @return Array Registration LetterIs_sign,stime, etc. */protected function Todaydata () {$time = time ();    $start _stime = strtotime (Date (' y-m-d 0: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 (); }/** * Integral rule, returns the number of consecutive check-in days for the points * * @param int $days The score that should be earned on the day * @return int integral */protected function gettodayscores ($da    YS) {if ($days = =) {return 50;    }else if ($days >) {return 8;    }else if ($days > 9) {return 5;    }else{return 3; }}/** * Displays the check-in list * * @param array $signDays The date of registration for the Month Array (1,2,3,4,5,12,13) * @param int $year optional, year * @param in T $month Optional, Month * @return string date list <li>1</li&gt ... */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) {//day date style TDC = Todaycolor if ($someDay = = $now) {//On the same day sign          The IF (In_array ($j, $signDays)) {$str. = ' <li class= ' current FW tdc ' > '. $j. ' </li> ';          }else{$str. = ' <li class= ' today FW TDC > '. $j. ' </li> ';            }}else{//sign-in date style current BFC = Beforecolor, FW = Font-weight if (In_array ($j, $signDays)) {          $str. = ' <li class= ' current FW BFC > ' $j. ' </li> ';          }else{$str. = ' <li class= ' fw BFC > '. $j. ' </li> ';      }}}else{$str. = ' <li> ' $j. ' </li> ';  }} return $str; }/** * Gets the number of days of check-in in the month, used with $this->showdays () * @return Month sign-in date 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: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")->ord    ER (' stime asc ')->getfield (' Stime ', true);    foreach ($list as $key = + $value) {$list [$key] = date (' j ', $value);  } return $list; }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.