php計算會員卡

前段時間接到一個製作會員卡的單子,一開始挺好奇爲什麼要把一個會員天數要分開成卡,後來才發現,那個是以充值天數來的,然後需要以卡類型進行判斷
那麼卡分別爲

  • 天卡·day
  • 周卡·week
  • 月卡·month
  • 季卡·season
  • 年卡·year

直接放算法

<?php

/**
 * Class vip_class 天數計算月卡
 * @author 院主網絡科技團隊
 * @link url www.berfen.com
 */
class vip_class{
    private $year=0;
    private $season=0;
    private $month=0;
    private $week=0;
    private $day=0;

    public function count($vip){
        switch ($this->rank($vip)){
            case 'day'://計算天卡
                $data=$this->day_count($vip);
                $this->day=$data['num'];
                break;

            case 'week'://計算周卡
                $data=$this->week_count($vip);
                $this->week=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'month'://計算月卡
                $data=$this->month_count($vip);
                $this->month=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'season'://計算季卡
                $data=$this->season_count($vip);
                $this->season=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='month'){
                    $data=$this->month_count($int);
                    $this->month=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;

            case 'year'://計算年卡
                $data=$this->year_count($vip);
                $this->year=$data['num'];
                $int=$data['int'];
                if ($this->rank($int)=='season'){
                    $data=$this->season_count($int);
                    $this->season=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='month'){
                    $data=$this->month_count($int);
                    $this->month=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='week'){
                    $data=$this->week_count($int);
                    $this->week=$data['num'];
                    $int=$data['int'];
                }
                if ($this->rank($int)=='day'){
                    $data=$this->day_count($int);
                    $this->day=$data['num'];
                }
                break;
        }
        $data=array(
            'year'=>$this->year
            ,'season'=>$this->season
            ,'month'=>$this->month
            ,'week'=>$this->week
            ,'day'=>$this->day
        );
        return $data;
    }

    private function year_count($int){//計算總出現的年卡
        $year=1;
        $int=$int-365;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='year'){
                $int=$int-365;
                $year++;
            }else{
                continue;
            }
        }
        $data['num']=$year;
        $data['int']=$int;
        return $data;
    }

    private function season_count($int){//計算總出現的季卡
        $season=1;
        $int=$int-90;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='season'){
                $int=$int-90;
                $season++;
            }else{
                continue;
            }
        }
        $data['num']=$season;
        $data['int']=$int;
        return $data;
    }

    private function month_count($int){//計算總出現的月卡
        $month=1;
        $int=$int-30;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='month'){
                $int=$int-30;
                $month++;
            }else{
                continue;
            }
        }
        $data['num']=$month;
        $data['int']=$int;
        return $data;
    }

    private function week_count($int){//計算總出現的周卡
        $week=1;
        $int=$int-7;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='week'){
                $int=$int-7;
                $week++;
            }else{
                continue;
            }
        }
        $data['num']=$week;
        $data['int']=$int;
        return $data;
    }

    private function day_count($int){//計算總出現的天卡
        $day=1;
        $int--;
        for ($i=1;$i<100;$i++){
            $js=$this->rank($int);
            if ($js=='day'){
                $int--;
                $day++;
            }else{
                continue;
            }
        }
        $data['num']=$day;
        $data['int']=$int;
        return $data;
    }

    private function rank($score)
    {
        switch (true)
        {
            case $score >= 365:   return 'year';
            case $score >= 90:   return 'season';
            case $score >= 30:   return 'month';
            case $score >= 7:    return 'week';
            case $score >= 1:     return 'day';
            default:             return false;
        }
    }
}

代碼有點複雜,沒事,多看幾遍其中的關係,就知道了
我們先看最後一個函數

private function rank($score)
    {
        switch (true)
        {
            case $score >= 365:   return 'year';
            case $score >= 90:   return 'season';
            case $score >= 30:   return 'month';
            case $score >= 7:    return 'week';
            case $score >= 1:     return 'day';
            default:             return false;
        }
    }

這個是一個判斷一個數值是什麼卡
如89,那就是月卡,然後通過月卡判斷的循環次數就可以得到有幾張卡了

調用示例

include 'vip_class.php';//必須引用這個類
$vip_class=new vip_class();//使用函數
$vip=896;//購買的天數
$data=$vip_class->count($vip);//調用函數返回數據

返回示例

首先,上面的$data是一個數據變量,我們只需要這樣取出

echo $data['day'];

就可以得到天卡多少張了

年卡2張
季卡1張
月卡2張
周卡2張
天卡2張
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章