0

I have a PHP code for the counter but I need to add something to the counter is as follows:

  • 1k
  • 1100k
  • 1200k etc
  • 2k
  • 2100k etc. and
  • 2m
  • 2500m

For now I have this counter done in PHP.

<?php function get_likes($url) {
  $json_string = file_get_contents('http://graph.facebook.com/?ids=' . $url);
  $json = json_decode($json_string, true);
  return intval( $json[$url]['shares'] );
}?>

Show with

<?php echo get_likes(http://url...); ?>
Peter O.
  • 32,158
  • 14
  • 82
  • 96
Beko Ponce
  • 11
  • 2

2 Answers2

1

Try passing the value into this simple function:

function kilomega( $val ) {
    if( $val < 1000 ) return $val;
    $val = (int)($val/1000);
    if( $val < 1000 ) return "${val}k";
    $val = (int)($val/1000);
    return "${val}m";
}
paddy
  • 60,864
  • 6
  • 61
  • 103
0

instead of show with just echo, check the size so :

<?php
 $num_likes = get_likes(http://url...); 
 if($num_likes < 1000){
   echo num_likes;
 }elseif($num_likes > 100000 ){
    echo $num_likes/100000 + "M";
 }else{
    echo $num_likes/1000 + "K";
 }

?>

... your probably also going to want to round these values to some precision 1.1k instead of actual 1.117k for example here a link to php doc's to round to the precision you want rounding in php

brendosthoughts
  • 1,683
  • 5
  • 21
  • 38