html - Why wont my PHP echo output as expected? -
i trying use php echo build table on page http://www.trackmymarks.com/teacher_analysis.php?year=7&class=7l/sc1. unfortunately, echo command not working expected , have no idea why. seems failing concatenate strings correctly.
on line 120, code fails produce necessary tags format table want it. here php page:
<?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 * student inner join `year ".$year."` on student.upn = `year ".$year."`.upn , student.currentclass='".$class."' , `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>'; ($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"; ?>
the problem in line:
echo '<tr><td>'. ($i + 1) .'</td><td>'. $retentionaverage .'</td><td>'. $ppiretentionaverage-$nppiretentionaverage .'</td><td>'.' '.'</td><td>'. $girlsretentionaverage-$boysretentionaverage .'</td></tr>'; the arithmetic + , - operators have same precedence string concatenation operator (.) , of them associative left right.
this means subexpression in parentheses ($i + 1) evaluated first , used compute rest of string. goes until reaches:
.'</td><td>'. $ppiretentionaverage-$nppiretentionaverage because of equal precedence , left-to-right associativity, computed string concatenated </td><td> , $ppiretentionaverage (after converted string). resulted string used left operator of the arithmetic subtraction -$nppiretentionaverage.
the same thing happens again on . $girlsretentionaverage-$boysretentionaverage.
to fix use parentheses around arithmetic operators (or extract arithmetic subexpressions variables computed before echo()):
echo '<tr><td>'. ($i + 1) .'</td><td>'. $retentionaverage .'</td><td>'. ($ppiretentionaverage - $nppiretentionaverage) .'</td><td>'.' ' .'</td><td>'. ($girlsretentionaverage - $boysretentionaverage) .'</td></tr>';
Comments
Post a Comment