Jump to content

Php Mysql count and percentage display


Go to solution Solved by Barand,

Recommended Posts

What I have is a working page that shows a percentage bar for each goal of "Play Levels" for each user. It all works beautifully. However, once the goal gets passed 100%, it continues to count. This might be something easy for you all here, but I'm not sure what the math code would be to include to make it stop at 100% in the display. Below is a sample of the code I have to count. The first one DOES stop at 100% because it's just devided by itself. That was easy enough (because the 1st goal is total 1)
Also the screen shots below show a user that has not met 100% of the goal, and then a user that has exceeded the goal by large amounts. As you can see, it gets too 100 and fills the bar, then keeps counting.

OBJECTIVE: To make the bar fill and stop at 100% and read as such once the goal is met (even if exceeded)

ps... bonus THANK YOU's if someone can tell me some css to change the colors of the bars. It's from a bootstrap code I pasted from W2Schools (below)
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
 

//// goal met for level 1 and 2 ////

$totalgoal1 = number_format($num_rows['count']/$num_rows['count']*100);
$totalgoal2 = number_format($num_rows['count']/49*100);


//// the progress bar for level 2 with goal of completing 49 entries ////

<div class="container" align="center" style="width: 350px"> 
                                <div class="progress" align="center" style="height: 20px"> 
                                  <div valign="middle" class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="50" aria-valuemin="0" aria-valuemax="100" style="width:<?php echo $totalgoal2; ?>%" align="center"> 
                                    <b><font face="Verdana, Arial, Helvetica, sans-serif" size="4"> 
                                    <div align="left">&nbsp;&nbsp;<?php echo $totalgoal2; ?> 
                                      %</div>
                                    </font></b> </div>

User 1 has 26 entries. The progress bar works perfect
image.png.8658f2c0a37b81b7d593d54dacc2334e.png

User 2 has 329 entries. The progress bar filled up and keeps countng past 100% completed

image.png.8ca5a2ef6fdaeadbc0a2d1428488c713.png

Edited by PNewCode
Link to comment
https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/
Share on other sites

@Barand Thank you for that. Works perfect. If I'm to understand right, that works because it's saying the total is 49 with , 49 and then divide that goal times 100 to show it's maxed out, correct?
That kind of math isn't my strong suit so the math part I don't really understand. But what is making it work makes sense if I got what I just broke it down right

Edited by PNewCode
22 minutes ago, Barand said:
min($num_rows['count'], 49)

That expression returns whichever is the smaller number - the count or 49. So if the count is, say, 40, then it returns 40. If it is anything over 49 it returns 49 (100%) as that is the smaller number.

  • Like 1

@Barand I'm not sure if this next question should be a new post or not, but do you know of a way to make that display the count down to the exact decimal? For example in my screen shots above, user 1 is actually at 53.43% but it only shows 53%

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.