I've two tables (examresults and gradings). I'm trying to give grades according to score value. here are my tables
examresults
ID | SUBJECT | SCORE |
---|---|---|
1 | English | 100 |
1 | Math | 80 |
1 | Bios | 40 |
gradings
GNAME | MAXSCORE |
---|---|
A | 100 |
B | 80 |
C | 64 |
D | 40 |
F | 34 |
and here is what I've tried
my code
$counter = 0;
$stid=1;
if($sql=("SELECT * from `examresults` where `id`='$stid'"))
{
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
echo '<h3 class="text-center">Examination Results</h3>
';
echo'
<table class="table table-bordered">
<thead>
<tr style="background-color: #F2F2F2;">
<td class="text-bold">Sl No</td>
<td class="text-bold">Subject</td>
<td class="text-bold">Score</td>
<td class="text-bold">Grade</td>
</tr>
</thead>
';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$score=$row['score'];
$check_for_grade = $conn->prepare("SELECT IF(maxscore>='$score',gname,IF(maxscore<='$score',gname,'')) FROM gradings");
$check_for_grade->execute();
$mypy = $check_for_grade->fetch();
$grade = $mypy[0];
echo'
<tbody>
<tr role="row" class="odd">
<td>'.++$Counter.'</td>
<td style="padding-left: 50px;">'.$row['subject'].'</td>
<td>'.$row['score'].'</td>
<td>'.$grade.'</td>
</tr>
';
}
}
echo'
</thead>
</tbody>
</table>
';
The problem is that, it gives me wrong info and I think problem exist in my IF statement, please any help.
Here is Output
ID | SUBJECT | SCORE | GRADE |
---|---|---|---|
1 | English | 100 | A |
1 | Math | 80 | A |
1 | Bios | 40 | A |
What I need is
ID | SUBJECT | SCORE | GRADE |
---|---|---|---|
1 | English | 100 | A |
1 | Math | 80 | B |
1 | Bios | 40 | D |