2011-10-31 35 views
0

我想要的是echo "<p>Average Mark: $average</p>";出現在表格上方而不是在表格下方。如果我這樣做,雖然問題是它不會計算平均值,因爲回聲高於變量。但是,如果我將變量和整個表格上方的while循環移動(我需要while循環來從查詢中選擇$row[Mark]'字段,因爲這是要進行平均的字段),它會混淆表。我如何在表格上方顯示$average, $total, $count等變量和echo "<p>Average Mark: $average</p>";而不會搞亂表格結構?重新顯示一個回顯和變量會搞亂我的表?

我試圖$("#record").html(<?php echo "Average Mark: $average"; ?>);但我得到這個錯誤在這條線: 解析錯誤:語法錯誤,意外「(」,希望T_VARIABLE或/web/stud/u0867587/Mobile_app/exam_grade_report.php「$」上線135

下面是當前的代碼,你可以看到在while循環的$count++; , $total += $row['Mark'];和$平均變動和回聲底部

<?php 

     $result = mysql_query($query); 
     mysql_close(); 

     $("#record").html(<?php echo "Average Mark: $average"; ?>); 
    ?> 
<div id="record"> 
    <table border='1'> 
      <tr> 
      <th>Session ID</th> 
      <th>TeacherUsername</th> 
      <th>Teacher Name</th> 
      <th>Module Number</th> 
      <th>Module Name</th> 
      <th>Course ID</th> 
      <th>Course Name</th> 
      <th>Year</th> 
      <th>Student Username</th> 
      <th>Student Name</th> 
      <th>Mark</th> 
      <th>Grade</th> 
      </tr> 
      <?php 
      $total = 0; 
      $count = 0; 
      while ($row = mysql_fetch_array($result)) { 
      $count++; 
       $total += $row['Mark']; 
       echo " 
      <tr> 
      <td>{$row['SessionId']}</td> 
      <td>{$row['TeacherUsername']}</td> 
      <td>{$row['TeacherForename']} {$row['TeacherSurname']}</td> 
      <td>{$row['ModuleId']}</td> 
      <td>{$row['ModuleName']}</td> 
      <td>{$row['CourseId']}</td> 
      <td>{$row['CourseName']}</td> 
      <td>{$row['Year']}</td> 
      <td>{$row['StudentUsername']}</td> 
      <td>{$row['StudentForename']} {$row['StudentSurname']}</td> 
      <td>{$row['Mark']}</td> 
      <td>{$row['Grade']}</td> 
      </tr>"; 
      } 
      ?> 

      </table> 
     </div> 
     <?php 
     $average = (int)($total/$count); 
     echo "<p>Average Mark: $average</p>"; 
     ?> 

回答

0

好像你混合javascriptphp

<?php 

    $result = mysql_query($query); 
    mysql_close(); 

    $("#record").html(<?php echo "Average Mark: $average"; ?>); // what is this?? 
?> 
+0

有人告訴我試試這個,不知道是什麼這意味着 – BruceyBandit

0

如果你只是把你想要的<table>(或任何<tr>)裏面的東西,但任何<td>細胞之外,它會出現在表格上方。

+0

我不認爲W3C驗證器會喜歡這個解決方案。 – samura

+0

是的。好的,我會添加一個更好的答案:p –

0

您可以做的是在表格底部定義表格標題。例如:

<table> 
    <tbody> 
     <tr> 
      <td>row1</td> 
     </tr> 
     <tr> 
      <td>row2</td> 
     </tr> 
    </tbody> 
    <thead> 
     <tr> 
      <th>Average: 20</th> 
     </tr> 
    </thead> 
</table> 
0

添加另一個答案,因爲我對我的老朋友有了一個更好的主意。

輸出表之前,就把:

<?php ob_start(create_function('$a','global $average; return "Average Mark: ".$average.$a;)); ?> 

並且表完成,您已經定義了$average變量之後:

<?php ob_end_flush(); ?>