0

Hello I have a problem in one of my php projects. The problem states that I need to provide the top students for each year of classes as well as the best overall student. However i am having trouble with the if statments; I am unable to post the name of each student where the name should be it displays their score for the year.

pic of database:

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

here is my code:

<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>";
3
  • An expression like '$row[Final]' is tolerated by PHP but it will generate some notices. Write it like: $row['Final']. Similarly echo "$row[Name]"; becomes echo $row["Name"]; Commented Apr 22, 2014 at 16:40
  • 1
    also $grader = "$_POST[result]"; should be $grader = $_POST['result']; Commented Apr 22, 2014 at 16:42
  • 2
    @Doge '$row[Final]' will be that exact string because of single quotes. No notices. However, it is still wrong and should be $row['Final']. Commented Apr 22, 2014 at 16:43

2 Answers 2

2

Everything in single quotes in PHP gets interpreted literally as a string. When you do '$row[Final]' PHP interprets that as "dollar sign, open bracket, the word 'Final', close bracket."

In order to have PHP interpret what you're saying as a reference to a variable, you have to reference only your index as a string, leaving the rest outside your quotes.

$row["Final"]; tells PHP, "check the variable $row and see what's stored at it's index represented by the string Final."

PHP: Strings - Manual

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like you need to store the highest result for each group you want to display later (ie, highest for each year), rather than just one single highest store in $set.

Not 100% sure on what you want, but to give you an idea something like this:-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.