Jump to content

noob321

Members
  • Posts

    10
  • Joined

  • Last visited

noob321's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. my bad , pasted your code - all good! thanks again :-)
  2. sure , here it is: <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"; $results = getLeagueResults($tablesPage); // loop through the teams, extracting their data from the results table foreach($_POST['team'] as $team) { $teamData = getTeamData($team, $results); //.. now decide what to do with $teamData, eg show each selected teams results echo getPosition($teamData) . '</br />'; echo getResults($teamData) . '</br />'; echo getPoints($teamData) . '</br />'; echo getDifference($teamData) . '</br />'; } } 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 " ; } // this functions get the rsesults from the bbc, and returns the xml object function getLeagueResults($tableData) { $xml = new DOMDocument(); @$xml->loadHtmlFile($tablesPage); // get the DOM of the HTML return $xml; // return the xml object } function getTeamData($team, $xmlResults){ /* 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" */ @$xpath = new DOMXPath($xmlResults); //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>
  3. So i added the additional echo's , now I'm getting : Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/table3.php on line 83 Warning: array_combine(): Both parameters should have an equal number of elements in /Applications/MAMP/htdocs/table3.php on line 86 Team are currently number in the league have won , drawn , and lost games to date have points under the current system Under two points for a win they would have 0 goal difference is at the moment Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/table3.php on line 83 Warning: array_combine(): Both parameters should have an equal number of elements in /Applications/MAMP/htdocs/table3.php on line 86 Team are currently number in the league have won , drawn , and lost games to date have points under the current system Under two points for a win they would have 0 goal difference is at the moment
  4. okay , thanks so much again. i tried it again ensuring i select a team or teams but no joy I'm afraid. maybe i need to add more echo's to // loop through the teams, extracting their data from the results table foreach($_POST['team'] as $team) { $teamData = getTeamData($team, $results); //.. now decide what to do with $teamData, eg show each selected teams results echo getResults($teamData) . '</br />'; }
  5. thanks again, i made that change, but am now getting : Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/table3.php on line 83 Warning: array_combine(): Both parameters should have an equal number of elements in /Applications/MAMP/htdocs/table3.php on line 86 have won , drawn , and lost games to date Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/table3.php on line 83 Warning: array_combine(): Both parameters should have an equal number of elements in /Applications/MAMP/htdocs/table3.php on line 86 have won , drawn , and lost games to date
  6. thanks so much, am getting a few errors though! Notice: Undefined variable: tableData in /Applications/MAMP/htdocs/table3.php on line 18 Notice: Undefined offset: 2 in /Applications/MAMP/htdocs/table3.php on line 83 Warning: array_combine(): Both parameters should have an equal number of elements in /Applications/MAMP/htdocs/table3.php on line 86 have won , drawn , and lost games to date ​and heres my code with your changes...maybe i have entered it wrong ... <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"; $results = getLeagueResults($tableData); // loop through the teams, extracting their data from the results table foreach($_POST['team'] as $team) { $teamData = getTeamData($team, $results); //.. now decide what to do with $teamData, eg show each selected teams results echo getResults($teamData) . '</br />'; } } 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 " ; } // this functions get the rsesults from the bbc, and returns the xml object function getLeagueResults($tableData) { $xml = new DOMDocument(); @$xml->loadHtmlFile($tablesPage); // get the DOM of the HTML return $xml; // return the xml object } function getTeamData($team, $xmlResults){ /* 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" */ @$xpath = new DOMXPath($xmlResults); //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>
  7. ok thanks I've corrected to multiple attribute tag, unfortunately I'm unsure as to where id fix the getTemData() to output multiple teams data
  8. Hello , i have had made a number of changes to the below program but am really stumped on my last task- i need a multiple attribute to pull multiple 'teams' data (points,goal difference etc) with php form. i can get single teams to work but not multiple. see screen attached and heres my code below. <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>
  9. thanks so much for your help
  10. HI all i need to write a small nested loop example to output a series of integers in the following format: 1 12 123 1234 12345 where the 1 is 10px, 2 is 20px font size etc. i have got to to output 1,2,3,4,5 but not my above desired output , can anyone point me in the right direction? heres my code: <?php $sizes =array("10px","20px","30px","40px","50px"); for($i = 0, $l = count($sizes); $i < $l; $i++){ $size = $sizes[$i]; $text = $i + 1; echo "<span style='font-size:$size'>$text</span><br/>"; } ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.