PHP问题:使用if语句

时间:2014-04-22 16:38:59

标签: php mysql if-statement

你好我的一个php项目有问题。问题是我需要为每年的班级提供最优秀的学生以及最好的整体学生。但是我的if语句有问题;我无法发布每个学生的姓名,其名称应该显示他们当年的分数。

数据库的照片:

http://imagizer.imageshack.us/a/img843/9674/7r2t.png

这是我的代码:

<body>
    <form>
        <?php
        $username = "amar";
        $password = "amar";
        $hostname = "localhost";
        $database = "study";
        $set = 100;
        $met = 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;
            $row[Final_Grade] = $grader;

               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) {
                $year1 = $grade;
                $year2011 = $row;
            }
            if ('$row[Final]' < $set) {
                $year2 = $grade;
                $year2012 = $row;
            }
            if ('$row[Final]' < $set) {
                $year3 = $grade;
                $year2013 = $row;
            }
            if ('$row[Final]' < $set) {
                $year4 = $grade;
                $year2014 = $row;
            }
            if ('$row[Final]' < $set) {
                $final = $grade;
                $finalscore = $row;
            }
            if ('$row[Final]' < $set) {
                $overall = $grade;
                $overall = $row;
            }

            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 "$year2011[Name]";
            echo "</td><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 "<tr><td colspan=7>Topper for 2012: </td></tr>";
            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 "<tr><td colspan=7>Topper for 2013: </td></tr>";
            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 "<tr><td colspan=7>Topper for 2014: </td></tr>";
            echo "$finalscore[Name]";
            echo "</td><td>";
            echo "$finalscore[year_1]";
            echo "</td><td>";
            echo "$finalscore[year_2]";
            echo "</td><td>";
            echo "$finalscore[year_3]";
            echo "</td><td>";
            echo "$finalscore[year_4]";
            echo "</td><td>";
            echo "$finalscore[Final]";
            echo "</td><td>";
            echo "$grade";
            echo "</td></tr>";

        echo "<tr><td colspan=7>Topper overall: </td></tr>";
            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>";

2 个答案:

答案 0 :(得分:2)

PHP中单引号的所有内容都被解释为字符串。当您执行'$row[Final]' PHP时,将其解释为“美元符号,左括号,单词'最终',关闭括号。”

为了让PHP解释您所说的变量的引用,您必须仅将您的索引作为字符串引用,而将其余部分引用到引号之外。

$row["Final"];告诉PHP,“检查变量$row并查看存储在字符串Final所代表的索引的内容。”

PHP: Strings - Manual

答案 1 :(得分:0)

看起来您需要为之后要显示的每个组(即每年最高的组)存储最高的结果,而不是仅在$ set中存储一个最高的商店。

不是100%肯定你想要什么,而是给你一个这样的想法: -

<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;
            $row['Final_Grade'] = $grader;

            switch (true)
            {
                case ($grader <= 100 and $grader >= 89) :
                    $grade = "A";
                    break;
                case ($grader <= 90 and $grader >= 79) :
                    $grade = "B";
                    break;
                case ($grader <= 80 and $grader >= 69) :
                    $grade = "C";
                    break;
                case ($grader <= 70 and $grader >= 59) :
                    $grade = "D";
                    break;
                default :
                    $grade = "F";
                    break;
            }

            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>".implode("</td><td>", $year2011)."</td></tr>";

        echo "<tr><td colspan=7>Topper for 2012: </td></tr>";
        echo "<tr><td>".implode("</td><td>", $year2012)."</td></tr>";

        echo "<tr><td colspan=7>Topper for 2013: </td></tr>";
        echo "<tr><td>".implode("</td><td>", $year2013)."</td></tr>";

        echo "<tr><td colspan=7>Topper for 2014: </td></tr>";
        echo "<tr><td>".implode("</td><td>", $year2014)."</td></tr>";

        echo "<tr><td colspan=7>Topper overall: </td></tr>";
        echo "<tr><td>".implode("</td><td>", $overall)."</td></tr>";

        echo "</table>";