-3

Hi i am working on a docker microservices grade checker application. I am getting user input from an HttpRequest. I am lacking some basic knowledge on arrays and how to work with them in php, its a weak point for me. I currently have the below code.

<?php
function getIndividualClassification($input_text)
{
    $grades = null;
    $lines = explode('newline', $input_text);
    foreach ($lines as $line){
        if ($line >= 70 && $line <= 100){
            $grades = 'Distinction';
        }elseif ($line >= 60 && $line <= 69){
            $grades =  'Commendation';
        }elseif ( $line >= 50 && $line <= 59){
            $grades = 'Pass';
        }elseif ($line >= 40 && $line <= 49){
            $grades =  'Marginal Fail';
        }elseif ($line >= 30 && $line <= 39){
            $grades = 'Fail';
        }elseif ($line >= 0 && $line <= 29){
            $grades = 'Low Fail';
        }else{
            $grades = 'Error - Check input';

        }

        return $grades;
    }

what i am trying to do is take the input which will be any number of modules and marks in the form (module1,60) and classify each one into the above grade bands.

Input is like:

module1,60newlinemodule2,80 

so i will have an output like

module1 = Pass
module2 = fail 

Any help or pointers is very much apricated :)

Barmar
  • 741,623
  • 53
  • 500
  • 612
Leonie
  • 9
  • 4
  • You're missing a `}` – Barmar Aug 16 '22 at 23:03
  • Returning in a `foreach` loop will exit the loop after the first iteration. Did you mean to build up an array of grade bands? – Phil Aug 16 '22 at 23:04
  • 3
    Please provide a [mcve]. – mickmackusa Aug 16 '22 at 23:11
  • Only one iteration i think i need. my problem is my issues with arrays, the input should be put into an array and then the output reflect this. like: module 1 = Distinction module 2 = pass – Leonie Aug 16 '22 at 23:11
  • 1
    Please [edit] your question. Show us realistic sample data for `$input_text`, then show us your exact desired output for that sample data. Make your sample text large enough to expose fringe cases -- this helps us to differentiate good/correct answers from bad/incorrect answers. For this particular question, it will be important for us to know your PHP version (whether `match()` is an option). – mickmackusa Aug 16 '22 at 23:13
  • If you only need one iteration, why do you have a loop in the first place? – Barmar Aug 16 '22 at 23:27
  • Does the input text really have the word `newline` as the delimiter, rather than a `\n` character? – Barmar Aug 16 '22 at 23:28
  • I asked that same question in your [previous question](https://stackoverflow.com/questions/73366843/getting-error-when-trying-to-run-the-code-for-grade-classification) – Barmar Aug 16 '22 at 23:29
  • I was thinking i need the array to loop through the classifications each time. would there be an easier way to do it? – Leonie Aug 16 '22 at 23:29
  • 1
    If you need results for multiple modules, you do need a loop, and you should put the results in an associative array where the keys are the module names and the values are the classifications. – Barmar Aug 16 '22 at 23:31
  • Yep newline. It was the way it was written so input is like module1,60newlinemodule2,80 – Leonie Aug 16 '22 at 23:31
  • Do you want _"output"_ or a return value? It's not clear from your question what this function should actually do once it's figured out a grade band for each _module_ – Phil Aug 16 '22 at 23:35
  • Related [Checking the Range of a Number in PHP](https://stackoverflow.com/q/21191293/2943403). [This answer](https://stackoverflow.com/a/69317832/2943403) demonstrates how to store multiple values in a loop. [This answer](https://stackoverflow.com/a/24830691/2943403) also demonstrates how to populate an array. And [this answer](https://stackoverflow.com/q/59746557/2943403) – mickmackusa Aug 16 '22 at 23:46

1 Answers1

1

Collect the results in an associative array, and return that after the loop is done.

function getIndividualClassification($input_text)
{
    $grades = [];
    $lines = explode('newline', $input_text);
    foreach ($lines as $line){
        [$module, $grade] = explode(',', $line);
        if ($grade > 100 || $grade < 0) {
            $grades[$module] = 'Error - Check input';
        } elseif ($grade >= 70) {
            $grades[$module] = 'Distinction';
        } elseif ($grade >= 60) {
            $grades[$module] = 'Commendation';
        } elseif ( $grade >= 50) {
            $grades[$module] = 'Pass';
        } elseif ($grade >= 40) {
            $grades[$module] = 'Marginal Fail';
        } elseif ($grade >= 30) {
            $grades[$module] = 'Fail';
        } else {
            $grades[$module] = 'Low Fail';
        }
    }
    return $grades;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I'd make the delimiter an optional argument. Dollars to donuts OP is just misinterpreting the _newline_ part of their specification – Phil Aug 16 '22 at 23:38
  • @Phil I'll wait for him to say that this doesn't work. – Barmar Aug 16 '22 at 23:39
  • @Barmar thanks for your help. I am just getting [object object] returned. It may have to do with the way im calling my json data in the HttpRequest. – Leonie Aug 16 '22 at 23:59
  • See https://stackoverflow.com/questions/4750225/what-does-object-object-mean – Barmar Aug 17 '22 at 00:01
  • @Leonie please narrow the focus of your question/problem. If you would have served up a [mcve] from the beginning, your question would have been better received and answers would be perfectly tailored to your data. – mickmackusa Aug 17 '22 at 00:23
  • @Leonie That problem is with how you're processing the result on your front-end, not a PHP issue. – Barmar Aug 17 '22 at 00:25