程序无法显示正确的数据

时间:2014-04-22 18:09:35

标签: php mysql if-statement while-loop echo

我希望这个程序能够在相应的年份发布得分最高的学生,意思是“2012年的Topper”,它应该只发布那一年中得分最高的学生,以及该学生的得分。其余的年份(如果他们没有在其他年份获得最高分,无关紧要,它应该基于具体年份)。同样在最后一行“整体礼帽:”中,它应该显示最佳学生。

注意:目前它会发布到“2011年的topper”部分,但不是基于最高分。此外,它还没有发布到2011年之后的剩余时间。

pic of the database:

代码:

<body>
    <form>
        <?php
        $username = "amar";
        $password = "amar";
        $hostname = "localhost";
        $database = "study";
        $set = array('2011' => 0, '2012' => 0, '2013' => 0, '2014' => 0, 'final' => 0, 'Final_Grade' => 0);

        $mysqli = new mysqli($hostname, $username, $password, $database)
                or die("Unable to connect to MySQL");

        $query = "select Name, year_1, year_2, year_3, year_4, Final, Final_Grade  from toppers";
        $result = mysqli_query($mysqli, $query);

        if (!$result) {
            $message = 'Invalid query: ' . mysqli_error() . "\n";
            $message .= 'Whole query: ' . $query;
            die($message);
        }

        echo "<table border=1 cellpadding=5>";
        echo "<tr><td>Student Name</td>";
        echo "<td>2011</td>";
        echo "<td>2012</td>";
        echo "<td>2013</td>";
        echo "<td>2014</td>";
        echo "<td>Final Exam</td>";
        echo "<td>Grade</td></tr>";

        while ($row = mysqli_fetch_array($result)) {

            $grader = $_POST['result'];
            $x = $row['year_1'] + $row['year_2'] + $row['year_3'] + $row['year_4'] + $row['Final'];
            $grader = $x / 5;


            if ($grader <= 100 and $grader >= 89) {
                $grade = "A";
            } elseif ($grader <= 90 and $grader >= 79) {
                $grade = "B";
            } elseif ($grader <= 80 and $grader >= 69) {
                $grade = "C";
            } elseif ($grader <= 70 and $grader >= 59) {
                $grade = "D";
            } else {
                $grade = "F";
            }

            if ($row['Final'] > $set['2011']) {
                $year2011 = $row;
                $set['2011'] = $row['Final'];
            }
            if ($row['Final'] < $set['2012']) {
                $year2011 = $row;
                $set['2011'] = $row['Final'];
            }
            if ($row['Final'] < $set['2013']) {
                $year2011 = $row;
                $set['2011'] = $row['Final'];
            }
            if ($row['Final'] < $set['2014']) {
                $year2011 = $row;
                $set['2011'] = $row['Final'];
            }
            if ($row['Final'] < $set['final']) {
                $finalscore = $row;
                $set['final'] = $row['Final'];
            }
            if ($row['Final_Grade'] < $set['Final_Grade']) {
                $overall = $row;
                $set['Final_Grade'] = $row['Final_Grade'];
            }

            echo "<tr><td>";
            echo "$row[Name]";
            echo "</td><td>";
            echo "$row[year_1]";
            echo "</td><td>";
            echo "$row[year_2]";
            echo "</td><td>";
            echo "$row[year_3]";
            echo "</td><td>";
            echo "$row[year_4]";
            echo "</td><td>";
            echo "$row[Final]";
            echo "</td><td>";
            echo "$grade";
            echo "</td></tr>";
        }

        echo "<tr><td colspan=7>Topper for 2011: </td></tr>";
        echo "<tr><td> $year2011[Name] </td>";
        echo "<td>";
        echo $year2011['year_1'];
        echo "</td><td>";
        echo $year2011['year_2'];
        echo "</td><td>";
        echo $year2011['year_3'];
        echo "</td><td>";
        echo $year2011['year_4'];
        echo "</td><td>";
        echo $year2011['Final'];
        echo "</td><td>";
        echo "$grade";
        echo "</td></tr>";
        echo "<br>";

        echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
        echo "<tr><td>";
        echo $year2012['Name'];
        echo "</td><td>";
        echo $year2012['year_1'];
        echo "</td><td>";
        echo $year2012['year_2'];
        echo "</td><td>";
        echo $year2012['year_3'];
        echo "</td><td>";
        echo $year2012['year_4'];
        echo "</td><td>";
        echo $year2012['Final'];
        echo "</td><td>";
        echo "$grade";
        echo "</td></tr>";
        echo "<br>";

        echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
        echo "<tr><td>";
        echo $year2013['Name'];
        echo "</td><td>";
        echo $year2013['year_1'];
        echo "</td><td>";
        echo $year2013['year_2'];
        echo "</td><td>";
        echo $year2013['year_3'];
        echo "</td><td>";
        echo $year2013['year_4'];
        echo "</td><td>";
        echo $year2013['Final'];
        echo "</td><td>";
        echo "$grade";
        echo "</td></tr>";
        echo "<br>";

        echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
        echo "<tr><td>";
        echo $year2014['Name'];
        echo "</td><td>";
        echo $year2014['year_1'];
        echo "</td><td>";
        echo $year2014['year_2'];
        echo "</td><td>";
        echo $year2014['year_3'];
        echo "</td><td>";
        echo $year2014['year_4'];
        echo "</td><td>";
        echo $year2014['Final'];
        echo "</td><td>";
        echo "$grade";
        echo "</td></tr>";

        echo "<tr><td colspan=7>Overall Topper: </td></tr>";
        echo "<tr><td>";
        echo $overall['Name'];
        echo "</td><td>";
        echo $overall['year_1'];
        echo "</td><td>";
        echo $overall['year_2'];
        echo "</td><td>";
        echo $overall['year_3'];
        echo "</td><td>";
        echo $overall['year_4'];
        echo "</td><td>";
        echo $overall['Final'];
        echo "</td><td>";
        echo "$grade";
        echo "</td></tr>";

        echo "</table>";

0 个答案:

没有答案