代码之家  ›  专栏  ›  技术社区  ›  Leo Sutton

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

  •  -3
  • Leo Sutton  · 技术社区  · 9 年前

    我正在尝试使用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 回复  |  直到 9 年前
        1
  •  2
  •   axiac    9 年前

    问题在于:

    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>';