View Full Version : Posts, replys & Views "counts" without comma?
RaYdeN.ADM
05-24-2017, 03:44 AM
I need to remove the comma in order to send the variable in javascript and work the number to 1200> 1.2K. The problem is that javascript takes the comma as if it were sending 2 values and only works with numbers without a comma.
So what I need is to remove the comma before sending it to javascript.
Example:
1,000 > 1000
1,000,000 > 1000000
<script type="text/javascript">
function fHcountHits(num) {
if (num >= 1000000000) {
return (num / 1000000000).toFixed(1).replace(/\.0$/, '') + 'G';
} else if (num >= 1000000) {
return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M';
} else if (num >= 1000) {
return (num / 1000).toFixed(1).replace(/\.0$/, '') + 'K';
}
return num;
}
</script>
<script type="text/javascript">document.write(fHcountHits({vb:raw forum.threadcount}));</script>
<script type="text/javascript">document.write(fHcountHits({vb:raw forum.replycount}));</script>
The error is that the {vb:raw forum.threadcount} and {vb:raw forum.replycount} return the numbers with a comma, how do I remove them ?.
thx
MarkFL
05-24-2017, 04:03 AM
In your second script, try this:
<script type="text/javascript">
var n = {vb:raw forum.threadcount};
n = n.replace(/\,/g, '');
n = parseInt(n, 10);
document.write(fHcountHits(n));
</script>
RaYdeN.ADM
05-24-2017, 04:13 AM
In your second script, try this:
<script type="text/javascript">
var n = {vb:raw forum.threadcount};
n = n.replace(/\,/g, '');
n = parseInt(n, 10);
document.write(fHcountHits(n));
</script>
https://vborg.vbsupport.ru/external/2017/05/7.png
I have tried it from this method, but without results, it directly does not return anything and with the second script that publishes it returns 1 when it detects a comma.
Any other solution ?.
MarkFL
05-24-2017, 05:00 AM
You would be better off doing this server side (PHP), rather than client side (javascript). For example, you could define your function by using a plugin hooked at "forumhome_start" with the code:
function fHcountHits($num)
{
switch ($num)
{
case ($num > 1000000000):
$num = round($num/1000000000, 1);
$num .= 'G';
break;
case ($num > 1000000):
$num = round($num/1000000, 1);
$num .= 'M';
break;
case ($num > 1000):
$num = round($num/1000, 1);
$num .= 'K';
break;
}
return $num;
}
And then, create another plugin, this one hooked at "forumbit_display" with the code:
$forum['threadcount'] = fHcountHits($forum['threadcount']);
$forum['replycount'] = fHcountHits($forum['replycount']);
RaYdeN.ADM
05-24-2017, 05:22 AM
I removed my javascript, left the raw default and did this:
plugin 1:
https://vborg.vbsupport.ru/external/2017/05/8.png
plugin 2:
https://vborg.vbsupport.ru/external/2017/05/9.png
forumhome:
Apparently it works, at least shows numbers, no counter exceeds 1000 so no wisdom really worked, but at the moment it shows the normal.
forumdisplay = subforum?:
Fatal error: Call to undefined function fHcountHits() in public_html/forum/includes/functions_forumlist.php(466) : eval()'d code on line 26
edit:
If I move the plugin 1 to global_start, it works, but look how it responds:
https://vborg.vbsupport.ru/external/2017/05/10.png
MarkFL
05-24-2017, 05:29 AM
In order to get the same kind of thing happening on the forumdisplay page, you need to create 2 new plugins...one hooked at "forumdisplay_start" to contain the fHcountHits() function, and another at "threadbit display" with the code:
$thread['views'] = fHcountHits($thread['views']);
Just move that line from the one you put it in, to this plugin.
RaYdeN.ADM
05-24-2017, 05:43 AM
hmm so?:
plugin1 - main
https://vborg.vbsupport.ru/external/2017/05/11.png
plugin2 - hook1
https://vborg.vbsupport.ru/external/2017/05/12.png
plugin3 - hook2
https://vborg.vbsupport.ru/external/2017/05/13.png
now, forumhome error:
Fatal error: Call to undefined function fHcountHits() in /public_html/forum/includes/functions_forumlist.php(466) : eval()'d code on line 26
and subforum:
https://vborg.vbsupport.ru/external/2017/05/14.png
edit:
Sorry, I pass it to forumhome_start, also forumehome works and subforum stops working with the same error that the function is not declared.
On the other hand, although this stated does not do what it has to do.
MarkFL
05-24-2017, 05:53 AM
You need 4 plugins total...you are missing the function definition hooked at "forumhome_start"...:)
The function definition needs to be in 2 plugins.
RaYdeN.ADM
05-24-2017, 06:03 AM
You need 4 plugins total...you are missing the function definition hooked at "forumhome_start"...:)
The function definition needs to be in 2 plugins.
Ahh okey! XD
Well, the plugins and hooks already work. Now what is wrong for "0G" to write and the "1,"00" > "1200" > "1.2K" ?.. That do not work even if the 4 plugins are working. :confused: :(
MarkFL
05-24-2017, 06:12 AM
I would have to come to your site to sort it out...so if you want, PM the login credentials to a full admin account to me, and I will come by tomorrow and take a look. It is very late here. :)
RaYdeN.ADM
05-24-2017, 04:11 PM
Thx for all MarkFL, the final solution is:
function fHcountHits($num)
{
$new_value = str_replace( ",", "", $num );
if ( $new_value > 1000000000 ) {
$new_value = round( $new_value/1000000000, 1 ) . 'G';
} else if ( $new_value > 1000000 ) {
$new_value = round( $new_value/1000000, 1 ) . 'M';
} else if ( $new_value > 1000 ) {
$new_value = round( $new_value/1000, 1 ) . 'K';
}
return $new_value;
}
credit raul bravo.
MarkFL
05-24-2017, 04:16 PM
Glad you got it sorted out. :)
MarkFL
05-24-2017, 05:20 PM
You could use the following:
function fHcountHits($num)
{
if ($num)
{
$num = str_replace(',', '', $num);
$prefix = array(0 => '', 1 => 'K', 2 => 'M', 3 => 'G', 4 => 'T');
$pk = floor((1/3)*log($num, 10));
$num = round(($num/pow(10, 3*$pk)), 1) . $prefix[$pk];
}
return $num;
}
RaYdeN.ADM
05-26-2017, 04:30 AM
You could use the following:
function fHcountHits($num)
{
if ($num)
{
$num = str_replace(',', '', $num);
$prefix = array(0 => '', 1 => 'K', 2 => 'M', 3 => 'G', 4 => 'T');
$pk = floor((1/3)*log($num, 10));
$num = round(($num/pow(10, 3*$pk)), 1) . $prefix[$pk];
}
return $num;
}
Thanks for the optimization of the code. It works.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.