Jump to content

Recommended Posts

Hi,

 

I have done all the form validation through the use of functions. But what I am trying to achieve is that all the validation must be completed before the final function which contains the end result/HTML output can be executed, how would I go about doing this? Here is my code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Footy scoreboard.</title>
</head>

<body>

<?
  //Determine if the sumbit button has been clicked. If so, begin validating form data.
  if ($_POST['Submit'] == "Submit")
  {
  	
	//Check if two same teams have been selected.
	if ($_POST['team1']== $_POST['team2'])
	{
		echo "You have selected the same two teams. <br /><br />";			
	}
	else 
	{
		//Assign teams
		team1();
		team2();
		//Determine if a Player Name for team 1 was entered.
		playerNameTeam1();
		// Determine if a text value was entered for the player's name.
		playerNameValidationTeam1();
		//Determine whether the player name already exists.
		checkIfNameExistsTeam1();
		//If one or more goals fields and points fields arent filled out, automatically set their variables to 0. Also check if the amount of goals and points entered are integer.
		goalsAndPointsValidationTeam1();
		//Determine if a Player Name for team 2 was entered.
		playerNameTeam2();
		// Determine if a text value was entered for the player's name.
		playerNameValidationTeam2();
		//Determine whether the player name already exists.
		checkIfNameExistsTeam2();
		//If one or more goals fields and points fields arent filled out, automatically set their variables to 0. Also check if the amount of goals and points entered are integer.
		goalsAndPointsValidationTeam2();
		//Display results.
		teamsArray();				
	}
}

  function team1()
  {
	if ($_POST['team1'] == "1")
	{
		$team = "Adelaide Crows";
	}
	if ($_POST['team1']  == "2")
	{
		$team = "Brisbane Lions";
	}
	if ($_POST['team1']  == "3")
	{
		$team = "Carlton";			
	}
	if ($_POST['team1']  == "4")
	{
		$team = "Collingwood";			
	}
	if ($_POST['team1']  == "5")
	{
		$team = "Essendon";			
	}
	if ($_POST['team1']  == "6")
	{
		$team = "Fremantle";			
	}
	if ($_POST['team1']  == "7")
	{
		$team = "Geelong";		
	}
	if ($_POST['team1'] == "8")
	{
		$team = "Hawthorn";			
	}
	if ($_POST['team1']  == "9")
	{
		$team = "Melbourne";			
	}
	if ($_POST['team1']  == "10")
	{
		$team = "North Melbourne";			
	}
	if ($_POST['team1']  == "11")
	{
		$team = "Port Adelaide";			
	}
	if ($_POST['team1'] == "12")
	{
		$team = "Richmond";			
	}
	if ($_POST['team1'] == "13")
	{
		$team = "Saint Kilda";			
	}
	if ($_POST['team1'] == "14")
	{
		$team = "Sydney Swans";			
	}
	if ($_POST['team1'] == "15")
	{
		$team = "West Coast Eagles";			
	}
	if ($_POST['team1']== "16")
	{
		$team = "Western Bulldogs";			
	}

	return $team;
  }

  function team2()
  {
	if ($_POST['team2'] == "1")
	{
		$team = "Adelaide Crows";
	}
	if ($_POST['team2']  == "2")
	{
		$team = "Brisbane Lions";
	}
	if ($_POST['team2']  == "3")
	{
		$team = "Carlton";			
	}
	if ($_POST['team2']  == "4")
	{
		$team = "Collingwood";			
	}
	if ($_POST['team2']  == "5")
	{
		$team = "Essendon";			
	}
	if ($_POST['team2']  == "6")
	{
		$team = "Fremantle";			
	}
	if ($_POST['team2']  == "7")
	{
		$team = "Geelong";		
	}
	if ($_POST['team2'] == "8")
	{
		$team = "Hawthorn";			
	}
	if ($_POST['team2']  == "9")
	{
		$team = "Melbourne";			
	}
	if ($_POST['team2']  == "10")
	{
		$team = "North Melbourne";			
	}
	if ($_POST['team2']  == "11")
	{
		$team = "Port Adelaide";			
	}
	if ($_POST['team2'] == "12")
	{
		$team = "Richmond";			
	}
	if ($_POST['team2'] == "13")
	{
		$team = "Saint Kilda";			
	}
	if ($_POST['team2'] == "14")
	{
		$team = "Sydney Swans";			
	}
	if ($_POST['team2'] == "15")
	{
		$team = "West Coast Eagles";			
	}
	if ($_POST['team2']== "16")
	{
		$team = "Western Bulldogs";			
	}

	return $team;

  }	
  
  //Determine if a Player Name for team 1 was entered. 
  function playerNameTeam1()
{
	//this begins the loop
	for ($i = 1; $i <= 22; $i++) 
	{
		if ($_POST['team_1_player_' . $i.'_name'] == "")
		{
			//error message
			echo "Enter <b>player name " .$i. "</b> for <b>".team1($team)."</b> on line <b>" .$i ."</b><br /><br />";

		}
		else
		{
			//Perform no action.
		}
	}

}

function playerNameValidationTeam1()
{

	for ($i = 1; $i <= 22; $i++) 
	{
		if (trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "" )
		{
			// Determine if a text value was entered for the player's name.
			if (!preg_match('#^[a-zA-Z ]+$#i',trim(strtolower($_POST['team_1_player_' . $i.'_name']))))
			{
				//error message
				echo "You are required to input a <b>text value</b> in the <b>player's name</b> field on <b>line " . $i . " for ".team1($team).".</b><br /><br/>";	

			}
			else
			{
				//Perform no action.
			}
		}
		else
		{
		  //Perform no action.
		}
	}

}

function checkIfNameExistsTeam1()
{
	$players_names = array();
	for($i = 1; $i <= 22; $i++)
	{
		if(trim(strtolower($_POST['team_1_player_' . $i.'_name'])) != "")
		{
			$players_names[] = trim(strtolower($_POST['team_1_player_' . $i.'_name']));
		}
	}
	$num_of_duplicates = array_count_values($players_names);
	foreach($num_of_duplicates as $player => $key)
	{
		if($key > 1)
		{ 
			echo "The <b>player name " .$player. "</b> for <b>".team1($team)."</b> already exists.</b><br /><br />";

		}
		else
		{ 
			//Perform no action.
		}
	}
}   

function goalsAndPointsValidationTeam1()
{
	for ($i = 1; $i <= 22; $i++) 
	{
		//If one or more goal's fields arent filled out, automatically set their variables to 0.
		if ($_POST['team_1_player_' . $i.'_goals'] == "")
			{
				$_POST['team_1_player_' . $i.'_goals'] = "0"; 
			}
		//Checking if the amount of goals entered are integer
		else if (!preg_match('#^[0-9 ]+$#i',$_POST['team_1_player_' . $i.'_goals']))
		{
		    //error message
			echo "You are required to input an <b>integer value</b> in the <b>goals</b> field on <b>line " . $i . " for ".team1($team).".</b><br /><br/>";

		}
		else
		{
			//Perform no action. 
		}
		//If one or more point's fields arent filled out, automatically set their variables to 0.
		if ($_POST['team_1_player_' . $i.'_points'] == "")
			{
				$_POST['team_1_player_' . $i.'_points'] = "0"; 
			}
		//Checking if the amount of points entered are integer
		else if (!preg_match('#^[0-9 ]+$#i',$_POST['team_1_player_' . $i.'_points']))
		{
			//error message
			echo "You are required to input an <b>integer value</b> in the <b>points</b> field on <b>line " . $i . " for ".team1($team).".</b><br /><br/>";

		}
		else
		{
			//Perform no action.
		}
	}

}

//Determine if a Player Name for team 2 was entered. 
  function playerNameTeam2()
{
	//this begins the loop
	for ($i = 1; $i <= 22; $i++) 
	{
		if ($_POST['team_2_player_' . $i.'_name'] == "")
		{
			//error message
			echo "Enter <b>player name " .$i. "</b> for <b>".team2($team)."</b> on line <b>" .$i ."</b><br /><br />";

		}
		else
		{
			//Perform no action.
		}
	}

}

function playerNameValidationTeam2()
{

	for ($i = 1; $i <= 22; $i++) 
	{
		if (trim(strtolower($_POST['team_2_player_' . $i.'_name'])) != "" )
		{
			// Determine if a text value was entered for the player's name.
			if (!preg_match('#^[a-zA-Z ]+$#i',trim(strtolower($_POST['team_2_player_' . $i.'_name']))))
			{
				//error message
				echo "You are required to input a <b>text value</b> in the <b>player's name</b> field on <b>line " . $i . " for ".team2($team).".</b><br /><br/>";	

			}
			else
			{
				//Perform no action.
			}
		}
		else
		{
		  //Perform no action.
		}
	}

}

function checkIfNameExistsTeam2()
{
	$players_names = array();
	for($i = 1; $i <= 22; $i++)
	{
		if(trim(strtolower($_POST['team_2_player_' . $i.'_name'])) != "")
		{
			$players_names[] = trim(strtolower($_POST['team_2_player_' . $i.'_name']));
		}
	}
	$num_of_duplicates = array_count_values($players_names);
	foreach($num_of_duplicates as $player => $key)
	{
		if($key > 1)
		{ 
			echo "The <b>player name " .$player. "</b> for <b>".team2($team)."</b> already exists.</b><br /><br />";

		}
		else
		{ 
			//Perform no action.
		}
	}
}   

function goalsAndPointsValidationTeam2()
{
	for ($i = 1; $i <= 22; $i++) 
	{
		//If one or more goal's fields arent filled out, automatically set their variables to 0.
		if ($_POST['team_2_player_' . $i.'_goals'] == "")
			{
				$_POST['team_2_player_' . $i.'_goals'] = "0"; 
			}
		//Checking if the amount of goals entered are integer
		else if (!preg_match('#^[0-9 ]+$#i',$_POST['team_2_player_' . $i.'_goals']))
		{
		    //error message
			echo "You are required to input an <b>integer value</b> in the <b>goals</b> field on <b>line " . $i . " for ".team2($team).".</b><br /><br/>";

		}
		else
		{
			//Perform no action. 
		}
		//If one or more point's fields arent filled out, automatically set their variables to 0.
		if ($_POST['team_2_player_' . $i.'_points'] == "")
			{
				$_POST['team_2_player_' . $i.'_points'] = "0"; 
			}
		//Checking if the amount of points entered are integer
		else if (!preg_match('#^[0-9 ]+$#i',$_POST['team_2_player_' . $i.'_points']))
		{
			//error message
			echo "You are required to input an <b>integer value</b> in the <b>points</b> field on <b>line " . $i . " for ".team2($team).".</b><br /><br/>";

		}
		else
		{
			//Perform no action.
		}
	}

}

function teamsArray()
{
	//ARRAY
	//Go through $_POST array and put all values into $teamsArray.
	foreach ($_POST as $key => $value)
	{
		if ($value != "")
		{
			$teamsArray[$key] = $value;
		}
	}
	$i = 0;
	foreach ($teamsArray as $key => $value)
	{
		if($_POST['team_1_player_' . $i.'_goals'] != "" || $_POST['team_1_player_' . $i.'_points'] != "")
		{
			$goalWorth1 = "6";
			$pointWorth1 = "1";
			$goalScore1 = $teamsArray['team_1_player_' . $i.'_goals'] * $goalWorth1;
			$pointScore1 = $teamsArray['team_1_player_' . $i.'_points'] * $pointWorth1;
			$player1Score = $goalScore1 + $pointScore1;
			$team1Score = $player1Score + $team1Score;
			$amountGoals1 = $teamsArray['team_1_player_' . $i.'_goals'] + $amountGoals1;
			$amountPoints1 = $teamsArray['team_1_player_' . $i.'_points'] + $amountPoints1;	
						}
		$i = $i + 1;
	}

	$i = 0;
	foreach ($teamsArray as $key => $value)
	{
		if($_POST['team_2_player_' . $i.'_goals'] != "" || $_POST['team_2_player_' . $i.'_points'] != "")
		{
			$goalWorth2 = "6";
			$pointWorth2 = "1";
			$goalScore2 = $teamsArray['team_2_player_' . $i.'_goals'] * $goalWorth2;
			$pointScore2 = $teamsArray['team_2_player_' . $i.'_points'] * $pointWorth2;
			$player2Score = $goalScore2 + $pointScore2;
			$team2Score = $player2Score + $team2Score;
			$amountGoals2 = $teamsArray['team_2_player_' . $i.'_goals'] + $amountGoals2;
			$amountPoints2 = $teamsArray['team_2_player_' . $i.'_points'] + $amountPoints2;				
		}
		$i = $i + 1;
	}
	//XHTML Output.
	echo '<table cols="2" border="0" cellpadding="5" cellspacing="0" align="center" width="50%">';
	echo '<tr>';
	echo '<td width="75%" bgcolor="black" valign="top">';
	echo '<font color="gray" />';
	echo "<b><center><u> Scoreboard: ".team1($team)." vs ".team2($team)."</center></b></u>";
	echo "<center>";
	echo "<b>".team1($team).":</b><br/>";
	echo "<b>Goals: </b>".$amountGoals1." <br /> <b>Points: </b>" . $amountPoints1." <br /> <b>Total Score: </b>". $team1Score."<br /><br />";
	echo "<b>".team2($team).":</b><br/>";
	echo "<b>Goals: </b>".$amountGoals2." <br /> <b>Points: </b>" . $amountPoints2."  <br /><b>Total Score: </b>". $team2Score."<br /><br />";
	echo "<b>Final Result:</b> <br />";
	if($team1Score < $team2Score)
	{
		echo " ".team2($team).": ".$amountGoals2." ".$amountPoints2." (".$team2Score.") <b>defeated</b> ".team1($team).": ".$amountGoals1." ".$amountPoints1." (".$team1Score. ").";
	}
	else if ($team1Score > $team2Score)
	{
		echo " ".team1($team).": ".$amountGoals1." ".$amountPoints1." (".$team1Score.") <b>defeated</b> ".team2($team).": ".$amountGoals2." ".$amountPoints2." (".$team2Score. ").";
	}
	else if($team1Score == $team2Score)
	{
		echo " ".team1($team).": ".$amountGoals1." ".$amountPoints1." (".$team1Score.") <b>drew</b> ".team2($team).": ".$amountGoals2." ".$amountPoints2." (".$team2Score. ").";
	}
	echo "</center>";
	echo '</td>';
	echo '<td bgcolor="gray">';
	echo '</td></tr></table>';
	echo '<table cols="2" border="0" cellpadding="5" cellspacing="0" align="center" width="50%">';
	echo '<tr>';
	echo '<td width="75%" bgcolor="black" valign="top">';
	echo '<font color="gray" />';
	echo "<b><center><u>Goal Scorers for ".team1($team).":</center></b></u>";
	for ($i = 1; $i <= 22; $i++) 
	{
		if($_POST['team_1_player_' . $i.'_goals'] > 0)
		{
			echo "<center><b>".$teamsArray['team_1_player_' . $i.'_name']."</b></center>";
		}
	}
	echo '</td>';
	echo '<td bgcolor="gray">';
	echo "<b><center><u>Goals Scored:</u></center></b>";
	for ($i = 1; $i <= 22; $i++) 
	{
		if($_POST['team_1_player_' . $i.'_goals'] > 0)
		{
			echo "<b><center>".$teamsArray['team_1_player_' . $i.'_goals']."</b></center>";
		}
	}
	echo '</td></tr></table>';
	echo '<table cols="2" border="0" cellpadding="5" cellspacing="0" align="center" width="50%">';
	echo '<tr>';
	echo '<td width="75%" bgcolor="black" valign="top">';
	echo '<font color="gray" />';
	echo "<b><center><u>Goal Scorers for ".team2($team).":</center></b></u>";
	for ($i = 1; $i <= 22; $i++) 
	{
		if($_POST['team_2_player_' . $i.'_goals'] > 0)
		{
			echo "<center><b>".$teamsArray['team_2_player_' . $i.'_name']."</b></center>";
		}
	}
	echo '</td>';
	echo '<td bgcolor="gray">';
	echo "<b><center><u>Goals Scored:</center></b></u>";
	for ($i = 1; $i <= 22; $i++) 
	{
		if($_POST['team_2_player_' . $i.'_goals'] > 0)
		{
			echo "<b><center>".$teamsArray['team_2_player_' . $i.'_goals']."</b></center>";
		}
	}
	echo '</td></tr></table>';
}	
?>

<br /><a href="javascript:history.go(-1)">GO BACK</a>
</body>
</html>

 

The function that contains all the HTML output is called the teamsArray().

Team1 and team2 function arent validation functions they just assign values.

All the other functions are validation functions that I want to be executed before the teamsArray() can show the output, how would I go about doing that?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/172683-function-help/
Share on other sites

This is incredibly poor and messy code structure. Functions do not have access to values in the global scope (you have used several global variables in your functions). Variables required by the function should be passed as a parameter. Functions should return values i.e if a condition is not met in the function return false to the global scope. If a condition is met then return true to the global scope. Your functions do not return any value so all code will simply continue to execute.

 

Functions are written so that duplication of code does not occur. i.e multiple pages make use of a function. Your functions are simply wrappers. Learn correct usage of functions: http://uk3.php.net/manual/en/functions.user-defined.php

Link to comment
https://forums.phpfreaks.com/topic/172683-function-help/#findComment-910295
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.