php - multiple attribute form output -
i have had made number of changes below program stumped on last task- need multiple attribute pull multiple 'teams' data (points,goal difference etc) php form. can single teams work not multiple. see screen attached , heres code below. ![enter image description here][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 win"){ echo getpoints($teamdata); } else if(!empty($_post["data"]) && $_post["data"] == "goal difference"){ echo getdifference($teamdata); } } } function getposition($teamdata){ /*this function takes array of team data , returns string containing name of team , position in leauge */ return "team ". $teamdata["team"] ." number " . $teamdata["position"] . " in league " ; } function getresults($teamdata){ /*this function takes array of team data , returns string containing results of team */ return $teamdata["team"] ." have won " . $teamdata["won"] . " , drawn " . $teamdata["drew"] . " , , lost " . $teamdata["lost"] . " games date " ; } function getpoints($teamdata){ /*this function takes array of team data , returns string containing points , calculates old 2 points system */ $oldpoints = $teamdata["won"] * 2 + $teamdata["drew"]; return $teamdata["team"] ." have " . $teamdata["points"] . " points under current system " . "<br> under 2 points win have " . $oldpoints ; } function getdifference($teamdata){ /*this function takes array of team data , returns string containing name of team , goal difference in leauge */ return $teamdata["team"] ." goal difference " . $teamdata["difference"] . " @ moment " ; } function getteamdata($team, $tablespage){ /* function takes webpage url , name of team 2 string arguments. e.g. getteam(""http://www.bbc.com/sport/football/tables", "liverpool") returns array of data team. don't need understand function returns array contains keya , values. values map 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 relevant table row $values[] = array(); foreach($items $node){ foreach($node->childnodes $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 team , data item 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 win">two points win</option> <option value="goal difference">goal difference</option> <select> <input type="submit" value="get data"></input> </select> </form> </body> </html>
here code need replace. have left 'debug' code in can see happening. delete when done.
removed debug code.
tested code: php 5.3.18 on windows xp.
<?php if ($_server["request_method"] == "post" ){ $tablespage = "http://www.bbc.com/sport/football/tables"; if(!empty($_post["team"])){ // remember $_post['team'] array of team names foreach($_post['team'] $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 win"){ echo getpoints($teamdata); } else if(!empty($_post["data"]) && $_post["data"] == "goal difference"){ echo getdifference($teamdata); } echo '<br />'; } // end currentteam } }
Comments
Post a Comment