Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+6 votes
1.6k views
in Q2A Core by

How i make view count short? Example: 2000 to 2k

Q2A version: 1.7-beta-1
by
In SnowFlat theme not work.

1 Answer

+4 votes
by
selected by
 
Best answer

This might be a bit of an overkill solution but should be quite configurable. Just add/merge this code into your qa-theme.php file:

private $suffixes = array('', 'k', 'm');
 
private function formatViews($size, $precision = 1) {
    $base = log($size) / log(1000);
    return round(pow(1000, $base - floor($base)), $precision) . $this->suffixes[floor($base)];
}
 
private function updateViewCount(&$questionItem) {
    if (isset($questionItem['views_raw'], $questionItem['views']['data'])) {
        $questionItem['views']['data'] = $this->formatViews($questionItem['views_raw']);
    }
}
 
public function q_view($q_item) {
    $this->updateViewCount($q_item);
    qa_html_theme_base::q_view($q_item);
}
 
public function q_list_item($q_item) {
    $this->updateViewCount($q_item);
    qa_html_theme_base::q_list_item($q_item);
}
 
Some notes:
  • This should work for question lists and question view pages
  • You can change the precision (amount of digits after the decimal separator) with $precision = X
  • If you already had any of the q_list_item or q_view functions already defined then just add $this->updateViewCount($q_item); as the first line inside them and do not add those functions twice
  • You can change the suffixes in the array on top. Currently 'k' for 1000 and 'm' for 1000000. If you need more you can add... but I hardly believe you would :)
  • Example for a precision of 1: 123,456 turns into 123.5k
by
Thanks @pupi1985.
I delete ", $precision" and it's views very cool.

I use this code SnowFlat theme. Example: http://szh.kz/questions?sort=hot

http://habrastorage.org/files/cbe/901/066/cbe9010662094ee68b7eba7685d7f76f.png
by
Welcome. It looks pretty well!
by
Why not we can have this modification in core itself. As this situation is going to hit some of old users sooner or later like yerbol.
...