Php to calculate marks and award grades - php

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';
}
?>

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 ";
} ?>

error Column count doesn't match value count at row 1 while inserting data to my db

I have this e-commerce site I am working on. Now I keep getting that error every time I try to insert data to the db. I am trying to insert details on table buyer and message but I don't know where the error is.
I don't know where the error is please help me out:
My code:
$buyer_name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$mobile_no = $_POST['mobile_no'];
$id_no = $_POST['id_no'];
$total = $_POST['total'];
echo 'Home<br>';
echo 'Thank you for shopping at our Thread online store. And the following is the data you enter.';
echo '<p>The total cost for the purchase of products is:Tsh '.$total.
'/=, and the cost can be send through the M-Pesa mobile number +255 757 393 937, Airtel Money: # and Tigo-Pesa: #.</p>';
echo '<p>And the goods will be sent to the address below:</p>';
echo '<p>Name : '.$buyer_name.
'<br>';
echo '<p>Address and Location : '.$address.
'</p>';
echo '<p>Total Price : '.$total.
'</p>';
echo '<p>With Details : </p>';
$p = mysql_query("SELECT * FROM buyer");
$cek = mysql_fetch_array($p);
$c1 = $cek['name'];
$c2 = $cek['address'];
$c3 = $cek['email'];
$c4 = $cek['mobile_no'];
$c5 = $cek['id_no'];
if ($c1 == $buyer_name && $c2 == $address && $c3 == $email && $c4 == $mobile_no && $c5 == $id_no) {
$i = 1;
foreach($_SESSION as $name => $value) {
if ($value > 0) {
if (substr($name, 0, 5) == 'cart_') {
$id = substr($name, 5, (strlen($name) - 5));
$get = mysql_query("SELECT * FROM products WHERE product_id='$id'");
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['product_price'] * $value;
echo ' < table class = "table table-striped" >
< thead >
< tr >
< th > # < /th> < th > Product ID < /th> < th > Product Name < /th> < th > Quantity < /th> < /tr> < /thead> < tbody >
< tr >
< td > '.$i.' < /td> < td > '.$get_row['
product_id '].' < /td> < td > '.$get_row['
product_title '].' < /td> < td > '.$value.' < /td> < /tr> < /tbody < /table>';
//<p>'.$i.' '.$get_row['product_id'].' '.$get_row['product_title'].' '.$value.' SubTotal : Tsh '.$sub.' /=</p>
$getBuyer = mysql_query("SELECT buyer.id, buyer.name, buyer.address, products.product_id, products.product_title FROM buyer, products WHERE name='$buyer_name' AND address='$address'") or die(mysql_error());
$data = mysql_fetch_array($getBuyer);
$pemb = $data['id'];
$na = $data['name'];
$al = $data['address'];
$ib = $get_row['product_id'];
$nb = $get_row['product_title'];
//echo $total;
$i++;
}
}
//#$total += $sub;
mysql_query("INSERT INTO message VALUES('', '$pemb', '$na', '$al', '$ib', '$nb', '$value', '$sub', now()) ") or die(mysql_error());
}
}
} else {
//Insert Data of the buyer to the db
$query = mysql_query("INSERT INTO buyer VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());
$i = 1;
foreach($_SESSION as $name => $value) {
if ($value > 0) {
if (substr($name, 0, 5) == 'cart_') {
$id = substr($name, 5, (strlen($name) - 5));
$get = mysql_query("SELECT * FROM products WHERE product_id='$id'");
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['product_price'] * $value;
echo ' < table class = "table table-striped" >
< thead >
< tr >
< th > # < /th> < th > Product ID < /th> < th > Product Name < /th> < th > Quantity < /th> < /tr> < /thead> < tbody >
< tr >
< td > '.$i.' < /td> < td > '.$get_row['
product_id '].' < /td> < td > '.$get_row['
product_title '].' < /td> < td > '.$value.' < /td> < /tr> < /tbody < /table>';
$getBuyer = mysql_query(" SELECT buyer.id, buyer.name, buyer.address, products.product_id, products.product_title FROM buyer, products WHERE name='$buyer_name' AND address='$address'") or die(mysql_error());
$data = mysql_fetch_array($getBuyer);
$pemb = $data['id'];
$na = $data['name'];
$al = $data['address'];
$ib = $get_row['product_id'];
$nb = $get_row['product_title'];
//echo $total;
$i++;
}
}
//#$total += $sub;
mysql_query("INSERT INTO message VALUES('', '$pemb', '$na', '$al', '$ib', '$nb', '$value', '$sub', now()) ") or die(mysql_error());
}
}
}
/*if ($query)
{
header('location:index.php');
}*/
session_destroy();
You must define field name for insert
Instead
$query = mysql_query("INSERT INTO buyer VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());
Use like this for insert to buyer
$query = mysql_query("INSERT INTO buyer(name,email,address,mobile,id) VALUES( '$buyer_name', '$email', '$address', '$mobile_no', '$id_no')") or die(mysql_error());

Adding points upon giving a rank

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;
}
?>

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

Returning a default grade from a score value

Here is my problem. I upload a csv file containing two columns (student Number and Score). But when i form an HTML table from the data, i'll like to have a column display a grade according to the score uploaded... Also all the courses that have a score >40 should all be put into another table. Below is the loop i'm trying to make work, but it's not getting me anywhere near it.
Thanks for the help. I most appreciate it. Thanks
while ($row4 = mysql_fetch_assoc($query4)) {
if ($row4['score'] >= 70) {
$grade = A;
}
elseif ($row4['score'] >= 60) {
$grade = B;
}
elseif ($row4['score'] >= 50) {
$grade = C;
}
elseif ($row4['score'] >= 45) {
$grade = D;
}
elseif($row4['score'] >= 40) {
$grade = E;
}
elseif($row4['score'] >= 40) {
$grade = F;
}else {
$grade = AR;
}
}
If I understand correctly, the following should do what you are looking for. I assume that your Course ID and Student ID are in the database row, but you will need to update those if not.
<?php
define('GRADE_A_MIN', 70);
define('GRADE_B_MIN', 60);
define('GRADE_C_MIN', 50);
define('GRADE_D_MIN', 45);
define('GRADE_E_MIN', 40);
define('GRADE_F_MIN', 35);
$table_one_values = array(); // Used to create table for students with scores 40+
$table_two_values = array();
while ($row4 = mysql_fetch_assoc($query4)) {
$score = $row4['score'];
$destination_table = 'two';
if ( $score >= GRADE_A_MIN ) {
$grade = 'A';
$destination_table = 'one';
} elseif ( $score >= GRADE_B_MIN ) {
$grade = 'B';
$destination_table = 'one';
} elseif ( $score >= GRADE_C_MIN ) {
$grade = 'C';
$destination_table = 'one';
} elseif ( $score >= GRADE_D_MIN ) {
$grade = 'D';
$destination_table = 'one';
} elseif ( $score >= GRADE_E_MIN ) {
$grade = 'E';
$destination_table = 'one';
} elseif ( $score >= GRADE_F_MIN ) {
$grade = 'F';
} else {
$grade = 'AR';
}
$table = 'table_'.$destination_table.'_values';
$$table[] = array(
'course_id' => $row4['course_id'],
'student_id' => $row4['student_id'],
'score' => $score,
'grade' => $grade
);
}
Some problems:
Your $grade variable gets overwritten in the loop so you only will have the last value at the end. As your row contains a student number as well, you could do something like: $grade[$row4['number']] = 'A'; to link that specific grade to a specific student number. Note that you would set your array before the loop: $grade = array();
Your assignment is wrong, unless A, B, etc. are constants. It should probably be 'A' instead of A, etc. So: $grade[$row4['number']] = 'C';, etc.

Categories