dongyong1897 2015-04-19 22:23
浏览 25
已采纳

MySQL / PHP编码程序

I have the following code which links my phpmyadmin database to my PHP script. The code takes a table of 50 NFL players with their stats of the previous season. Now, I want to be able to code it so that I can select a player via a drop down box or some sort of dropdown box/list like that and then when selected and the Table is shown again, that player will not be listed. However, I am stuck and am not sure how to do this. Could someone at least help me out with the basics and what I would need for this?

<!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条回答 默认 最新

  • doukang7486 2015-04-19 22:52
    关注

    You can do it like this:

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

    Try it and let me know if it worked for you :-)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?