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?
Quite simple:
floor($num / 10) * 10
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.
$count = 105;
$nearest10 = floor($count / 10) * 10;
printf("%d0+", $count * 0.1);
or
echo substr($count, 0, -1), '0+';
And the round
Docs function does support it out of the box, too:
round($count, -1, PHP_ROUND_HALF_DOWN);
but the plus-sign is missing.
%u0+
instead?). Also, wouldn't round($count, -1);
work - Alix Axel 2012-04-04 06:38
printf("%.0F0+", $count, floor($count * 0.1));
- %u
can't handle that much, see http://codepad.org/cQ9Nu61 - hakre 2012-04-04 06:43
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
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.