PNewCode Posted June 26, 2023 Share Posted June 26, 2023 (edited) 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"> <?php echo $totalgoal2; ?> %</div> </font></b> </div> User 1 has 26 entries. The progress bar works perfect User 2 has 329 entries. The progress bar filled up and keeps countng past 100% completed Edited June 26, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/ Share on other sites More sharing options...
Solution Barand Posted June 26, 2023 Solution Share Posted June 26, 2023 $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100); 1 Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610012 Share on other sites More sharing options...
PNewCode Posted June 26, 2023 Author Share Posted June 26, 2023 (edited) @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 June 26, 2023 by PNewCode Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610013 Share on other sites More sharing options...
Barand Posted June 26, 2023 Share Posted June 26, 2023 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610014 Share on other sites More sharing options...
PNewCode Posted June 26, 2023 Author Share Posted June 26, 2023 @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% Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610015 Share on other sites More sharing options...
Barand Posted June 26, 2023 Share Posted June 26, 2023 Specify the number of dec places in your call to number_format() Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610016 Share on other sites More sharing options...
PNewCode Posted June 26, 2023 Author Share Posted June 26, 2023 @Barand I took a total GUESS at something and it worked. This was before I saw your reply. But just to share, this is what allowed it to echo the decimal precision $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100); Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610017 Share on other sites More sharing options...
Barand Posted June 26, 2023 Share Posted June 26, 2023 You need to define the required precision $totalgoal2 = number_format(min($num_rows['count'], 49)/49*100, 2); ^ Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610018 Share on other sites More sharing options...
PNewCode Posted June 26, 2023 Author Share Posted June 26, 2023 Right @Barand thats exactly what I did. thank you so much Quote Link to comment https://forums.phpfreaks.com/topic/317010-php-mysql-count-and-percentage-display/#findComment-1610019 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.