php仿QQ等級太陽顯示函數

來源:互聯網
上載者:User
開頭先引述下QQ等級的演算法:
設當前等級為N,達到當前等級最少需要的活躍天數為D,當前活躍天數為Dc,升級剩餘天數為Dr,則:

從而推出:

好了,引述完成,懶得寫字了,貼出代碼: 複製內容到剪貼簿
代碼:
<?php
/**
*
* QQ等級輸出類庫
* @author 毛毛蟲 <wangchong1985@gmail.com>
* @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;
    }
}
?>
以上就是顯示類,再來個簡單的調用執行個體: 複製內容到剪貼簿
代碼:
<?php
require_once('showRank.php');
//定義等級圖片
$tImage1 = '<img src="images/time_star.gif">';
$tImage2 = '<img src="images/time_yueliang.gif">';
$tImage3 = '<img src="images/time_sun.gif">';
//執行個體化並傳輸等級圖片
$tShow = new showRank($tImage1,$tImage2,$tImage3);
//輸入的活躍天數
$tScore = 1009;
$echo = $tShow->get_score($tScore);
//輸出顯示效果
print '<div align=center>'.$echo.'</div>';
?>


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.