亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何顯示 1.1k 等數字而不是 1100

如何顯示 1.1k 等數字而不是 1100

PHP
狐的傳說 2023-06-24 17:08:01
我正在嘗試在我正在構建的網站上顯示 1.1k 而不是 1000 的帖子瀏覽量。這是一個 Wordpress 網站,我在嘗試向該網站添加自定義功能時遇到了麻煩。每次我添加一些我在這里找到的片段時,它都會使網站完全崩潰。這是目前的代碼 - 它只是計算登陸該帖子的用戶以及管理員能夠手動設置帖子計數。    if(!function_exists('davenport_getPostViews')):function davenport_getPostViews($postID){    $count_key = '_davenport_post_views_count';    $count = get_post_meta($postID, $count_key, true);    if($count == ''){        delete_post_meta($postID, $count_key);        add_post_meta($postID, $count_key, '0');        return 0;    }    return $count;}endif;if(!function_exists('davenport_setPostViews')):function davenport_setPostViews() {    global $post;    $postID = $post->ID;    $count_key = '_davenport_post_views_count';    $count = get_post_meta($postID, $count_key, true);    if($count == '') {        $count = 0;        delete_post_meta($postID, $count_key);        add_post_meta($postID, $count_key, '0');    } else {        $count++;        update_post_meta($postID, $count_key, $count);    }}add_action('davenport_set_post_views', 'davenport_setPostViews');endif;任何幫助將不勝感激,因為我正竭盡全力試圖解決這個問題。
查看完整描述

4 回答

?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

一個簡單的方法是將其分開。所以這樣做看起來像這樣:


<?php 



function DisplayViews($views){

    if($views > 0){

        $display = round($views / 1000, 2);

        return $display."k";

    } else {

        return "0";

    }

}



echo DisplayViews($count); //$count should be your view count


?>

雖然上面的方法可以正常工作,但我建議您進行更多檢查,以便在只有 10 個視圖時不會顯示 0.01k。當觀看次數超過 999,999 次時也是如此。


要進行這些檢查,您需要執行以下操作:


if($views <= 999){

    //Display number without letter "K"

}


if($views > 999999){

    //Display number with the letter "m"

}

因此,將這兩者結合起來,您可以檢查數字是否小于 100,還可以檢查它是否大于 999,999,這樣做意味著您不會顯示末尾帶有錯誤字母的數字。最終代碼將如下所示:


<?php


function DisplayViews($views){

    if($views > 0){

        if($views <= 999){

            return $views;

        } elseif($views > 999999){

            $display = round($views / 1000000, 2);

            return $display."M";

        } else {

            $display = round($views / 1000, 2);

            return $display."K";

        }

    } else {

        return "0";

    }

}



echo DisplayViews($count);


?>


查看完整回答
反對 回復 2023-06-24
?
交互式愛情

TA貢獻1712條經驗 獲得超3個贊

function numberAbbreviation($number) {

$abbrevs = array(12 => "T", 9 => "B", 6 => "M", 3 => "K", 0 => "");


foreach($abbrevs as $exponent => $abbrev) {

? ? if($number >= pow(10, $exponent)) {

? ? ? ? $display_num = $number / pow(10, $exponent);

? ? ? ? $decimals = ($exponent >= 3 && round($display_num) < 100) ? 1 : 0;

? ? ? ? return number_format($display_num,$decimals) . $abbrev;

? ? }

}

}


查看完整回答
反對 回復 2023-06-24
?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

它類似于將字節大小轉換為人類可讀的格式。


function getReadableCount($count, $dec = 2) {

    $units = ['K', 'M', 'B'];

    for ($i = count($units); $i > 0; $i --) {

        $base = pow(1000, $i);

        if ($count >= $base) {

            return round($count/$base, $dec) . $units[$i-1];

        }

    }

    return $count;

}


echo getReadableCount($count, 1);


查看完整回答
反對 回復 2023-06-24
?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

在您的計數變量上調用此函數。


function humanize_number($input){

    $input = number_format($input);

    $input_count = substr_count($input, ',');

    if($input_count != '0'){

        if($input_count == '1'){

            return substr($input, 0, -4).'k';

        } else if($input_count == '2'){

            return substr($input, 0, -8).'mil';

        } else if($input_count == '3'){

            return substr($input, 0,  -12).'bil';

        } else {

            return;

        }

    } else {

        return $input;

    }

}


查看完整回答
反對 回復 2023-06-24
  • 4 回答
  • 0 關注
  • 208 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號