Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit.</p> <p>So, according to the Pastebin you linked (if I happen to give line numbers, it will be the lines I see on Pastebin), I didn't go into the whole code, but for the parts we're interested in. Your problem is that you calculate the grade for all the students, and save it always in the same variable. So that variable, when you try to print it later outside the loop, only contains the grade of the last students that passed in the loop.</p> <p><code>$query7</code> and <code>$query8</code> are the same except you don't retrieve the same fields. It's twice the job for the DB to process, so the best thing is to use only <code>$query7</code> (which contains the score as well), and delete <code>$query8</code> (lines 122 to 130).</p> <p>Your grade calculation (lines 141 to 155) is in a loop on <code>$query8</code>, you don't need it as you're going to calculate one grade for one score, not all at once. So, just delete the loop, keep what's inside.</p> <p>The inside of the said loop (lines 142 to 155), put it in a function, like I did in my first post:</p> <pre><code>function makeGrade($score) { //just a rewrite of your own code, exactly the same purpose if($score&gt;=70) return 'A'; if($score&gt;=60) return 'B'; if($score&gt;=50) return 'C'; if($score&gt;=45) return 'D'; if($score&gt;=40) return 'E'; return 'F'; } </code></pre> <p>On the lines 170 to 188, you loop on <code>$query7</code> to print the data, and on line 182 you print <code>$grade</code>. Instead of printing <code>$grade</code>, just call the function with the field "Score" that you retrieve in <code>$query7</code>:</p> <pre><code>echo makeGrade($row['Score']); </code></pre> <p>Now it should give the right grade for each student and each course. By the way, here you don't need a grade field in your DB, so you can delete it.</p> <p>An alternate way of doing is to have the grade inside the DB (but anyway you don't need <code>$query8</code>). First, you need to set it when the code inserts the rows in <code>MAINTABLE</code> (some other file I suppose), using the same <code>makeGrade()</code> function (though in this case you must declare the function where both scripts can access it, like in an include file). Second, for the existing rows, you'd need to run a series of <code>UPDATE</code>, like this:</p> <pre><code>UPDATE MAINTABLE SET grade='A' WHERE score&gt;=70; UPDATE MAINTABLE SET grade='B' WHERE score&gt;=60 AND score&lt;70; UPDATE MAINTABLE SET grade='C' WHERE score&gt;=50 AND score&lt;60; UPDATE MAINTABLE SET grade='D' WHERE score&gt;=45 AND score&lt;50; UPDATE MAINTABLE SET grade='E' WHERE score&gt;=40 AND score&lt;45; UPDATE MAINTABLE SET grade='F' WHERE score&lt;40; </code></pre> <p>Then you just have to add <code>grade</code> in <code>$query7</code>'s <code>SELECT</code> part, and use <code>$row['grade']</code> on line 182.</p> <p>Make your choice :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload