1

I have a table in MYSQL database with 14586 records.

each record has a row called active with a value of 0.

at some point the value of this active row becomes 1.

what i need to do is to simply display a bootstrap progress bar showing the fields that have the active=1 in the progress bar.

I tried something like this:

PHP:

$result = mysqli_query($db_conx, "SELECT count(*) FROM mytable WHERE active=1");
$row = mysqli_fetch_row($result);
$num = $row[0];

Bootstrap HTML:

  <div class="progress">
    <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="40" aria-valuemin="0" aria-valuemax="100" style="width:<?php echo $num; ?>%">
      <?php echo $num; ?>%
    </div>
     </div>

But this is wrong because I get a silly number like this 14586% in the progress bar.

Could someone please advice on this?

any help would be great.

Thanks in advance.

8
  • What should the number be Commented Sep 6, 2017 at 18:41
  • @Akintunde, the progress bar is usually from 0 to 100. Commented Sep 6, 2017 at 18:41
  • You can perform basic arithmetic. Get the number of rows with the active status and divide by 100% or divide by the total number of rows in the table Commented Sep 6, 2017 at 18:43
  • 1
    I would use cout(id) or count(active) instead of selecting all columns with count(*) to speed up your query. Commented Sep 6, 2017 at 18:50
  • 1
    COUNT(*) can sometimes out perform counting a specific column. This allows MySQL to try to select the best indexed column for your query. The other thing to note is if there are null values in the column you select to be counted those values will be ignored. Always test multiple methods of counting rows for your application as what works best for someone else may not work best for you. Commented Sep 6, 2017 at 18:59

3 Answers 3

1

You need to get the total records with no WHERE filter AND the total records with the WHERE active=1. This will allow you to calculate a percent value from 0 to 100.

[total active=1] / [total records] * 100 = [percent from 0 - 100]

Example: 7293 / 14586 = 0.5 * 100 = 50%

See: https://en.wikipedia.org/wiki/Percentage

Sign up to request clarification or add additional context in comments.

Comments

1

You will need the total rows in your table, in your case that is 14,586 which is 100%. The get the number as you are in your example and divide by 100. Then you will use round((float)$num * 100 ) . '%'; to round the number to get your percent.

Comments

0

Suppose you want to display the percentage to 2 decimal places, then you can use this as your mysqli_query statement:

"select format(100*sum(active)/count(active),2) from mytable"

To get a rounded up figure, you change the last parameter of the above format function to 0.

Explanation: sum(active) adds up the ones, which is equivalent to counting them, assuming that the only other values in that column are zeros or nulls. On the other hand count(active) counts both the ones and the zeros, ignoring the nulls, if any.

In case you want your denominator total to include even rows where the active column is neither 0 nor 1, including the null values, then instead of count(active) you would use count(*) which would count all records in the table.

Also in case you are not sure whether the active column might have some other figures (possibly erroneously, e.g. 2, 5, etc.) then instead of sum(active) use the more precise sum(active=1) which means sum of active where active = 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.