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?

+3


source to share


5 answers


Pretty simple:

floor($num / 10) * 10

      



docs

+5


source


To do something like this you have to use log 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 is better to do what you asked.

+3


source


$count = 105;
$nearest10 = floor($count / 10) * 10;

      

+2


source


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.

+1


source


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.

0


source







All Articles