Jump to content

multiple attribute problem


noob321
Go to solution Solved by noob321,

Recommended Posts

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.

 

post-168390-0-02712300-1396780683_thumb.png

<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>
Link to comment
Share on other sites

You have left of the closing angle bracket (  >  ) for your select tag here

<select name="team" multiple="multiple" size="3"

Also when using the multiple attribute on your select tag you need append square brakes [] to the field name.

 

The chosen teams will be submitted as an array. Which will mean you will need to loop through the $_POST['team'] array to get each chosen team. Before looping over this array you may want to change the getTeamData() function so it one gets the results from the bbc once, not foreach time you want to get the result for a team.

Edited by Ch0cu3r
Link to comment
Share on other sites

First you need to remove these lines fromthe getTeamData function.

$xml = new DOMDocument(); 
@$xml->loadHtmlFile($tablesPage); //use DOM 

And place them into a new function, call it getLeageResults, you'd pass this function $tableData as an argument.

// this functions get the rsesults from the bbc, and returns the xml object
function getLeageResults($tableData)
{
    $xml = new DOMDocument(); 
    @$xml->loadHtmlFile($tablesPage); // get the DOM of the HTML

    return $xml; // return the xml object
}

Now you'd only call this function first to get the league table results.

$results = getLeagueResults($tableData);

Now you'd pass getTeamData() $results instead of $tableData. The first two lines of code for the getTeamData function should be

function getTeamData($team, $xmlResults){

	@$xpath = new DOMXPath($xmlResults); //use XPath

Now you can loop through the $_POST['team'] array and call getTeamData to get the chosen teams results.

$results = getLeageResults($tableData);

// loop through the teams, extracting their data from the results table
foreach($_POST['teams'] as $team)
{
   $teamData = getTeamData($team, $results);

   //.. now decide what to do with $teamData, eg show each selected teams results
   echo getResults($teamData) . '</br />';
}
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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 

Link to comment
Share on other sites

I have copied the code in post #5 and changed the variables on line 18 and 58 to $tablesPage and I do not get those errors. The only time I do get those errors is when choosing the empty option (before Everton) from the list. Make sure you have chosen a team from the list before submitting the form when testing the code

Link to comment
Share on other sites

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 />';
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Can you post your code?

 

This is what I am testing with and I get no errors.

<html>
<head>
  <style>
    .red {color: red}
  </style>
  <title>Table</title>
</head>
<body>
 <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>
<?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 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($tablesPage)
{
    $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);
}
 
 
?>
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.