I actually had this code working just fine, but I did some things to make it look a lot more clean and concise by using array keys. You can see how my first go at it is kind of sloppy looking with the keys being $classes[$i+3]
etc. It works fine, but I wanted to make it more readable and make more sense at a glance.
This is the error message:
Notice: Undefined index: A in C:\xampp\htdocs\CreateaTranscript.php on line 127 ("A" is a key in the "$grade_conversion" array that relates to a decimal value for computing the user's GPA.)
This is the line of code I use to have that actually worked:
$grade_points += $grade_conversion[$classes[$i+3]] * $classes[$i+2];
This is the line of code causing the error:
$grade_points += $grade_conversion[$course[$i]['grade']] * $course[$i]['credit_hours'];
I tried enclosing the multi-dimensional array in 's. However, it just creates a different error that prevents the script from running. I know the basic issue is that the compiler sees an array key not enclosed in 's. However, why does it work with the normal array and not the multi-dimensional array?
$grade_conversion=array(
'A' => 4,
'A-' => 3.7,
'B+' => 3.3,
'B' => 3.0,
'B-' => 2.7,
'C+' => 2.3,
'C' => 2.0,
'C-' => 1.7,
'D+' => 1.3,
'D' => 1,
'D+' => 0.7,
'F' => 0,
);
I was trying to keep this concise. Is it possible to use an index in the manner I declared it? It worked fine before I switched it over to a multi-dimensional array. I'm going to add a sample of the for statement that the line was from:
for($i = 0;$i < count($course);$i++){
print "<tr bgcolor = 'F8F8F8'><td>" . $course[$i]['course_id'] . "<td>" . $course[$i]['course_name'] . "</td><td>" . $course[$i]['credit_hours'] . "</td><td>" . $course[$i]['grade'] . "</td></tr>";
//Keep a running count of credit hours for the GPA equation.
//Keep a running total of grade points using the $grade_conversion array
$total_credit_hours += $course[$i]['credit_hours'];
$grade_points += $grade_conversion[$course[$i]['grade']] * $course[$i]['credit_hours'];
}
The $course array is create by reading data from a file.
Here is how the $course array is created. The data is pulled from a data file line by line.
$fh=fopen($file, "r");
$j = 0;
while (!feof($fh)){
$course[$j]['course_id'] = fgets($fh);
$course[$j]['course_name'] = fgets($fh);
$course[$j]['credit_hours'] = fgets($fh);
$course[$j]['grade'] = fgets($fh);
$j++;
}
fclose($fh);
As mentioned in my comment above, your values will include some whitespace characters thanks to fgets
. You'll need to trim()
them when you store them in the the $course
array or when using them as an index for $grade_conversions
.
You can also avoid manually specifying array indexes when populating your array, eg
while (!feof($fh)){
$course[] = [
'course_id' => trim(fgets($fh)),
'course_name' => trim(fgets($fh)),
'credit_hours' => trim(fgets($fh)),
'grade' => trim(fgets($fh))
];
}
and iterating it
foreach ($course as $courseItem) {
$total_credit_hours += $courseItem['credit_hours'];
// etc
}
And finally, I think just about any structured text format (JSON, XML, CSV) would be better than un-keyed, newline separated values, eg
// file.json
[{
"id": 1,
"name": "Foo",
"credit_hours": 2,
"grade": "A"
},
{
"id": 2,
"name": "Bar",
"credit_hours": 14,
"grade": "B"
}]
and load it...
$courses = json_decode(file_get_contents('file.json'), true);