我已经对下面的程序进行了一些更改,但我真的很难接受我的上一个任务 - 我需要一个多属性来拉动多个团队'使用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>
答案 0 :(得分:0)
以下是您需要替换的代码。我已经保留了“调试”代码,因此您可以看到发生了什么。完成后删除它。
删除了调试代码。
经过测试的代码: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
}
}