Jump to content

Recommended Posts

My case statement is not working. What happened is that two students got a $markgrade of 61 and 67. So they both should get grade B but instead they both get Grade A. 1 student got 55 which should be grade C but gets grade A. why is it not following the switch statement?

 

    function outputModule($moduleID, $moduleName, $sessionData)    
      
      {        
    	  
    	  if(!count($sessionData)) { return false; }  
    
    
    $markTotal = 0;        
        	  $markGrade = 0;
        	  $weightSession = 0;
        	  $grade = "";
        	  $sessionsHTML = '';  
        	  
        	  switch($grade){
        	case ($markGrade >=70):
        	$grade = 'A';
        	break;
        	case ($markGrade >=60 && $markGrade <=69):
        	$grade = 'B';
        	break;
        	case ($markGrade >=50 && $markGrade <=59):
        	$grade = 'C';
        	break;
        	case ($markGrade >=40 && $markGrade <=49):
        	$grade = 'D';
        	break;
        	case ($markGrade >=30 && $markGrade <=39):
        	$grade = 'E';
        	break;
        	case ($markGrade >=0 && $markGrade <=29):
        	$grade = 'F';
        	break;
        };      
        	  
        	  foreach($sessionData as $session)        {           
        		  
        		   $sessionsHTML .= "<p><strong>Session:</strong> {$session['SessionId']} {$session['Mark']} {$session['SessionWeight']}%</p>\n";            
        		   $markTotal += ($session['Mark'] / 100 * $session['SessionWeight']); 
        		   $weightSession  += ($session['SessionWeight']);  
        		   $markGrade = ($markTotal /  $weightSession * 100);   
        		   
        		   }        
        		   
        		   $moduleHTML = "<p><br><strong>Module:</strong> {$moduleID} - {$moduleName} {$markTotal} {$markGrade} {$grade}</p>\n";     
    
     return $moduleHTML . $sessionsHTML;    } 

Link to comment
https://forums.phpfreaks.com/topic/250427-my-case-statement-is-not-working/
Share on other sites

You're switching on $grade, yet trying to make cases for $markGrade.  Further, cases cannot be used as normal conditionals.  You can only use them when you anticipate that your variable will equal a certain value, such as:

 

switch($i)
{
    case 0:
      echo $i;
      break;

   case 1:
      echo "\$i = 1";
      break;
}

 

switch

 

So, just use normal if/elseif/else clauses.

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.