SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Got explanation what I need to do but don't know how to code it

    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 :
    Code:
    `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:

    Code:
        <?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

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    What should the output look like?

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    What should the output look like?
    The output should look like this below:


    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%

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,398
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    Try loading the data in a multidimensional array first, and the loop through the array to display the data:
    PHP Code:
            // load query data in a multidimensional array
            
    $dataArray = array();
            while (
    $row mysql_fetch_array($result)) {
                
    $dataArray[$row['CourseId']]['CourseName'] = $row['CourseName'];
                
    $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName'];
                
    $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['Mark'] = $row['Mark'];
                
    $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['Sessions'][$row['SessionId']]['SessionWeight'] = $row['SessionWeight'];
            }

            
    // just for debugging purposes, let's do a print_r of the array
            // eliminate this line when you don't need it anymore
            
    print_r($dataArray);

            
    // loop through the array and create the output
            
    $output ""
            foreach (
    $dataArray as $courseId => $courseData) { 
               
    // elaborate course data
               
    output .= '<p><br><strong>Course:</strong> ' $courseId ' - ' $courseData['courseName'] . ' <strong>Course Mark:</strong></p><br>\n';

               foreach (
    $courseData['Modules'] as $moduleId => $moduleData) {
                 
    // elaborate module data
                 
    $output .= '<p><strong>Module:</strong> ' $moduleId ' - ' $moduleData['moduleName'];

                 
    $markTotal 0
                 
    $markGrade 0
                 
    $weightSession 0;
                 
    $grade ""
                 
    $sessionsHTML ""
                 foreach (
    $moduleData['Sessions'] as $sessionId => $sessionData) {
                    
    // elaborate session data
                    
    $markTotal += round($sessionData['Mark'] / 100 $sessionData['SessionWeight']); 
                    
    $weightSession  += ($sessionData['SessionWeight']); 
                    
    $sessionsHTML .= '<p><strong>Session:</strong> ' $sessionId ' <strong>Session Mark:</strong> ' $sessionData['Mark'] . '</strong> <strong>Session Weight Contribution</strong> ' $sessionData['SessionWeight'] . '%</p>\n'
                 }
                 
    $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 >= && $markGrade <= 29) { $grade "F"; } 

                 
    $output .= ' <strong>Module Mark:</strong> ' $markTotal ' <strong>Mark Percentage:</strong> ' $markGrade ' <strong>Grade:</strong> ' $grade ' </p>\n';  
                 
    $output .= $sessionsHTML;
               }  
    // <-- end of sessions foreach
             
    }  // <-- end of modules foreach
           
    }  // <-- end of courses foreach

           //Display the output 
           
    echo $output

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2011
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much, that has worked very well as it does show the course details only once. The print_r does show clearly how the query works and I now know how what that person meant by initializing an multi dimensional array in a loop. Thank you very much and thank you for devoting your time to help me


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •