Estimated PHP score
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 number.
I would like it to round up to the nearest 10, so for example for 105 articles I would like to say "100+" etc.
How should I do it?
source to share
printf("%d0+", $count * 0.1);
or
echo substr($count, 0, -1), '0+';
And the round
Docs function also supports it as well:
round($count, -1, PHP_ROUND_HALF_DOWN);
but there is no plus sign.
source to share
First of all, you will need to use the COUNT()
MySQL Aggregate function to get the total number of results or something.
Then you will need to use the modulo ( %
) operator with 10 as the base and then subtract that value from the main result, which will look 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.
source to share