HTML PHP Math : Fill a progress bar using rank values - php

I got a progress bar to be filled showing remaining rank points to next level.
Ex: got 25 points and next rank is at 50 points so the bar must to be filled at 50%, if i got 40 point must to be 75% filled etc.
<div class="progress-bar" role="progressbar" aria-valuenow="<?php print $rank; ?>" aria-valuemin="0" aria-valuemax="100" style="width: <?php print get_rank($rank) * next_rank($level) / 2; ?>%;">
The get_rank() function take Rank points and convert to remain points minus $rank value, then I'd pass the $rank value to level() function in order to retrieve the level and finally next_rank() function get the level and return the remaining points.
So the used math is (get_rank($rank) * next_rank($level) / 2) / 10.
Using this return the remaining value ( eg. 19 * 20 / 2 = 500 / 10 = 19% ) print in the width 0.25%, how i can reverse the result to get 99.75% instead of 0.25?
For completeness will paste the complete functions:
<?php
function get_rank($rank) {
if ( $rank >= 5 ) { $rem_point = 20 - $rank; }
if ( $rank >= 20 ) { $rem_point = 50 - $rank; }
if ( $rank >= 50 ) { $rem_point = 100 - $rank; }
if ( $rank >= 100 ) { $rem_point = 500 - $rank; }
if ( $rank >= 500 ) { $rem_point = 1000 - $rank; }
if ( $rank >= 1000 ) { $rem_point = 2500 - $rank; }
if ( $rank >= 2500 ) { $rem_point = 5000 - $rank; }
return $rem_point;
}
function level($rank) {
if ( $rank <= 5 ) { $level = 1; }
elseif ( $rank <= 20 ) { $level = 2; }
elseif ( $rank <= 50 ) { $level = 3; }
elseif ( $rank <= 100 ) { $level = 4; }
elseif ( $rank <= 500 ) { $level = 5; }
elseif ( $rank <= 1000 ) { $level = 6; }
elseif ( $rank <= 2500 ) { $level = 7; }
elseif ( $rank <= 5000 ) { $level = 8; }
return $level;
}
function next_rank($level) {
if ( $level = 1 ) { $next_r = 5; }
elseif ( $level = 2 ) { $next_r = 20; }
elseif ( $level = 3 ) { $next_r = 50; }
elseif ( $level = 4 ) { $next_r = 100; }
elseif ( $level = 5 ) { $next_r = 500; }
elseif ( $level = 6 ) { $next_r = 1000; }
elseif ( $level = 7 ) { $next_r = 2500; }
elseif ( $level = 8 ) { $next_r = 5000; }
return $next_r;
}
?>
Thanks to all who can help.

Replace <?php print get_rank($rank) * next_rank($level) / 2; ?>
by <?php print_r(($rank*100)/(get_rank($rank)+$rank)); ?> should do the job you want.
For function level($rank) if i input level(10) output will be 10 as you are returning $rank without any calculation.

Related

Get user level depending on his/her points number PHP

I have a user points system which gives users points depending on some actions like selling a product or add new post etc...
I want to make a smarter PHP function to set a level for the user depending on his/her points.
Here's how I make this:
function get_user_level( $user_id ) {
$user_points = 3515 // Here I get the number of points that user have from the database
if ( $user_point >= 3000 ) {
$level = '5';
} elseif ( $user_point >= 2000 ) {
$level = '4';
} elseif ( $user_point >= 1500 ) {
$level = '3';
} elseif ( $user_point >= 1000 ) {
$level = '2';
} elseif ( $user_point >= 500 ) {
$level = '1';
} else {
$level = '0';
}
echo 'Level:' . $level;
}
The problem that my function seems very bad and not smarter I want to develop my function to upgrade user level for each 1000 point the user has (Making unlimited levels automatically).
You mean something like:
if ($user_points < 2000)
{
$level = floor($user_points / 500);
}
else
{
$level = 4 + floor(($user_points-2000)/1000);
}
Which yields level 0-4 for 0-2000 points and then one additional level every 1000 points.
function get_user_level( $user_id ) {
$user_points = 3515; // Here I get the number of points that user have from the database
$level = intval($user_points/1000);
echo $level;
}
You could use a switch statement:
<?php
function get_user_level( $user_point )
{
switch (true) {
case $user_point >= 3000:
return 5;
case $user_point >= 2000:
return 4;
case $user_point >= 1500:
return 3;
case $user_point >= 1000:
return 2;
case $user_point >= 500:
return 1;
default:
return 0;
}
}
echo get_user_level(3515); // outputs 5
See it here: https://3v4l.org/TmiAH

Filter an array with PHP

I have this array:
Array
(
[0] => Array
(
[DEB_Date] => 2018-01-06
[DEB_Total] => 100.00
[DEB_Nb_Days] => 181
)
[1] => Array
(
[DEB_Date] => 2018-07-06
[DEB_Total] => 100.00
[DEB_Nb_Days] => 0
)
)
I want to have a sort of report depending the DEB_Nb_Days.
So my script is the following:
// Init the array
$arr['0_to_30']['DEB_Total'] = 0;
$arr['31_to_60']['DEB_Total'] = 0;
$arr['61_to_90']['DEB_Total'] = 0;
$arr['91_to_120']['DEB_Total'] = 0;
$arr['121_and_more']['DEB_Total'] = 0;
// Loop between the debtors
foreach($debtors as $debtor){
if($debtor['DEB_Nb_Days'] >= 0 || $debtor['DEB_Nb_Days'] <= 30) { $arr['0_to_30']['DEB_Total'] += $debtor['DEB_Total']; }
if($debtor['DEB_Nb_Days'] >= 31 || $debtor['PAY_Nb_Days'] <= 60) { $arr['31_to_60']['DEB_Total'] += $debtor['DEB_Total']; }
if($debtor['DEB_Nb_Days'] >= 61 || $debtor['PAY_Nb_Days'] <= 90) { $arr['61_to_90']['DEB_Total'] += $debtor['DEB_Total']; }
if($debtor['DEB_Nb_Days'] >= 91 || $debtor['PAY_Nb_Days'] <= 120) { $arr['91_to_120']['DEB_Total'] += $debtor['DEB_Total']; }
if($debtor['PAY_Nb_Days'] >= 121) { $arr['121_and_more']['DEB_Total'] += $debtor['DEB_Total']; }
}
I should have this:
$arr['0_to_30']['DEB_Total'] = 100;
$arr['31_to_60']['DEB_Total'] = 0;
$arr['61_to_90']['DEB_Total'] = 0;
$arr['91_to_120']['DEB_Total'] = 0;
$arr['121_and_more']['DEB_Total'] = 100;
But actually the result is:
$arr['0_to_30']['DEB_Total'] = 200;
$arr['31_to_60']['DEB_Total'] = 200;
$arr['61_to_90']['DEB_Total'] = 200;
$arr['91_to_120']['DEB_Total'] = 200;
$arr['121_and_more']['DEB_Total'] = 0;
What I'm missed here please ?
Thanks.
You could restructure the set of if conditions to look for the highest first and then use else to reduce the number of comparisons...
foreach ($debtors as $debtor) {
if ($debtor['DEB_Nb_Days'] >= 121) {
$arr['121_and_more']['DEB_Total'] += $debtor['DEB_Total'];
}
else if ($debtor['DEB_Nb_Days'] >= 91) {
$arr['91_to_120']['DEB_Total'] += $debtor['DEB_Total'];
}
else if ($debtor['DEB_Nb_Days'] >= 61) {
$arr['61_to_90']['DEB_Total'] += $debtor['DEB_Total'];
}
else if ($debtor['DEB_Nb_Days'] >= 31) {
$arr['31_to_60']['DEB_Total'] += $debtor['DEB_Total'];
}
else {
$arr['0_to_30']['DEB_Total'] += $debtor['DEB_Total'];
}
}
( This also corrects that at some points your using PAY_Nb_Days instead of DEB_Nb_Days ).

Array Calculation PHP

Again I am stuck with PHP array calculation, please if anyone understands my question try to help me out, let's explain...
[data.txt content]
Peer Call ID Duration Recv: Pack Lost ( %) Jitter Send: Pack Lost ( %) Jitter
139.59.232.196 0bb9262d6a1 00:01:12 0000003558 0000000000 ( 0.00%) 0.0000 0000001177 0000000000 ( 0.00%) 0.0200
139.59.232.196 41283499492 00:00:00 0000000000 0000000000 ( 0.00%) 0.0000 0000000000 0000000000 ( 0.00%) 0.0000
139.59.232.196 7033a541240 00:00:08 0000000000 0000000000 ( 0.00%) 0.0000 0000000019 0000000000 ( 0.00%) 0.0000
3 active SIP channels
PHP Code Starting from here.
$data = file_get_contents('./data.txt', TRUE);
$lines = explode("\n", $data);
foreach ($lines as $line) {
if (!preg_match('/( 0.00%)/', $line)) {
continue;
}
$data = explode(' ', $line);
$list[] = $data;
}
foreach ($list as $qoscalc) {
$average[] = ($qoscalc[17] * 1000 / 2);
$jitter[] = (int)$qoscalc[14];
$packet_loss[] = (int)$qoscalc[13];
}
print_r($average);
Till here the code is working fine it's giving me the output of $average array this >>
Array ( [0] => 10 [1] => 0 [2] => 0 )
After that, I couldn't do this math with the array, if I convert them in variable & I go with 1 data only then code is working fine, but when I try to get the result of all I couldn't make it, please help me if anyone understands my question.
$effective_latency = ($average + $jitter * 2 + 10 );
if ($effective_latency < 160) {
$r_value = 93.2 - ($effective_latency / 40);
} else {
$r_value = 93.2 - ($effective_latency - 120) / 10;
}
$r_value = $r_value - ($packet_loss * 2.5);
$mosresult = 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
$moslist[] = $mosresult;
I want to get all 3 array result, its suppose to be like this example: Array ( [0] => 4.40372901469 [1] => 3.40372901469 [2] => 4.90372901469 )
$i = 0; $t = 0; $e = 0; $g = 0; $f = 0; $p = 0; $b = 0;
foreach ($moslist as $mos) {
$i++;
if ($mos <= "5") {
$qosq = 'Excellent';
$e++;
} else if ($mos <= "4") {
$qosq = 'Good';
$g++;
} else if ($mos < "3") {
$qosq = 'Fair';
$f++;
} else if ($mos <= "2") {
$qosq = 'Poor';
$p++;
} else if ($mos <= "1") {
$qosq = 'Bad';
$b++;
} else {
continue;
}
$t++;
}
echo $qosq, "<br><br>\n";
If I understood the question correctly, the simplest solution would be to create a function for calculating the mosResult
function getMosResult($average,$jitter,$packet_loss)
{
$effective_latency = ($average + $jitter * 2 + 10 );
if ($effective_latency < 160) {
$r_value = 93.2 - ($effective_latency / 40);
} else {
$r_value = 93.2 - ($effective_latency - 120) / 10;
}
$r_value = $r_value - ($packet_loss * 2.5);
return 1 + (0.035) * $r_value + (0.000007) * $r_value * ($r_value - 60) * (100 - $r_value);
}
and then applying the function in a for loop on all the results like so
$length = count($list);
for($i = 0;$i < $length;$i++){
$mosList[]=getMosResult($average[$i],$jitter[$i],$packet_loss[$i]);
}
This solution would not be good for a larger project as you would quickly lose consistency between the three source arrays. For a more solid solution, look into objects or t least associative arrays.

Sum values in each group with a loop

I have a while loop that gives this result:
Userid Point
1 10
1 15
2 5
2 10
3 8
3 2
How can I sum the userid points and output with highest number first, like this:
Userid Point
1 25
2 20
3 10
Is there any "foreach", "for" or any other method that can accomplish such result?
The code:
include ('variables.php');
//Fetch data from matchdata table
$q = "SELECT userid, matchid, homescore, awayscore FROM predictiondata ORDER BY userid ASC";
$r = mysqli_query($mysqli, $q);
while ($row = mysqli_fetch_array($r)) {
//Define predictions
$predhome = $row['homescore'];
$predaway = $row['awayscore'];
//Fetch gameresults
$qres = "SELECT id, homescore, awayscore, bonuspoints FROM matches WHERE id = ".$row['matchid']."";
$rres = mysqli_query($mysqli, $qres);
$result = mysqli_fetch_array($rres);
$homescore = $result['homescore'];
$awayscore = $result['awayscore'];
$bonus = $result['bonuspoints'];
$id = $result['id'];
//Calculate points
if ($homescore == $predhome && $awayscore == $predaway && $homescore != '') { $result_point = $correct_score + $correct_result + $correct_number + $correct_number; }
else if ($homescore == $predhome && $awayscore != $predaway OR $homescore != $predhome && $awayscore == $predaway) { $result_point = $correct_result + $correct_number; }
else if ($predhome > $predaway && $homescore > $awayscore OR $predhome < $predaway && $homescore < $awayscore) { $result_point = $correct_result; }
else if (is_null($predhome) OR $homescore == '') { $result_point = 0; }
else { $result_point = 0; }
if ($homescore == $predhome && $awayscore == $predaway && $homescore != '') { $bonus = $bonus; }
else if (is_null($predhome) OR $homescore == '') { $bonus = 0; }
else { $bonus = 0; }
if (is_null($predhome) OR $homescore == '') { $total_point = 0; }
else { $total_point = $result_point + $bonus; }
//Calculate total round sum
$total_roundsum = $result_point + $bonus;
//echo $username.' - '.$total_roundsum.'<br />';
if($total_roundsum != 0) {
echo $row['userid']. ' - ' .$total_roundsum.'<br />';
}
}
At the moment, the code only echo's the results.
The "variables.php" holds the $correct_score + $correct_result + $correct_number variables.
Assuming the two columns are an associated array -- $users_and_points.
$points_array = array();
foreach ( $users_and_points as $user_id => $point ) {
if( !isset( $points_array[$user_id] ) {
$points_array[$user_id] = 0;
}
$points_array[$user_id] += $point;
}
// newly associated array with calculated totals
$points_array;
Update
Based on your code above instead of echoing build an array of users and points.
echo $row['userid']. ' - ' .$total_roundsum.'<br />';
can be replaced with:
if( !isset( $points_array[$row['userid']] ) {
$points_array[$row['userid']] = 0;
}
$points_array[$row['userid']] += $total_roundsum;
That will give you an associated array of user ids and associated points.
print_r( $points_array );
Note: Set the variable before your loop. Example, $points_array = array();

can anyone help me to replace this code snippet with better approach without using if elseif

In this code snippet ,all the values will be database driven and i need a better replacement for this code.
commission in percentage will be calculated based on the task price range.
$commission1=10;//in percentage
$commission2=20;
$commission3=30;
$taskprice =200;
if($taskprice >=0 && $taskprice <=150)
{
$adminearnings = ($taskprice *$commission1 ) / 100 ;
$Geniepayment = $taskprice - $adminearnings;
}
else if($taskprice >=151 && $taskprice <=300)
{
$adminearnings = ($taskprice * $commission2) / 100 ;
$Geniepayment = $taskprice - $adminearnings;
}
else if($taskprice >=301 && $taskprice <=450)
{
$adminearnings = ($taskprice * $commission3 ) / 100 ;
$Geniepayment = $taskprice - $adminearnings;
}
echo $taskprice.'</br>';
echo $adminearnings.'</br>';
echo $Geniepayment.'</br>';
As for me, I would make a simple function to have fast access to what you need :
function calculate($commission, $taskprice)
{
$arrayCommission = array(0, 10, 20, 30);
$adminearnings = ($taskprice * $arrayCommission[$commission] ) / 100 ;
$Geniepayment = $taskprice - $adminearnings;
return array($taskprice, $adminearnings, $Geniepayment);
}
That would be something like this:
if($taskprice >=0 && $taskprice <=150)
{
$arrayAnswer = calculate(1, $taskprice);
}
elseif($taskprice >=151 && $taskprice <=300)
{
$arrayAnswer = calculate(2, $taskprice);
}
elseif($taskprice >=301 && $taskprice <=450)
{
$arrayAnswer = calculate(3, $taskprice);
}
I agree with the others that the conditionals are required. I would refactor to pull out the calculation, something like this:
<?php
// Returns array(earnings, payment)
//
function getEarningsPayment($taskprice, $commission)
{
$earnings = ($taskprice * $commission) / 100 ;
$payment = $taskprice - $adminearnings;
return array ($earnings, $payment);
}
$commission1=10;//in percentage
$commission2=20;
$commission3=30;
$taskprice =200;
if($taskprice >=0 && $taskprice <=150)
$commission = $commission1;
else if($taskprice >=151 && $taskprice <=300)
$commission = $commission2;
else if($taskprice >=301 && $taskprice <=450)
$commission = $commission3;
list ($adminearnings, $Geniepayment) = getEarningsPayment($taskprice, $commission);
echo $taskprice.'</br>';
echo $adminearnings.'</br>';
echo $Geniepayment.'</br>';
?>
guys how about this idea ..waiting for your feedbacks and suggestions
$commissions = array
(
'0'=>array
(
"commission_start_range"=>"0","commission_end_range"=>"100","commission_amount"=>"10"
),
'1'=>array
(
"commission_start_range"=>"101","commission_end_range"=>"200","commission_amount"=>"20"
),
'2'=>array
(
"commission_start_range"=>"201","commission_end_range"=>"300","commission_amount"=>"30"
)
);
function check_commission($task_price,$commissions)
{
foreach($commissions as $commission_data)
{
$start = $commission_data['commission_start_range'];
$end = $commission_data['commission_end_range'];
$amount= $commission_data['commission_amount'];
if($task_price >=$start && $task_price <=$end)
{
$admin_earnings = ($task_price * $amount) / 100 ;
$genie_payment = $task_price - $admin_earnings;
return $admin_earnings;//echo $admin_earnings;
}
}
}
check_commission(200,$commissions);

Categories