1

So right now I have my MySql database on phpmyadmin connected to my PHP script. It lists 50 different NFL players and their stats from last year. I would like to be able to list a dropdown box to where I can sort the players by any of the categories (i.e. Receptions, Rec Yds, TDs, etc.) but am not sure how I would do this..?? I have a switch statement in there but it doesn't seem to be doing anything right now.

<!DOCTYPE html>
<html>
<!-- Seth Rataiczak -->
<head>
        <title>PHP Project</title>
        <style>
            table,th,td {
                border:1px solid navy;
                }
            body {
            background-color:peachpuff;
            }
        </style>
</head>

<body>

<?php
// database connection
    $db_hostname='localhost';
    $db_username='root';
    $db_password='';
    $db_database='Project';

    $connection = new mysqli(   $db_hostname,
                                $db_username,
                                $db_password,
                                $db_database);

//MySQL Select Statement

    $sort = "";
    if(isset($_GET['sort'])) {
        switch ($_GET['sort'] ) {
            case 0:
                $sort = ' ORDER BY Team DESC';
                break;
            case 1:
                $sort = ' ORDER BY Pos DESC';
                break;
            case 2:
                $sort = ' ORDER BY Rec DESC';
                break;
            case 3:
                $sort = ' ORDER BY Yds DESC';
                break;
            case 4:
                $sort = ' ORDER BY Avg DESC';
                break;
            case 5:
                $sort = ' ORDER BY Yds/G DESC';
                break;
            case 6:
                $sort = ' ORDER BY TD DESC';
                break;
        }
    }    

        $sql = "SELECT * FROM NFL_2014_Receiving WHERE Field=1" . $sort;
        $result = $connection->query($sql);
        if (!$result) die ($connection->error);
        $n = $result->num_rows;

        $nfl = array();

// echos the table headers
        echo "<table>
            <tr><th>ID</th><th>Player</th><th>Team</th>
            <th>Position</th><th>Receptions</th>
            <th>Receiving Yards</th><th>Avg Yds/Catch</th>
            <th>Avg Yds/Game</th><th>Touchdowns</th></tr>";

// echos the table data
        while ($row = $result->fetch_array(MYSQLI_ASSOC)){
            $nfl[$row['iD']] = $row['Player'];
            if(!isset($_POST['hide']) || $_POST['hide'] != $row['iD']){
                echo "<tr><td width=20>" . $row['iD'] . "</td><td width=150>" . $row['Player'] . "</td><td width=40>" .
                        $row['Team'] . "</td><td width=30>" . $row['Pos'] . "</td><td width=30>" .
                        $row['Rec'] . "</td><td width=40>" . $row['Yds'] . "</td><td width=30>" .
                        $row['Avg'] . "</td><td width=40>" . $row['Yds/G'] . "</td><td width=20>" .
                        $row['TD'] . "</td></tr>";
            }
        }
        echo "</table><br>";

//dropdown box
        echo "<form method='post' action='index.php'><select name='hide'>";
        foreach($nfl as $key=>$value){
            echo "<option value='".$key."'>".$value."</option>";
        }
// submit button
        echo "<input type='submit' value='Submit'>";
        echo "</select></form>";

?>

</body>
</html>
2
  • Are you sure $sort is getting set? Commented May 4, 2015 at 19:03
  • @JayBlanchard no I'm not sure. I'm kind of stuck at this point in my code Commented May 4, 2015 at 19:08

2 Answers 2

2

You are setting a POST method action and you are searching GET for value.

To be clear: You can change your form method to get <form method='get' action='index.php'> or change your php value $_GET['sort'] to $_POST['sort']

I m quite sure this is your problem but you can echo your $sql variable to see what your query does ;)

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

1 Comment

so you're saying that I should change it so I am searching for a POST value instead of a GET value?
1
  • the input submit is inside of select tag. must be after select, not inside.
  • the method of form must be equal to the parameter received, in this case must be GET.
  • the name of the select must be 'sort' instead of 'hide'

3 Comments

much better answer! I didn't even notice!
@DanteJavier I switched it up so the input submit is outside the select tag. I also changed it so everything is GET instead of POST. However, if i change it to 'sort' instead of 'hide' then it does not hide the player anymore when selected which is what I need. I need to get both where I can select a player and hide him when selected but also be able to choose from a dropdown box how I want to sort the table.
then change $_GET['sort'] to $_GET['hide']

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.