Jump to content

Calculation help in php


u0867587

Recommended Posts

Hi,

 

I have a calculation issue in my php. Below is my code:

 

 

 

$

moduletotal = 0;
$modulecount = 0;
$sessionweight = 0;
        
while ($row = mysql_fetch_array($result)){    

        $moduletotal += $row['Mark'];      
  
        $modulemark = (int)($moduletotal);  // Here and above is the calculation which is simply adding the total number of marks


// Below is the code that displays my results. The variable of the calculation is shown at the end of the last if statement (3rd one down)

$output = "";
$studentId  = false;
$courseId  = false;
$moduleId  = false;
          
if($studentId != $row['StudentUsername'])  {        

	$studentId = $row['StudentUsername'];        
	$output .= "<p><strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})";   

   }    
   
  if($courseId != $row['CourseId']) {    

	$courseId = $row['CourseId'];        		
	$output .= "<br><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <br><strong>Year:</strong> {$row['Year']}</p>";    

	}    

if($moduleId != $row['ModuleId'])  {        

   $moduleId = $row['ModuleId'];        
   $output .= "<p><br><strong>Module:</strong> {$row['ModuleId']} - {$row['ModuleName']} {$row['Credits']} $modulemark</p>";   

  }  
    
   $output .= "<p><strong>Session:</strong> {$row['SessionId']} {$row['Mark']} {$row['Grade']} {$row['SessionWeight']}%</p>";
			   
  }

  
echo $output;

      ?>

The Results look like this at the moment (The answer the calculation is next to each Module):

 

Student: Mayur Patel (u0867587)

Course: INFO101 - Bsc Information Communication Technology 

Year: 3

 

 

Module: CHI2550 - Modern Database Applications 20 72 (72 is the answer to the calculation for the first module but this is incorrect at it should also add the 67 and thus become 139)

 

Session: AAB 72 A 20%

 

Session: AAE 67 B 40%

 

 

Module: CHI2513 - Systems Strategy 20 200 (200 is the answer to the calculation for this module but this is incorrect it should be only 61. But what it has done is that it has added the 72 and 67 from the first module and added it with the 61 in this module)

 

Session: AAD 61 B 50%

 

 

 

SO WHAT MY QUESTION IS HOW CAN I GET THE CALCULATION SO THAT IT ONLY ADDS UP EACH SESSION MARKS THAT BELONGS TO A MODULE ONLY? IN THE EXAMPLE ABOVE IT SHOULD ONLY ADD THE MARKS FOR SESSION AAB AND AAE IN THE MODERN DATABASE APPLICATIONS MODULE AND THEN WHEN IT GETS TO THE NEXT MODULE WHICH IS SYSTEMS STRATEGY, THE MARKS FOR THAT MODULE SHOULD START AGAIN AND ADD ON THE MARKS FOR THE SESSION AAD IN THAT MODULE. HOPEFULLY YOU UNDERSTAND AND THANK YOU

 

Below is the query if you want to look at it:

 

 $query = "
          SELECT c.CourseName,  
st.CourseId, st.Year, st.StudentUsername, st.StudentForename, st.StudentSurname,
s.ModuleId, s.SessionId, s.SessionWeight, 
gr.Mark, gr.Grade
m.ModuleName, m.Credits,
          FROM Course c
          INNER JOIN Student st ON c.CourseId = st.CourseId
          JOIN Grade_Report gr ON st.StudentId = gr.StudentId
          JOIN Session s ON gr.SessionId = s.SessionId
          JOIN Module m ON s.ModuleId = m.ModuleId
          WHERE
          (st.StudentUsername = '".mysql_real_escape_string($studentid)."')
          ORDER BY c.CourseName, st.StudentUsername, m.ModuleName, s.SessionId
          ";

Link to comment
Share on other sites

That didn't work Joel, if I do that it does not display any results. Thats why I prefer doing my calculations using php rather than SQL functions. By the way how do I delete this post because I have a similar post which explains the question a bit more better than this one?

Link to comment
Share on other sites

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.