php仿QQ等级太阳显示函数



先引述下qq等级的算法:设当前等级为n,达到当前等级最少需要的活跃天数为d,当前活跃天数为dc,升级剩余天数为dr,则:

当前活跃天数为dc,升级剩余天数为dr,则:

从而推出:

代码:
/**
*
* qq等级输出类库
* @author 毛毛虫
* @version 1.0 2008-04-09
* 
*/
class showrank {
    
    /**
     * 第一级图片显示字段
     */
    public $mimage1;
    
    /**
     * 第二级图片显示字段
     */
    public $mimage2;
    
    /**
     * 第三级图片显示字段
     */
    public $mimage3;
    /**
     * 构造函数:传入图片值
     * @return 无
     */
    function __construct($pimage1 = '★',$pimage2 = '▲',$pimage3 = '●') {
        $this->mimage1 = $pimage1;
        $this->mimage2 = $pimage2;
        $this->mimage3 = $pimage3;
    }
    
    /**
     * 根据活跃天数计算用户等级。(模仿qq的升级方式)
     * @return int
     * @access public
     */
    function get_rank($pscore) {
        $temp = $pscore+4;
        $trank = sqrt($temp)-2;
        $trank = floor($trank);
        return $trank;
    }
    
    /**
     * 用户等级标志,根据用户等级显示用户标志
     * 仿照qq等级的四进制显示
     * @return str
     * @access public
     */
    function get_score($pscore) {
        $str = '';
        $trank = $this->get_rank($pscore);//根据分数取得等级
        $tpicnum = base_convert($trank,10,4);//转化为四进制
        $tpicnum = strrev($tpicnum);//翻转字符串
        $tarray = str_split($tpicnum);//转化为数组
        $tnum = count($tarray);
        if($tnum<=3) {
            for($i=$tnum-1;$i>=0;$i--){
                switch($i){
                    case '0':
                        for($j=0;$j<$tarray[$i];$j++){
                            $str .= $this->mimage1;
                        }
                    break;
                    case '1':
                        for($j=0;$j<$tarray[$i];$j++){
                            $str .= $this->mimage2;
                        }
                    break;
                    case '2':
                        for($j=0;$j<$tarray[$i];$j++){
                            $str .= $this->mimage3;
                        }
                    break;
                    default:
                        //$str = ;
                    break;
                }
            }
        }else {
            $str =     $this->mimage3.$this->mimage3.$this->mimage3.$this->mimage3;
        }
        return $str;
    }
}
?>

以上就是显示类,再来个简单的调用实例:

复制内容到剪贴板
代码:
require_once('showrank.php');
//定义等级图片
$timage1 = '';
$timage2 = '';
$timage3 = '';
//实例化并传输等级图片
$tshow = new showrank($timage1,$timage2,$timage3);
//输入的活跃天数
$tscore = 1009;
$echo = $tshow->get_score($tscore);
//输出显示效果
print '
'.$echo.'
';
?>



你可能感兴趣的:(php仿QQ等级太阳显示函数)