MySQL / PHP编码程序

时间:2015-04-19 22:23:45

标签: php mysql database phpmyadmin

我有以下代码将我的phpmyadmin数据库链接到我的PHP脚本。该代码采用了50个NFL球员的表格,其中包含了上个赛季的统计数据。现在,我希望能够对其进行编码,以便我可以通过下拉框或某种类型的下拉框/列表选择播放器,然后在选中并再次显示表格时,将不会列出该播放器。但是,我被困住了,不知道该怎么做。有人至少可以帮助我解决基本问题以及我需要做些什么吗?

<!DOCTYPE html>
<html>
<head>
        <title>PHP Project</title>
        <style>
            table,th,td {
                border: 1px solid navy;
                }
        </style>
</head>

<body>

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

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

    if ($connection->connect_error) {
        echo "Sorry";
    } else {
        echo "Connected!<br><br>";      
        $sql = "SELECT * FROM NFL2014Receiving";
        $result = $connection->query($sql);
        if (!$result) die ($connection->error);
        $n = $result->num_rows;     

        for ($i=1; $i<=$n; $i++) {
            $row = $result->fetch_array(MYSQLI_ASSOC);

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

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

?>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

你可以这样做:

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

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

    if ($connection->connect_error) {
        echo "Sorry";
    } else {
        echo "Connected!<br><br>";      
        $sql = "SELECT * FROM NFL2014Receiving";
        $result = $connection->query($sql);
        if (!$result) die ($connection->error);
        $n = $result->num_rows;     

        $nfl = array();

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

        for ($i=1; $i<=$n; $i++) {
            $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>";
        echo "<form method='post' action=''><select name='hide'>";
        foreach($nfl as $key=>$value){
            echo "<option value='".$key."'>".$value."</option>";
        }
        echo "<input type='submit' value='Submit'>";
        echo "</select></form>";
    }

?>

尝试一下,让我知道它是否适合你: - )