为什么我的PHP回声输出不会如预期的那样?

时间:2017-07-26 10:24:48

标签: php html echo

我正在尝试使用PHP echo在此页面http://www.trackmymarks.com/teacher_analysis.php?year=7&class=7L/Sc1上构建表格。不幸的是,echo命令没有按预期工作,我不明白为什么。它似乎无法正确连接字符串。

在第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";
?>

1 个答案:

答案 0 :(得分: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>';
相关问题