4 Replies - 756 Views - Last Post: 08 November 2011 - 05:04 AM Rate Topic: -----

#1 manleybruce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 07-November 11

Got description on what need to do but don't know how to do it (ph

Posted 07 November 2011 - 06:57 PM

I am doing a project and our group are nearly finsihed. I just need one last thing to sort out and we are done. Below is our current output on the browser:

Course: INFO101 - Bsc Information Communication Technology Course Mark:



Course: INFO101 - Bsc Information Communication Technology Course Mark:


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%

As you can see above it shows the course details twice, this should not happen as it should only show it once but I do not know how to do this. The course details are dislayed by this code in the function :
`echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\n";`


I posted this question on another website and this is what one person said what is happening and what I should do:

"This code does not cause the replication you are seeing. The output you've specified only prints that out once. So its down to usage.

This means you are iterating a block outside of this function and are calling this function twice for the desired module. That indicates that these modules: CHI2550 and CHI2513 both belong to course INFO101. Since these are directly printed and not captured, this indicates that you have called the function within a loop, but are not printing it until after the loop. The replication appears this way since they are echo'd out directly.

Easiest fix is to handle the data returned as an array. If you provide a referenced array to write into, you can easily append to the index and implode them after to create the strings."

He is correct here but the problem is that I do not know how to implement this on the code, can somebody show an example on how this can be done on the code I have written below:

 <?php
          if (isset($_POST['submit'])) {
    
          $query = "
              SELECT st.CourseId, c.CourseName, st.Year, st.StudentUsername, st.StudentForename, st.StudentSurname,
              s.ModuleId, m.ModuleName, m.Credits, s.SessionId, s.SessionWeight, gr.Mark, gr.Grade
              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
              ";
    
            $num = mysql_num_rows($result = mysql_query($query));
            mysql_close();
    
        ?>
    
          <p>
          Your Search:
          </p>
          <p>
          <strong>Student Username:</strong> <?php echo "'$studentid'"; ?><br/>
        </p>
         <?php
         
         if($num ==0){
        echo "<p>Sorry, No Records were found from this Search</p>";}
        else{
        
     
     function outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionData) 
        { 
        
            if(!count($sessionData)) 
            { 
                return false; 
            } 
        
            $markTotal = 0; 
            $markGrade = 0; 
            $weightSession = 0;
            $courseTotal = 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"; 
                } 
        } 
      
        
        
            echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></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 $moduleHTML . $sessionsHTML; 
        } 
        
        $output = ""; 
        
        $studentId = false; 
        $courseId  = false; 
        $moduleId  = false; 
        
        while ($row = mysql_fetch_array($result)) 
        {
    	    
        	  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']; 
                $courseName  = $row['CourseName'];
                $courseId = $row['CourseId']; 
            } 
                 
            //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; 
          }
      }
        ?>


Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: Got description on what need to do but don't know how to do it (ph

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Got description on what need to do but don't know how to do it (ph

Posted 07 November 2011 - 09:15 PM

One nice thing about functions is that you don't need to put them in the middle of your code. Splitting it off makes it easier to read.
<?php
if (isset($_POST['submit'])) {
	$query = "
				 SELECT st.CourseId, c.CourseName, st.Year, st.StudentUsername, st.StudentForename, st.StudentSurname,
				 s.ModuleId, m.ModuleName, m.Credits, s.SessionId, s.SessionWeight, gr.Mark, gr.Grade
				 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
				 ";

	$num = mysql_num_rows($result = mysql_query($query));
	mysql_close();
?>
		 <p>
		 Your Search:
		 </p>
		 <p>
		 <strong>Student Username:</strong> <?php echo "'$studentid'"; ?><br/>
	 </p>
<?php		
	if($num ==0){
		echo "<p>Sorry, No Records were found from this Search</p>";
	} else {
		$output = ""; 
		$studentId = false; 
		$courseId  = false; 
		$moduleId  = false; 
	 
	 	while ($row = mysql_fetch_array($result)) {
			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']; 
				$courseName  = $row['CourseName'];
				$courseId = $row['CourseId']; 
			 } 
						
			 //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; 
	}
}
 
function outputModule($courseId, $courseName, $moduleId, $moduleName, $sessionData) { 
	 if(!count($sessionData)) { 
			 return false; 
	 } 

	 $markTotal = 0; 
	 $markGrade = 0; 
	 $weightSession = 0;
	 $courseTotal = 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"; 
			 } 
		} 

	 echo "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></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 $moduleHTML . $sessionsHTML; 
}
 
?>

Here I see that your function outputs the course information and returns the module information. So what appears to be happening is that INFO101 has two modules but the course headers are being output before the module information.

I would have the function return everything in the order found:
	 $moduleHTML = "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></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 $moduleHTML . $sessionsHTML; 
}


Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,577
  • Joined: 08-June 10

Re: Got description on what need to do but don't know how to do it (ph

Posted 08 November 2011 - 12:36 AM

View Postmanleybruce, on 08 November 2011 - 02:57 AM, said:

"This code does not cause the replication you are seeing. The output you've specified only prints that out once. So its down to usage.

This means you are iterating a block outside of this function and are calling this function twice for the desired module. That indicates that these modules: CHI2550 and CHI2513 both belong to course INFO101. Since these are directly printed and not captured, this indicates that you have called the function within a loop, but are not printing it until after the loop. The replication appears this way since they are echo'd out directly.

Easiest fix is to handle the data returned as an array. If you provide a referenced array to write into, you can easily append to the index and implode them after to create the strings."


I should have guessed that it is from Fou-Lu
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,738
  • Joined: 08-August 08

Re: Got description on what need to do but don't know how to do it (ph

Posted 08 November 2011 - 04:55 AM

That looks right. You've go two different instances of the course so you have two headers. If you really want to remove the second one you could do something like this at the end of the function:
UNTESTED
		static $count = 0;
		static $temp = "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\n";
		$courseHTML = "<p><br><strong>Course:</strong> {$courseId} - {$courseName} <strong>Course Mark:</strong></p><br>\n";
	 
	 	if($courseHTML == $temp && $count) {
	 		$courseHTML = "";
	 		$count = 0;
	 	} else { 
			$temp = $courseHTML;
			$count = 1;
	 	}
	 
	 	$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; 


This post has been edited by CTphpnwb: 08 November 2011 - 04:55 AM

Was This Post Helpful? 0
  • +
  • -

#5 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Got description on what need to do but don't know how to do it (ph

Posted 08 November 2011 - 05:04 AM

Hello,

This:
    //Get output for last module 
   $output .= outputModule($courseId, $courseName, $moduleId,     $moduleName, $sessionsAry); 



Seems a bit odd to me, why are you calling this function outside the while loop? It seems to me that no values have changed so you would be calling it twice with the same values at the end. I could very well be missing something though.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1