Jump to content

Why is it not recognising this variable and it is not outputting all the data


Recommended Posts

Below is my code:

 

 

 

  function outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionData) 
    { 
    
        if(!count($sessionData)) 
        { 
            return false; 
        } 
    
        $markTotal = 0; 
        $markGrade = 0; 
        $weightSession = 0;
        $grade = ""; 
        $sessionsHTML = ""; 
    
        foreach($sessionData as $session) 
        { 
    
            $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} <strong>Session Mark:</strong> {$session['Mark']}</strong> <strong>Session Weight Contribution</strong> {$session['SessionWeight']}%</p>\n"; 
            $markTotal += round($session['Mark'] / 100 * $session['SessionWeight']); 
            $weightSession  += ($session['SessionWeight']); 
            $markGrade = round($markTotal /  $weightSession * 100); 
                 
            if ($markGrade >= 70) 
            { 
                $grade = "A"; 
            } 
    
            else if ($markGrade >= 60 && $markGrade <= 69) 
            { 
                $grade = "B"; 
            } 
    
            else if ($markGrade >= 50 && $markGrade <= 59) 
            { 
                $grade = "C"; 
            } 
    
            else if ($markGrade >= 40 && $markGrade <= 49) 
            { 
                $grade = "D"; 
            } 
    
            else if ($markGrade >= 30 && $markGrade <= 39) 
            { 
                $grade = "E"; 
            } 
    
            else if ($markGrade >= 0 && $markGrade <= 29) 
            { 
                $grade = "F"; 
            } 
    } 
    
    	$courseHTML = "<p><br><strong>Course:</strong> {$courseId} - {$courseName}</p><br>\n";
        $moduleHTML = "<p><strong>Module:</strong> {$moduleId} - {$moduleName} <strong>Module Mark:</strong> {$markTotal} <strong>Mark Percentage:</strong> {$markGrade} <strong>Grade:</strong> {$grade} </p>\n"; 
    
        return $courseHTML . $moduleHTML . $sessionsHTML; 
    } 
    
    $output = ""; 
    
    $studentId = false; 
    $courseId  = false; 
    $moduleId  = false; 
    
    while ($row = mysql_fetch_array($result)) 
    { 
    	
    	  if($courseId != $row['CourseId']) 
        { 
    
            //Course has changed 
            $courseName  = $row['CourseName'];
            $courseId = $row['CourseId']; 
    
            $output .= outputModule($courseId, $courseName);  
        }
    
        if($moduleId != $row['ModuleId']) 
        { 
    
            //Module has changed 
            if(isset($sessionsAry)) //Don't run function for first record 
            { 
    
                //Get output for last module and sessions 
                $output .= outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
            } 
    
            //Reset sessions data array and Set values for new module 
    
            $sessionsAry = array(); 
            $moduleId    = $row['ModuleId']; 
            $moduleName  = $row['ModuleName']; 
        } 
             
        //Add session data to array for current module 
             
        $sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark'], 'SessionWeight'=>$row['SessionWeight']); 
             
    }    //Get output for last module 
    
    $output .= outputModule($moduleId, $moduleName, $sessionsAry); 
    
    //Display the output 
    echo $output; 

Below is the output:

 

 

    Notice: Undefined variable: sessionData in /web/stud/u0867587/Mobile_app/student_overall_grade.php on line 69

   

    Notice: Undefined variable: sessionData in /web/stud/u0867587/Mobile_app/student_overall_grade.php on line 69

   

    Course: INFO101 - Bsc Information Communication Technology

   

   

    Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B

   

    Session: AAB Session Mark: 72 Session Weight Contribution 20%

   

    Session: AAE Session Mark: 67 Session Weight Contribution 40%

 

There is suppose to be another module with its own sessions below this but it does not show it. Also it is giving me notices that $SessionData is undefined. Why can it not find the variable $SessionData?

 

Below is what it should of outputted:

 

Course: INFO101 - Bsc Information Communication Technology Course Mark Grade

Year: 3

 

 

Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B

 

Session: AAB Session Mark: 72 Session Weight Contribution 20%

 

Session: AAE Session Mark: 67 Session Weight Contribution 40%

 

 

Module: CHI2513 - Systems Strategy Module Mark: 31 Mark Percentage: 62 Grade: B

 

Session: AAD Session Mark: 61 Session Weight Contribution 50%

Two lines from your code:

 

  function outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionData) 
$output .= outputModule($moduleId, $moduleName, $sessionsAry); 

What's wrong there?

I understand that, can't belive I missed that. For the whole output it needs all veraibles from the function. But the problem strangly enough is that it gets rid of one notice, I still get one notice which is the same notice. What do I need to look at to get rid of the last notice problem.

It is the same line. It came with two notices with the same notice on the same line. I did what you told me to do and it got rid of one notice but not the other. Below is the latest output on the browser:

 

 

Notice: Undefined variable: sessionData in /web/stud/u0867587/Mobile_app/student_overall_grade.php on line 69

 

Course: INFO101 - Bsc Information Communication Technology

 

 

Module: CHI2550 - Modern Database Applications Module Mark: 41 Mark Percentage: 68 Grade: B

 

Session: AAB Session Mark: 72 Session Weight Contribution 20%

 

Session: AAE Session Mark: 67 Session Weight Contribution 40%

 

 

Course: INFO101 - Bsc Information Communication Technology

 

 

Module: CHI2513 - Systems Strategy Module Mark: 31 Mark Percentage: 62 Grade: B

 

Session: AAD Session Mark: 61 Session Weight Contribution 50%

 

Below is the current code: (It is same as code in first post except I added $courseId and $courseName in the "output .=" near the bottom)

 

 function outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionData) 
    { 
    
        if(!count($sessionData)) 
        { 
            return false; 
        } 
    
        $markTotal = 0; 
        $markGrade = 0; 
        $weightSession = 0;
        $grade = ""; 
        $sessionsHTML = ""; 
    
        foreach($sessionData as $session) 
        { 
    
            $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} <strong>Session Mark:</strong> {$session['Mark']}</strong> <strong>Session Weight Contribution</strong> {$session['SessionWeight']}%</p>\n"; 
            $markTotal += round($session['Mark'] / 100 * $session['SessionWeight']); 
            $weightSession  += ($session['SessionWeight']); 
            $markGrade = round($markTotal /  $weightSession * 100); 
                 
            if ($markGrade >= 70) 
            { 
                $grade = "A"; 
            } 
    
            else if ($markGrade >= 60 && $markGrade <= 69) 
            { 
                $grade = "B"; 
            } 
    
            else if ($markGrade >= 50 && $markGrade <= 59) 
            { 
                $grade = "C"; 
            } 
    
            else if ($markGrade >= 40 && $markGrade <= 49) 
            { 
                $grade = "D"; 
            } 
    
            else if ($markGrade >= 30 && $markGrade <= 39) 
            { 
                $grade = "E"; 
            } 
    
            else if ($markGrade >= 0 && $markGrade <= 29) 
            { 
                $grade = "F"; 
            } 
    } 
    
    	$courseHTML = "<p><br><strong>Course:</strong> {$courseId} - {$courseName}</p><br>\n";
        $moduleHTML = "<p><strong>Module:</strong> {$moduleId} - {$moduleName} <strong>Module Mark:</strong> {$markTotal} <strong>Mark Percentage:</strong> {$markGrade} <strong>Grade:</strong> {$grade} </p>\n"; 
    
        return $courseHTML . $moduleHTML . $sessionsHTML; 
    } 
    
    $output = ""; 
    
    $studentId = false; 
    $courseId  = false; 
    $moduleId  = false; 
    
    while ($row = mysql_fetch_array($result)) 
    { 
    	
    	  if($courseId != $row['CourseId']) 
        { 
    
            //Course has changed 
            $courseName  = $row['CourseName'];
            $courseId = $row['CourseId']; 
    
            $output .= outputModule($courseId, $courseName);  
        }
    
        if($moduleId != $row['ModuleId']) 
        { 
    
            //Module has changed 
            if(isset($sessionsAry)) //Don't run function for first record 
            { 
    
                //Get output for last module and sessions 
                $output .= outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
            } 
    
            //Reset sessions data array and Set values for new module 
    
            $sessionsAry = array(); 
            $moduleId    = $row['ModuleId']; 
            $moduleName  = $row['ModuleName']; 
        } 
             
        //Add session data to array for current module 
             
        $sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark'], 'SessionWeight'=>$row['SessionWeight']); 
             
    }    //Get output for last module 
    
    $output .= outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionsAry); 
    
    //Display the output 
    echo $output;

 

 

So for every output .= I have to include all variables? Because in that section I only want to it to output $CourseId and $CourseName? Plus it will come with notice that $moduleName and $moduleId is undefined if I include those variables in that particular ouptut as well as the $sessionData notice.

Hi, Sorry I managed to sort it out, I realise I didn't need the If(CourseId != $row['CourseId']) so it means I didn't need the code and output in between those brackets. Thank you for helping me Maniac Dan :)

So for every output .= I have to include all variables?

 

Uh, yeah, since that's how you defined your function.

 

Quick and dirty function lesson:

 

Functions have signatures.  These signatures are a function's name and the arguments listed in its argument list.  If you define a function with a certain signature (such as your function which requires four arguments), it will only work if you actually pass in four arguments.

 

It is possible to have a function that can take an arbitrary number of arguments.  That said, since you're struggling with the PHP 101 basics, it's best if you try to gain a solid understanding of the fundamentals first.

 

Start with the PHP online manual and move on from there.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.