Typecho 总访问量统计代码友好化修改
笔记 2022-12-26 17:05:00 445 0 1

这几天在对小弟我的 typecho 简悦主题进行更新的时候用到了网站总访问量的统计,根据大神们提供的代码我们使用了如下代码

VIEWS字段如果数据库没有会报错,可以根据自己数据看的统计字段进行修改
function theAllViews()
        {
            $db = Typecho_Db::get();
            $row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`');
                echo number_format($row[0]['SUM(VIEWS)']);
        }

首页调用代码如下

<?php echo theAllViews();?>

效果是很好的,但是在使用过的过程中如果总访问量过大一长串数字就会显得很臃肿,小弟我就在网上找了有好话的转换,转换代码如下

    function convert($num) {
        if ($num >= 100000){
            $num = round($num / 10000) .'W+';
        } 
        else if ($num >= 10000) {
            $num = round($num / 10000, 1) .'W+';
        } 
        else if($num >= 1000) {
            $num = round($num / 1000, 1) . 'K+';
        }
        return $num;
    }

结合这段代码我们对总访问量进行有好化得出了如下代码

function theAllViews()
        {
            $db = Typecho_Db::get();
            $row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`');
$num  = number_format($row[0]['SUM(VIEWS)']);
if ($num >= 100000){
        $num = round($num / 10000) .'W+';
    } 
    else if ($num >= 10000) {
        $num = round($num / 10000, 1) .'W+';
    } 
    else if($num >= 1000) {
        $num = round($num / 1000, 1) . 'K+';
    }
    return $num;
}

可是实际在使用过程中却出不来想过,于是本着统一小日子的理念查看原有代码后发现我们获取总和的时候会出现分割符号,找到了问题所在就好解决了,经过修改正确食用代码如下

function theAllViews()
        {
$db = Typecho_Db::get();
$row = $db->fetchAll('SELECT SUM(VIEWS) FROM `typecho_contents`');
$data = number_format($row[0]['SUM(VIEWS)']);
$num = str_replace(",","",$data) ;
if ($num >= 100000){
        $num = round($num / 10000) .'W+';
    } 
    else if ($num >= 10000) {
        $num = round($num / 10000, 1) .'W+';
    } 
    else if($num >= 1000) {
        $num = round($num / 1000, 1) . 'K+';
    }
    return $num;
}

效果图

QQ20221226-171006@2x.png

typechotheAllViews简悦主题php显示阅读量语法

您填写的所有信息都将进行加密

首页 主题 Api 友联