php实现签到功能

 控制器的写

$date=yii::$app->request->get('date');


        if(isset($date)){
            $user_id=1;
            $model=new SignIn();
            if(!$model->show($user_id,$date)){
                $dated=$date-3600*24;
                $yesterday=$model->show($user_id,$dated);
                $sign_num=!empty($yesterday)?$yesterday[0]['sign_num']:0;
                if($sign_num+1==5){
                    $data=['user_id'=>$user_id,'sign_date'=>intval($date),'sign'=>1];

                }else{
                    $data=['user_id'=>$user_id,'sign_date'=>intval($date),'sign'=>1,'sign_num'=>$sign_num+1];
                }
                    if($model->add($data)){
                        $model->sign_total($data['user_id']);
                        return "";
                    }

            }else{
                return "";
            }


//            $data=['user_id'=>1,'sign_date'=>$date];
//
//            $model->sign($data);
        }else{
            return $this->render('sign');

        }


model层的一些写法

class SignIn extends \yii\db\ActiveRecord
{
    public function sign_total($userId){
        $sql='update user set sign_total=sign_total+10 WHERE id='.$userId;
        return Yii::$app->db->createCommand($sql)->query();
    }

    public function add($data){
        $value='';
        foreach($data as $val){
            $value.=",'$val'";
        }
        $k='';
        foreach($data as $key=>$val){
            $k.=",$key";
        }
        $value=substr($value,1);
        $k=substr($k,1);
    return    Yii::$app->db->createCommand("insert into sign_in($k) VALUES($value)")->query();

    }

    public function show($id,$dated){

    return    $rows = (new \yii\db\Query())->from('sign_in')
            ->where(['user_id' => $id])
            ->andWhere(['sign_date'=>$dated])
            ->all();
    }


你可能感兴趣的:(关于yii框架)