Ambrosia Community
General Category => Support Section => Nerdy Corner => Topic started by: Red Wolf on January 07, 2016, 09:12:34 pm
-
Hi,
I got this piece of code which gets id from a field.
<?php
$game = edited_connector_get_game(get_field('game_id'));
echo "Game voted from " . $game["gamevotes"] . " users." ;
?>basically, the output is
Game voted from X users, where X is a number.
How can i make this number in English format, aka, add comma after 3 digits.
Now i get:
Game voted from 1234 users,
i want it to be
Game voted from 1,234 users.
-
where does the php read the data?
I think you can separate the data in 2 varibles and place a echo "," between them.
one of the variables will acquire the numbers greater than 1000 and change accordingly to the data it reads, and the other 3 numbers can be placed in another variable.
I have't worked with php at all so I'm following just practical instinct :)
-
If I remember correctly there is a function that you can use to do what you want it to do. The function in question is number_format( ). Simply change your code to this:
$game = edited_connector_get_game(get_field(game_id'));
echo "Game voted from " . number_format( $game["gamevotes"] ) . " users." ;
-
where does the php read the data?
I think you can separate the data in 2 varibles and place a echo "," between them.
one of the variables will acquire the numbers greater than 1000 and change accordingly to the data it reads, and the other 3 numbers can be placed in another variable.
I have't worked with php at all so I'm following just practical instinct :)
Its a wordpress page and there is a field, the data is read there, a plugin takes the ID data from an external site, and I use strings on manual to get datas I need according to the 1st field mentioned.
If I remember correctly there is a function that you can use to do what you want it to do. The function in question is number_format( ). Simply change your code to this:
$game = edited_connector_get_game(get_field(game_id'));
echo "Game voted from " . number_format( $game["gamevotes"] ) . " users." ;
Thanks a lot man, that worked.
U can lock this.
-
Alternatively, you can transform the data to an array of strings and make an if condition, that when a string (in this case our number) consits of more or is equal to 4 characters, you set the second field to comma add print out the rest, although much more practical is to use the actual function tony provided. This is just a other-way-around solution you could use in programming languages that do not provide such function.
Here's a graphical illustration of how it would work for easier comprehension
if a number <= 4 characters
print out the number
else
make every character of the number into an array
for example the standard input would be 1258
set the 0th string to 1st character which would be 1 second to comma etc
in this case
--------------------
[0][1][2][3][4]
1 . 2 5 8
--------------------
Doesn't really matter how large the number would be, you can create a for loop that keeps on extending the array until we use all the characters from input.
Important is only adding the first number and the comma.
It's a pretty stupid/binary solution, but I didn't think of anything better that could be universally applied to pretty much any programming language. Obviously there are easier ways to do it, without even having to do this, but this is like easiest way to do it that would work with pretty much everything.
Though yeah, use the function if you can, no need to complicate our lives.