多属性表单输出


multiple attribute form output

我对下面的程序进行了许多更改,但我的上一个任务真的难住了 - 我需要一个多属性来使用 PHP 表单提取多个"团队"数据(积分、净胜球等)。 我可以让单个团队工作,但不能让多个团队工作。 请参阅附件屏幕,下面是我的代码。![在此输入图像描述][1]

   <html>
<head>
    <style>
        .red {color: red}
    </style>
    <title>Table</title>
</head>
<body>
<?php

 if ($_SERVER["REQUEST_METHOD"] == "POST"  ){
    $tablesPage = "http://www.bbc.com/sport/football/tables";
    if(!empty($_POST["team"])){
        $teamData= getTeamData($_POST["team"] , $tablesPage); //get associative array of team data
        if(!empty($_POST["data"]) && $_POST["data"] == "results"){
            echo getResults($teamData);
        } else if(!empty($_POST["data"]) && $_POST["data"] == "position"){
            echo getPosition($teamData); 
        } else if(!empty($_POST["data"]) && $_POST["data"] == "two points for a win"){
            echo getPoints($teamData); 
        } else if(!empty($_POST["data"]) && $_POST["data"] == "goal difference"){
            echo getDifference($teamData); 
        }
    }
}

function getPosition($teamData){
    /*This function takes an array of team data and returns a string containing the name of the team and its position in the leauge */
    return  "Team ". $teamData["team"]  ." are currently number " . $teamData["position"] . "  in the league " ;
}
function getResults($teamData){
    /*This function takes an array of team data and returns a string containing the results of the team */
    return  $teamData["team"]  ." have won " . $teamData["won"] . " , drawn " . $teamData["drew"] . " , and lost " . $teamData["lost"] . "  games to date  " ;
}
function getPoints($teamData){
    /*This function takes an array of team data and returns a string containing the points and calculates the old two points system */
    $oldpoints = $teamData["won"] * 2 + $teamData["drew"];
    return  $teamData["team"]  ." have " . $teamData["points"] . " points under the current system " .  "<br> Under two points for a win they would have " . $oldpoints ;
}
function getDifference($teamData){
    /*This function takes an array of team data and returns a string containing the name of the team and its goal difference in the leauge */
    return  $teamData["team"]  ." goal difference is " . $teamData["difference"] . " at the moment " ;
}
function getTeamData($team, $tablesPage){
    /* This function takes a webpage url and the name of a team as two string arguments. e.g. getTeam(""http://www.bbc.com/sport/football/tables", "liverpool")
    It returns an array of data about the team. 
    You don't need to understand what this function does just that it returns an array which contains keya and values. The values map to the following keys:
    "position", "team", "played", "won", "drew", "lost", "for", "against", "difference", "points"

    */
    $html = new DOMDocument(); 
    @$html->loadHtmlFile($tablesPage); //use DOM
    @$xpath = new DOMXPath($html); //use XPath
    $items = $xpath->query('//td/a[text()="' . $team . '"]/../..'); //get the relevant table row
    $values[] =  array();
    foreach($items as $node){
        foreach($node->childNodes as $child) {
        if($child->nodeType == 1) array_push($values, $child->nodeValue); //KLUDGE
        }
    }
    $values[2]  = substr($values[2], -1); //KLUDGE
    $values = array_slice( $values, 2, count($values)-4); //KLUDGE
    $keys = array("position", "team", "played", "won", "drew", "lost", "for", "against", "difference", "points");
    return array_combine($keys, $values);
}

?>
<br>
Select a team and a data item about that team
<form  method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<select name="team[]" multiple="multiple" size="3"> 
<option value=""></option>
  <option value="Everton">Everton</option>
  <option value="Arsenal">Arsenal</option>
  <option value="Chelsea">Chelsea</option>
  <option value="Man Utd">Man Utd</option>
  <option value="Liverpool">Liverpool</option>
</select>
<select name="data">
  <option value="position">position</option>
  <option value="results">results</option>
  <option value="two points for a win">two points for a win</option>
  <option value="goal difference">goal difference</option>
<select>
<input type="submit" value="Get Data"></input>
</select>
</form>
</body>
</html>

这是您需要替换的代码。我保留了"调试"代码,以便您可以看到正在发生的事情。完成后将其删除即可。

删除了调试代码。

测试代码:Windows xp上的PHP 5.3.18。

<?php

 if ($_SERVER["REQUEST_METHOD"] == "POST"  ){
    $tablesPage = "http://www.bbc.com/sport/football/tables";
    if(!empty($_POST["team"])){
        // remember that the $_POST['team'] is an array of team names
        foreach($_POST['team'] as $currentTeam) {
            $teamData= getTeamData($currentTeam , $tablesPage); //get associative array of team data
            if(!empty($_POST["data"]) && $_POST["data"] == "results"){
                echo getResults($teamData);
            } else if(!empty($_POST["data"]) && $_POST["data"] == "position"){
                echo getPosition($teamData);
            } else if(!empty($_POST["data"]) && $_POST["data"] == "two points for a win"){
                echo getPoints($teamData);
            } else if(!empty($_POST["data"]) && $_POST["data"] == "goal difference"){
                echo getDifference($teamData);
            }
            echo '<br />';
        } // end currentTeam
    }
}