Approximate count in PHP

Go To StackoverFlow.com

3

I have a website that contains a list of articles. I want to include a line that says how many articles there are in the DB, however I don't want it to show the exact count.

I would like it to round down to the nearest 10, so for example for 105 articles I would like it to say "100+", etc.

How would I go about doing this?

2012-04-04 06:24
by Sherwin Flight
so 18155 would be 18150+ - Andreas Wong 2012-04-04 06:27
Yes, that's correct - Sherwin Flight 2012-04-04 06:32
I'm baffled at all the answers. x - Alix Axel 2012-04-04 06:36
They were pretty quick, which I really appreciate. : - Sherwin Flight 2012-04-04 06:44


5

Quite simple:

floor($num / 10) * 10

docs

2012-04-04 06:27
by Shad
Going to mark this as the accepted answer. Very simple, and the link to the documentation was appreciated - Sherwin Flight 2012-04-04 06:39


3

To do something similar you have to use the logarithm with base = 10 and round it

$exp= floor(log($num)/log(10));
$num = pow(10,$exp)."+";

This works with 10, 100, 1000 ecc, i think it better do what you asked.

2012-04-04 06:32
by kappa
I agree, +1 from me - Alix Axel 2012-04-04 06:36
The accepted answer seems to work for what I need, and I tried it with many different numbers, and it is a bit shorter. I will mark this answer up though as it works as well - Sherwin Flight 2012-04-04 06:40


2

$count = 105;
$nearest10 = floor($count / 10) * 10;
2012-04-04 06:27
by Alix Axel
Marked this answer up as well. It was the same as an answer already posted, both said "10 mins ago", and I can only pick one accepted answer - Sherwin Flight 2012-04-04 06:42


1

printf("%d0+", $count * 0.1);

or

echo substr($count, 0, -1), '0+';

And the roundDocs function does support it out of the box, too:

round($count, -1, PHP_ROUND_HALF_DOWN);

but the plus-sign is missing.

2012-04-04 06:31
by hakre
+1 for the alternative approaches (but don't you need %u0+ instead?). Also, wouldn't round($count, -1); work - Alix Axel 2012-04-04 06:38
If you don't want to mess with integer limits: printf("%.0F0+", $count, floor($count * 0.1)); - %u can't handle that much, see http://codepad.org/cQ9Nu61 - hakre 2012-04-04 06:43
Oh! Nevermind, I was confusing %d for a float representation - Alix Axel 2012-04-04 06:54
@AlixAxel: But the hint on round is a good tip. I added it to the answer, thanks to the mode parameter it's actually pretty simple - hakre 2012-04-04 06:57


0

First of all, you will need to use the COUNT() Aggregate function of MySQL to fetch the total number of results, or something like that.

Then you will need to use the modulo operator (%) with 10 as the base, and then deduct that value from the main result, which will be something like this:-

$sql = "SELECT COUNT(id) AS `total_num_records` FROM `table`";
$resource = mysql_query($sql);
$result = mysql_fetch_array($resource);
$totalRecords = $result['total_num_records'];

if ($totalRecords > 0) {
    $remainder = $totalRecords % 10;
    $showHumanFriendlyRecords = ($totalRecords - $remainder);
    $strHumanFriendlyRecords = $showHumanFriendlyRecords . "+";
}

Hope it helps.

2012-04-04 06:33
by Knowledge Craving
Ads