Hi all,
I hope somebody can help in relation to the code below.
With the working code below I;m trying to setup a results section that would output a soccer teams results eg.Everton have won 12, drawn 4, and lost 5 games to date.
The team position option works fine but I;m not sure how to integrate the additional function within the code for the results section?
See comments in code //
Thanks for the help.
Regards.
<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);
if(!empty($_POST["data"]) && $_POST["data"] == "position"){
echo getPosition($teamData);
}
}
}
function getPosition($teamData){
return "Team ". $teamData["team"] ." are currently number " . $teamData["position"] . " in the league ";
}
function getTeamData($team, $tablesPage){
// I'm trying to get the following code to work to output some data from the results page??
//$resultsPage = "http://www.bbc.com/sport/football/results"
//function getResults($teamData, $resultsPage){
//return "Team ". $teamData["team"] ." has Won ". $teamData["won"] ." games, lost " . $teamData["lost"] . " games and drew " . $teamData['drew'] . " games";
//function getresults($team, $tablesPage)
// }
//echo getPosition($teamData);
$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">
<option value=""></option>
<option value="Everton">Everton</option>
<option value="Chelsea">Chelsea</option>
<option value="Southampton">Southampton</option>
<option value="Liverpool">Liverpool</option>
<option value="Arsenal">Arsenal</option>
</select>
<select name="data">
<option value="position">position</option>
<option value="results">results</option>
<select>
<input type="submit" value="Get Data"></input>
</select>
</form>
</body>
</html>