0

I am creating a social site. And I want to show people things like their total amount of likes, followers and people they are following. The way it is now, it shows the total amount of likes, followers and following as a whole number and if it's too long it will go over other words on the page.

So how do I use abbreviations like: K(for thousands), m(millions) etc ? This is what I have now.

$stmt = $con->prepare('SELECT name, username, num_likes, profile_pic FROM users WHERE user_closed = "0" 
    ORDER BY num_likes DESC LIMIT 100');
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($name, $username, $num_likes, $profile_pic);

function convert($num_likes)
{
    $num_likes = $number / 1000;
    return $num_likes . 'k';
}

This is how I show the result: <p> Total Likes: " . $num_likes ."</p>

I tried the following:

PHP Count round thousand to a K style count like facebook Share . . . Twitter Button ect

Shorten long numbers to K/M/B?

PHP Count round thousand to a K style count Facebook Share

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user4192325
  • 330
  • 1
  • 2
  • 12

2 Answers2

3

First of all, your function:

function convert($num_likes)
{
    $num_likes = $number / 1000;
    return $num_likes . 'k';
}

will not work as expected, because it converts to the opposite way :) Here is updated version:

function convert($num_likes)
{
    $number = $num_likes / 1000;
    return $number . 'k';
}

Second point. You should use the function somewhere... for example your line (actually only a part of it):

<p> Total Likes: " . $num_likes ."</p>

must be:

<p> Total Likes: " . convert($num_likes) ."</p>

And finally, using this answer we can modify convert function to this:

function convert($n) {
    if ($n < 1000) {
        $n_format = number_format($n);
    } else if ($n < 1000000) {
        // Anything less than a million
        $n_format = number_format($n / 1000, 3) . 'k';
    } else if ($n < 1000000000) {
        // Anything less than a billion
        $n_format = number_format($n / 1000000, 3) . 'M';
    } else {
        // At least a billion
        $n_format = number_format($n / 1000000000, 3) . 'B';
    }
    return $n_format;
}

Now we can convert all numbers up to billions.

Playground: click.

Anton
  • 2,669
  • 1
  • 7
  • 15
  • 1
    these are good pointers, but another pointer would be: the function itself is wrong. there is no conditional (what if $num_likes is 10?) and no rounding (1234 likes becomes 1.234k - not much better). – blahy Aug 28 '20 at 15:26
  • @blahy you are right. Added updated version of "convert" function. I just wanted to explain how we come to it. – Anton Aug 28 '20 at 15:33
  • 1
    I did it exactly like this: `function convert($num_likes) { if ($num_likes > 1000) { $number = $num_likes / 1000; return $number . 'k'; } else { return $num_likes; } }` – user4192325 Aug 28 '20 at 15:38
  • And then I showed it like this: `

    Total Likes: " . convert($num_likes) ."

    `
    – user4192325 Aug 28 '20 at 15:38
2

Perhaps like this,

Use round() if you don't want large fractions.

<?php
function convert(int $number)
{
    if ($number >= 1E9) {
        return round($number / 1E9, 2).'b';
    } else if ($number >= 1E6) {
        return round($number / 1E6, 2).'m';
    } else if ($number >= 1E3) {
        return round($number / 1E3, 2).'k';
    }
    return $number;
}

echo convert(1000000000).PHP_EOL; // 1b
echo convert(1000000).PHP_EOL;    // 1m
echo convert(1200).PHP_EOL;       // 1.2k
echo convert(1234).PHP_EOL;       // 1.23k
echo convert(100).PHP_EOL;        // 100

https://3v4l.org/cc54H

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106