2017-07-26 72 views
-3

我正在嘗試使用PHP回顯在此頁上構建表格http://www.trackmymarks.com/teacher_analysis.php?year=7&class=7L/Sc1。不幸的是,回聲命令沒有按預期工作,我不知道爲什麼。它似乎無法正確連接字符串。爲什麼我的PHP回聲輸出不如預期?

在第120行,代碼無法生成所需的標籤來格式化表格,因爲我需要它。下面是該頁面的PHP:

<?php 
require "requires.php"; 
require "header.php"; 

echo "<title>TrackMyMarks - Teacher Analysis</title>"; 

$year = htmlspecialchars($_GET["year"]); 
$class = htmlspecialchars($_GET["class"]); 

echo "<h1>Teacher Analysis ".$class."</h1>"; 

if ((int)date("M") < 9) { $academicyear = date("Y") - 1; } else { $academicyear = date("Y"); } 


$query = "SELECT * FROM Student INNER JOIN `Year ".$year."` ON Student.UPN = `Year ".$year."`.UPN AND Student.CurrentClass='".$class."' AND `Year ".$year."`.`Year Taken` = '" . $academicyear . "'"; 

echo $query; 

$result = mysqli_query($GLOBALS["con"], $query); 
if (!$result) 
{ 
    logwrite("MYSQL ERROR: " . mysqli_error($GLOBALS["con"])); 
} 

$retention = array(array(), array(), array(), array(), array(), array()); 
$PPIretention = array(array(), array(), array(), array(), array(), array()); 
$nPPIretention = array(array(), array(), array(), array(), array(), array()); 
$girlsretention = array(array(), array(), array(), array(), array(), array()); 
$boysretention = array(array(), array(), array(), array(), array(), array()); 

while($row = mysqli_fetch_array($result)){ 
// print_r($row); 

    if ($year == 7) 
    { 
     $TGLookup = "Year7_TG"; 
    } else { 
     $TGLookup = "Year8_TG"; 
    } 

    print_r($row); 

    $targetCredits = lookup($row['Target Level'], $TGLookup); 

    $studentRetention = array($row['Term 1 Mastery Test'] - $targetCredits, $row['Term 2 Mastery Test'] - $targetCredits, $row['Term 3 Mastery Test'] - $targetCredits, $row['Term 4 Mastery Test'] - $targetCredits, $row['Term 5 Mastery Test'] - $targetCredits, $row['Term 6 Mastery Test'] - $targetCredits); 

    array_push($retention[0], $studentRetention[0]); 
    array_push($retention[1], $studentRetention[1]); 
    array_push($retention[2], $studentRetention[2]); 
    array_push($retention[3], $studentRetention[3]); 
    array_push($retention[4], $studentRetention[4]); 
    array_push($retention[5], $studentRetention[5]); 

    if ($row['PPI'] == 1) { 
     array_push($PPIretention[0], $studentRetention[0]); 
     array_push($PPIretention[1], $studentRetention[1]); 
     array_push($PPIretention[2], $studentRetention[2]); 
     array_push($PPIretention[3], $studentRetention[3]); 
     array_push($PPIretention[4], $studentRetention[4]); 
     array_push($PPIretention[5], $studentRetention[5]); 
    } else { 
     array_push($nPPIretention[0], $studentRetention[0]); 
     array_push($nPPIretention[1], $studentRetention[1]); 
     array_push($nPPIretention[2], $studentRetention[2]); 
     array_push($nPPIretention[3], $studentRetention[3]); 
     array_push($nPPIretention[4], $studentRetention[4]); 
     array_push($nPPIretention[5], $studentRetention[5]); 
    } 

    if ($row['Gender'] == 'F') { 
     array_push($girlsretention[0], $studentRetention[0]); 
     array_push($girlsretention[1], $studentRetention[1]); 
     array_push($girlsretention[2], $studentRetention[2]); 
     array_push($girlsretention[3], $studentRetention[3]); 
     array_push($girlsretention[4], $studentRetention[4]); 
     array_push($girlsretention[5], $studentRetention[5]); 
    } else { 
     array_push($boysretention[0], $studentRetention[0]); 
     array_push($boysretention[1], $studentRetention[1]); 
     array_push($boysretention[2], $studentRetention[2]); 
     array_push($boysretention[3], $studentRetention[3]); 
     array_push($boysretention[4], $studentRetention[4]); 
     array_push($boysretention[5], $studentRetention[5]); 
    } 


} 

echo ' 

<div class="row"> 
<table class="table table-boardered"> 
    <thead> 
     <tr> 
      <th>Term</th> 
      <th>Average Retention</th> 
      <th>HW Completion percentage</th> 
      <th>PPI Gap</th> 
      <th>Gender Gap</th> 
     </tr> 
    </thead> 
    <tbody> 
     <tr> 
      <td>Overall</td> 
      <td></td> 
      <td></td> 
      <td></td> 
      <td></td> 
     </tr>'; 

for ($i = 0; $i < 6; $i++){ 
    if (count($retention[$i]) == 0) {$retentionAverage = 0;} else {$retentionAverage = array_sum($retention[$i])/count($retention[$i]);} 
    if (count($PPIretention[$i]) == 0) {$PPIretentionAverage = 0;} else {$PPIretentionAverage = array_sum($PPIretention[$i])/count($PPIretention[$i]);} 
    if (count($nPPIretention[$i]) == 0) {$nPPIretentionAverage = 0;} else {$nPPIretentionAverage = array_sum($nPPIretention[$i])/count($nPPIretention[$i]);} 
    if (count($boysretention[$i]) == 0) {$boysretentionAverage = 0;} else {$boysretentionAverage = array_sum($boysretention[$i])/count($boysretention[$i]);} 
    if (count($girlsretention[$i]) == 0) {$girlsretentionAverage = 0;} else {$girlsretentionAverage = array_sum($girlsretention[$i])/count($girlsretention[$i]);} 

    echo $retentionAverage; 

    echo '<tr><td>'. ($i + 1) .'</td><td>'. $retentionAverage .'</td><td>'. $PPIretentionAverage-$nPPIretentionAverage .'</td><td>'.' '.'</td><td>'. $girlsretentionAverage-$boysretentionAverage .'</td></tr>'; 
} 
echo ' 
    </tbody> 
</table> 

'; 

include "footer.php"; 
?> 
+2

和線是120? –

+0

嘗試提供[mcve]。 – axiac

+0

Like 120 is echo'​​'。 ($ i + 1)'。'​​'。 $ retentionAverage。「​​'。 $ PPIretentionAverage- $ nPPIretentionAverage。'​​'。' ''​​'。 $ girlsretentionAverage- $ boysretentionAverage。''; –

回答

2

的問題是在該行:

echo '<tr><td>'. ($i + 1) .'</td><td>'. $retentionAverage .'</td><td>'. $PPIretentionAverage-$nPPIretentionAverage .'</td><td>'.' '.'</td><td>'. $girlsretentionAverage-$boysretentionAverage .'</td></tr>'; 

算術+-operators具有相同的precedence作爲字符串連接運算符(.)和所有的他們從左到右是聯想的。

這意味着括號($i + 1)中的子表達式首先被計算,然後被用於計算字符串的其餘部分。一切順利的話,直到它到達:

.'</td><td>'. $PPIretentionAverage-$nPPIretentionAverage 

因爲相同的優先級和左到右關聯的,已經計算字符串的值與</td><td>,然後用$PPIretentionAverage(後轉換爲字符串)。結果字符串然後用作算術減法-$nPPIretentionAverage的左側運算符。

. $girlsretentionAverage-$boysretentionAverage上再次發生同樣的事情。

要修復它周圍使用算術運算符括號(或提取運算子表達式成的echo()之前被計算的變量):

echo '<tr><td>'. ($i + 1) 
    .'</td><td>'. $retentionAverage 
    .'</td><td>'. ($PPIretentionAverage - $nPPIretentionAverage) 
    .'</td><td>'.' ' 
    .'</td><td>'. ($girlsretentionAverage - $boysretentionAverage) 
    .'</td></tr>'; 
相關問題