Adding points upon giving a rank - php

Right so i have a promotion button setup. Every promotion someone receives it gives them earning points to their profile, But every rank has different points depending on how high it is or low it is. I'm having a hard time trying to figure out how to get the points to scale with their ranks. My database column for it is known as: rank and the ranks are known as numbers in the database in which when it shows externally they all have a name from a function. So ie; Rank 1, 2, 3, 4 would equal say Noob, Member, Elite, God. Anyway if you can help me find out where my error is that would be great, kinda new to php. The issue I'm having is that the points are not being given to the person. They get promoted fine but no points.
promote.php
<?php include_once('../classes/check.class.php'); ?>
<?php include('../config/db.php'); ?>
<?php include('functionsprom.php'); ?>
<?php if( protectThis("1, 2, 3") ) : ?>
<?php
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$date = date('d M y');
$prom = promote;
$result = mysql_query("UPDATE players SET date='$date', rank = rank + 1, exp = exp + '$prom' WHERE id=$id", $db1)
or die(mysql_error());
require_once("../config/logdb.php");
include('../logs.php');
write_mysql_log($promote, $db);
header("Location: ../home.php");
}
else
{
header("Location: home.php");
}
?>
<?php endif; ?>
functionsprom.php
<?php
include('functionsrank.php');
mysql_select_db('pts_player');
$rank = "SELECT rank FROM players WHERE id = $id";
function promote($rank) {
if($rank == 1):
$prom = 0;
elseif($rank == 2):
$prom = 0;
elseif($rank == 3):
$prom = 0;
elseif($rank == 4 ):
$prom = 10;
elseif($rank == 5):
$prom = 20;
elseif($rank == 6):
$prom = 30;
elseif($rank == 7):
$prom = 45;
elseif($rank == 8):
$prom = 45;
elseif($rank == 9):
$prom = 55;
elseif($rank == 10):
$prom = 60;
elseif($rank == 11):
$prom = 75;
elseif($rank == 12):
$prom = 75;
elseif($rank == 13):
$prom = 0;
elseif($rank == 14):
$prom = 0;
elseif($rank == 15):
$prom = 100;
elseif($rank == 16):
$prom = 200;
elseif($rank == 17):
$prom = 400;
elseif($rank == 18):
$prom = 600;
elseif($rank == 19):
$prom = 800;
elseif($rank == 20):
$prom = 900;
elseif($rank == 21):
$prom = 1000;
elseif($rank == 22):
$prom = 1200;
elseif($rank == 23):
$prom = 1400;
elseif($rank == 24):
$prom = 0;
endif;
return $rank;
}
?>

Related

Display students grades in alphabetical order

I've been able to display the number of times each grade appears on a student's report sheet. However, they are not sorted alphabetically.
grade image
The image above shows that the student got 2Bs, 4B+, 3As, and 1E. My code displays it like this.
But I want the grades to be displayed in alphabetical order like this 3As, 2Bs, 4B+, and 1E.
How do I do that?
Here is my code
<?php $i = 1;
$total = 0;
$count = count($subjectScores);
$grades_count = [];
foreach ($subjectScores as $value) { ?>
<?php
if ($value->tot_score >= 90 && $value->tot_score <= 100) {
$grade = 'A+';
$remark = 'DISTINCTION';
} elseif ($value->tot_score >= 80 && $value->tot_score <= 89.99) {
$grade = 'A';
$remark = 'EXCELLENT';
} elseif ($value->tot_score >= 70 && $value->tot_score <= 79.99) {
$grade = 'B+';
$remark = 'VERY GOOD';
} elseif ($value->tot_score >= 60 && $value->tot_score <= 69.99) {
$grade = 'B';
$remark = 'GOOD';
} elseif ($value->tot_score >= 50 && $value->tot_score <= 59.99) {
$grade = 'C';
$remark = 'ABOVE AVERAGE';
} elseif ($value->tot_score >= 45 && $value->tot_score <= 49.99) {
$grade = 'D';
$remark = 'AVERAGE';
} elseif ($value->tot_score >= 40 && $value->tot_score <= 44.99) {
$grade = 'E';
$remark = 'FAIR';
} elseif ($value->tot_score >= 0 && $value->tot_score <= 39.99) {
$grade = 'F';
$remark = 'NEEDS SERIOUS IMPROVEMENT';
}
// declare count for grade initially with 0
if(isset($grades_count[$grade]) === false) {
$grades_count[$grade] = 0;
}
{
// increment count for given grade
$grades_count[$grade]++;
}
?>
<?php foreach ($grades_count as $grade=>$count) {
echo "$count$grade ";
} ?>

Change text color based on condition

is there an example to change color of the text based on conditions?
for example i want to make condition when $hasil>=80 it will turn green color, $hasil>=70 yellow, and below that is red color.
here's my logic code
while ($data = mysqli_fetch_assoc($result))
{
$n1 = ($data["nilai_output"]) * 0.7;
$n2 = ($data["nilai_atasan"]) * 0.1;
$n3 = ($data["nilai_learning"]) * 0.1;
$n4 = ($data["nilai_kedisiplinan"]) * 0.05;
$n5 = ($data["nilai_5r"]) * 0.05;
$hasil = ($n1 + $n2 + $n3 + $n4 + $n5);
if ($hasil >= 95)
{
$grade = 1.25;
$ikk = "istemewa";
}
elseif ($hasil >= 90)
{
$grade = 1.10;
$ikk = "Sangat Memuaskan";
}
elseif ($hasil >= 85)
{
$grade = 1.00;
$ikk = "Memuaskan";
}
elseif ($hasil >= 80)
{
$grade = 0.90;
$ikk = "Cukup Memuaskan";
}
elseif ($hasil >= 75)
{
$grade = 0.75;
$ikk = "Memadai";
}
elseif ($hasil >= 70)
{
$grade = 0.50;
$ikk = "Kurang Memadai";
}
elseif ($hasil >= 1)
{
$grade = 0.25;
$ikk = "Tidak Memadai";
}
else
{
$ikk = "Tidak Berkontribusi";
}
$no++;
and here's my table column
<td class="font-weight-bold text-danger"><?php echo $hasil;?></td>
<td class="font-weight-bold text-danger"><?php echo $grade;?></td>
<td class="font-weight-bold text-danger"><?php echo $ikk;?></td>
i want that those three column is using color condition
thanks before
Try like this
if ($hasil >= 80) {
$grade = 0.90;
$color = 'green';
$ikk = "Cukup Memuaskan";
}elseif($hasil >= 70 && $hasil < 80 ) {
$grade = 0.75;
$color = 'yellow';
$ikk = "Memadai";
}elseif($hasil >= 1 && $hasil < 70 ) {
$grade = 0.50;
$color = 'red';
$ikk = "Kurang Memadai";
}else{
$ikk = "Tidak Berkontribusi";
}
<td class="font-weight-bold text-danger" style="color:<?=$color;?>"><?php echo $hasil;?></td>
I hope this is helps you!
there are many ways to do what you want. Here's one.
if ($hasil >= 80) {
$color = 'green';
}
elseif ($hasil >= 70) {
$color = 'yellow';
}
else {
$color = 'red';
}
then inside your html
<td class="font-weight-bold text-danger <?php echo $color ?>"><?php echo $hasil ?></td>
and finally css
.green {
color: green;
}
.yellow {
color: yellow;
}
.red {
color: red;
}

Php to calculate marks and award grades

Just wondering what is the problem with my below php, I am trying to insert into two tables marks and results. For the first type of exams I want it to be inserted in the two tables. Second type of exams to be inserted to marks and then update results table, that is to take the marks of the previous exams and add together the assign grade. When I post the first marks, it goes well. Now the second marks is the there is problem; marks table will be inserted successfully but updating results table gives different results, result column either become 0 or 1, and grade remains the same. Can anyone study my codes below and tell me where there is problem please or any other way to do this! I will appreciated. Thank you.
<?php
include('../database/dbcon.php');
$tearcher_post_id = $_POST['tearcher_id'];
$year_id = $_POST['year_id'];
$term_id = $_POST['term_id'];
$subject_post_code = $_POST['subject_code'];
$student_no = $_POST['student_no'];
$exam_id = $_POST['exam_id'];
$class_id = $_POST['class_id'];
$marks = $_POST['marks'];
$grade = '';
if($marks >=0 && $marks<= 19){
$grade = 'E';
}else if($marks > 19 && $marks <= 29){
$grade = 'D-';
}else if($marks > 29 && $marks <=34){
$grade = 'D';
}else if($marks > 34 && $marks <=39){
$grade = 'D+';
}else if($marks > 39 && $marks <=44){
$grade = 'C-';
}else if($marks > 44 && $marks <= 49){
$grade = 'C';
}else if($marks > 49 && $marks <= 54){
$grade = 'C+';
}else if($marks > 54 && $marks <= 59){
$grade = 'B-';
}else if($marks > 59 && $marks <= 64){
$grade = 'B';
}else if($marks > 64 && $marks <= 69){
$grade = 'B+';
}else if($marks > 69 && $marks <= 79){
$grade = 'A-';
}else if($marks > 79 && $marks <= 100){
$grade = 'A';
}
$query_details = mysql_query(
"SELECT staff.staff_id, students.student_id, subjects.subject_id
FROM staff, students, subjects
WHERE staff.id_number='$tearcher_post_id'
AND students.admission_no='$student_no'
AND subjects.subject_code='$subject_post_code'")or die(mysql_error());
$row_details = mysql_fetch_array($query_details);
$num_details = mysql_num_rows($query_details);
if($num_details > 0){
$staff_id= $row_details['staff_id'];
$student_id= $row_details['student_id'];
$subject_id= $row_details['subject_id'];
$query_marks = mysql_query("SELECT *
FROM marks
WHERE student_id='$student_id'
AND year_id='$year_id'
AND exam_type_id='$exam_id'
AND subject_id='$subject_id'")or die(mysql_error());
$num_marks = mysql_num_rows($query_marks);
if($num_marks > 0){
echo 'marks_excist';
}else{
$query = mysql_query("INSERT
INTO marks
(student_id,
staff_id,
term_id,
year_id,
exam_type_id,
subject_id,
class_id,
marks)
values(
'$student_id',
'$staff_id',
'$term_id',
'$year_id',
'$exam_id',
'$subject_id',
'$class_id',
'$marks' )")or die(mysql_error());
if($query){
$query_results = mysql_query("SELECT *
FROM results
WHERE student_id='$student_id'
AND term_id='$term_id'
AND year_id='$year_id'
AND subject_id='$subject_id'")or die(mysql_error());
$row_results = mysql_fetch_array($query_results);
$num_results = mysql_num_rows($query_results);
if($num_results > 0){
$id= $row_results['result_id'];
$results = $row_results['results'];
$total = $results + $marks;
$grade = '';
if($total >=0 && $total<= 19){
$grade = 'E';
}else if($total > 19 && $total <= 29){
$grade = 'D-';
}else if($total > 29 && $total <=34){
$grade = 'D';
}else if($total > 34 && $total <=39){
$grade = 'D+';
}else if($total > 39 && $total <=44){
$grade = 'C-';
}else if($total > 44 && $total <= 49){
$grade = 'C';
}else if($total > 49 && $total <= 54){
$grade = 'C+';
}else if($total > 54 && $total <= 59){
$grade = 'B-';
}else if($total > 59 && $total <= 64){
$grade = 'B';
}else if($total > 64 && $total <= 69){
$grade = 'B+';
}else if($total > 69 && $total <= 79){
$grade = 'A-';
}else if($total > 79 && $total <= 100){
$grade = 'A';
}
mysql_query("UPDATE results
SET results = '$total' AND grade = '$grade'
WHERE result_id = '$id'") or die(mysql_error());
echo 'true_marks';
}else{
mysql_query("INSERT
INTO results
(student_id,
year_id,
class_id,
term_id,
subject_id,
results,
grade)
values(
'$student_id',
'$year_id',
'$class_id',
'$term_id',
'$subject_id',
'$marks',
'$grade' )")or die(mysql_error());
}
}else{
echo 'false_marks';
}
}
}else{
echo 'details_dont_excist';
}
?>

How should I write this specific condition in php?

The Challenge is:
If the book is returned on or before the expected return date, no
fine will be charged (i.e.: fine=0).
If the book is returned after the expected return day but still
within the same calendar month and year as the expected return date,
fine=15 Hackos × (the number of days late).
If the book is returned after the expected return month but still
within the same calendar year as the expected return date, the
fine=500 Hackos × (the number of months late).
If the book is returned after the calendar year in which it was
expected, there is a fixed fine of 10000 Hackos.
And My code is:
<?php
$expectedDay = "6";
$expectedMonth = "6";
$expectedYear = "2015";
$returnDay = "9";
$returnMonth = "6";
$returnYear = "2015";
if ($expectedDay >= $returnDay && $expectedMonth >= $returnMonth && $expectedYear >= $returnYear) {
echo "Fine = 0";
}elseif ($expectedDay < $returnDay && $expectedMonth == $returnMonth && $expectedYear == $returnYear) {
$fine = 15 * ($returnDay-$expectedDay);
echo "Fine = ".$fine;
}elseif (($expectedDay <= $returnDay || $expectedDay >= $returnDay) && $expectedMonth < $returnMonth && $expectedYear == $returnYear) {
$fine = 500 * ($returnMonth-$expectedMonth);
echo "Fine = ".$fine;
}else{
echo "Fine = 1000";
}
?>
Its running well.But failed when the input is:
$expectedDay = "28";
$expectedMonth = "2";
$expectedYear = "2015";
$returnDay = "15";
$returnMonth = "4";
$returnYear = "2015";
How do I write for this condition?
Note: This is not a business logic. It is just Practice purposes. I am a beginner in PHP.
Something along these lines:
function calculateLateFees(DateTime $deadline, DateTime $returned) {
if ($returned <= $deadline) {
return 0;
}
if ($returned->format('Y') > $deadline->format('Y')) {
return 10000;
}
if ($returned->format('n') > $deadline->format('n')) {
return ($returned->format('n') - $deadline->format('n')) * 500;
}
return $deadline->diff($returned)->days * 15;
}
$deadline = new DateTime('2015-02-28');
$returned = new DateTime('2015-04-15');
echo calculateLateFees($deadline, $returned), ' Hackos';
You do not have to compare dates when the month condition is checked.
<?php
$expectedDay = "6";
$expectedMonth = "6";
$expectedYear = "2015";
$returnDay = "9";
$returnMonth = "6";
$returnYear = "2015";
$returnDate = new DateTime($returnDay.'-'.$returnMonth.'-'.$returnYear);
$expectedDate = new DateTime($expectedDay.'-'.$expectedMonth.'-'.$expectedYear);
if ($returnDate <= $expectedDate) {
echo "Fine = 0";
}elseif ($expectedDay < $returnDay && $expectedMonth == $returnMonth && $expectedYear == $returnYear) {
$fine = 15 * ($returnDay-$expectedDay);
echo "Fine = ".$fine;
}elseif ($expectedMonth < $returnMonth && $expectedYear == $returnYear) {
$fine = 500 * ($returnMonth-$expectedMonth);
echo "Fine = ".$fine;
}else{
echo "Fine = 1000";
}
?>
Try that.
First, I must say that this is a very unfair calculation of fines. What if the expected day is 31.12.2016 and the book was returned 01.01.2017? According to your method the a fine of 10000 will be imposed for the lapse of one day just because it was across the margin of two separate years.
I would recommend you to calculate the fine according to the number of days late.
$lateDays = date_diff($expectedDate, $returnDate, false);
if ($lateDays > 0) {
if ($lateDays < 30)
$fine = 15 * $lateDays
else
if ($lateDays > 365)
$fine = 10000
else
$fine = 500 * $lateDays / 30
}
if($returnYear > $expectedYear){
$fine = 1000;
}
else{
if($returnMonth > $expectedMonth ){
$fine = 500 * ($returnMonth-$expectedMonth);
}
else{
if($returnDay>$expectedDay){
$fine = $returnDay - $expectedDay;
}
else{
$fine = 0;
}
}
}
echo $fine;
Above logic is built taken into consideration that months in number crossed will take complete month cost example :
if expected return date is 28/04/2016 and actual return date is 02/05/2016 then since month is changed the below algorithm takes fine #complete month.
If you want exact days difference to be considered for months/year calculation then we can write a different logic all together
<?php
$expectedDay = "6";
$expectedMonth = "6";
$expectedYear = "2015";
$returnDay = "9";
$returnMonth = "6";
$returnYear = "2015";
$expectedate=$expectedYear.'-'.$expectedMonth.'-'.$expectedDay;
$returndate=$returnYear.'-'.$returnMonth.'-'.$returnDay;
$expected=date_create($expectedate);
$return=date_create($returndate);
$interval=date_diff($expected, $return);
$valor=$interval->format('%R%a');
if ($valor>0) {
if ($returnMonth==$expectedMonth && $returnYear==$expectedYear) echo "Fine=".(15*$valor);
if ($returnMonth!=$expectedMonth && $returnYear==$expectedYear) echo "Fine=".(500*($returnMonth-$expectedMonth));
if ($returnYear!=$expectedYear) echo "Fine=1000";
} else echo "Fine=0";
?>

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();

Categories