Home > OS >  PHP compare in percent
PHP compare in percent

Time:07-18

i am seeking to make a Programm that can Take different Numbers from the Database and compares Thema with a percent

Example: Database Numbers 1:100 User: 65

   Output: Your Numbers ist bigger than 35 percent of the Users

How can i do that?

CodePudding user response:

You need to execute 2 database queries:

  1. SELECT COUNT(*) FROM table - will get total count of the users (let's say you store result in $total variable)
  2. SELECT COUNT(*) FROM table WHERE number < 65 - will count users that have the number fields less than 65 (let's name this $selection variable)

After that, it's simple math:

$percent = $selection / $total * 100;

echo "Your Numbers is bigger than {$percent} percent of the Users";
  •  Tags:  
  • php
  • Related