Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15

Thread: function help.

  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts

    function help.

    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:

    PHP 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

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Do something like this:

    PHP Code:
    if(!playerNameTeam1();) {
        return 
    false;
    }
    if(!
    playerNameValidationTeam1();) {
        return 
    false;

    Just make your validate functions return false on error. I would also recommend renaming your functions to indicate that they validate something.

    There's a number of other enhancements that you can make to your code, but start there.
    Last edited by bacterozoid; 09-01-2009 at 01:04 PM.

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Comes up with a parse error if I do that.

  • #4
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Oops, that's my mistake - I had some bad semicolons in there

    PHP Code:
    if(!playerNameTeam1()) {
        return 
    false;
    }
    if(!
    playerNameValidationTeam1()) {
        return 
    false;


  • #5
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Just to confirm, where do I change the code you have given above? Within the functions or where the functions are listed?

  • #6
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Both

    When you call the functions, you need some sort of logic to determine if you have encountered an error. That's the code I provided you with. I'm checking to see if anyone of those functions returned false before proceeding. If it did return false, then there was an error validating, so we stop processing.

    Inside the function itself, you need to return false if there is an error. Here's a quick example:

    PHP Code:
    <?php

    $user_input 
    "enter a string here";

    if(!
    validateUserInputNotEmpty($user_input)) {
        echo 
    'User input was empty';
        return 
    false;
    }
    if(!
    validateUserInputNotStupid($user_input)) {
        echo 
    'User input was "stupid"';
        return 
    false;
    }

    echo 
    'I can only get here if the user inputted a string that is not empty and is not "stupid"';






    function 
    validateUserInputNotEmpty($user_input) {
        if(empty(
    $user_input)) {
            return 
    false// Error
        
    } else {
            return 
    true// No error
        
    }
    }

    function 
    validateUserInputNotStupid($user_input) {
        if(
    $user_input == 'stupid') {
            return 
    false// Error
        
    }
        else {
            return 
    true// No error
        
    }
    }


    ?>
    Edit: This is a simple validation example. Properly done, I would use just one validate function and return an error string or error code to tell me what the error was.

  • #7
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Ah I see.

    I tried doing it, now it isnt performing any validation at all and it just goes straight to the teamsArray function.

  • #8
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    What's your code look like now?

  • #9
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    PHP 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();
                if(!
    playerNameTeam1()) 
                {
                    return 
    false;
                }
                if(!
    playerNameValidationTeam1()) {
                    return 
    false;
                }
                if(!
    checkIfNameExistsTeam1()) {
                    return 
    false;
                }    
                if(!
    goalsAndPointsValidationTeam1()) {
                    return 
    false;
                }    
                if(!
    playerNameTeam2()) {
                    return 
    false;
                }    
                if(!
    playerNameValidationTeam2()) {
                    return 
    false;
                }    
                if(!
    checkIfNameExistsTeam2()) {
                    return 
    false;
                }    
                if(!
    goalsAndPointsValidationTeam2()) {
                    return 
    false;
                }
                
    //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 />";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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/>";    
                        return 
    false;
                    }
                    else
                    {
                        return 
    true;//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 />";
                    return 
    false;
                }
                else
                { 
                    return 
    true;//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/>";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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/>";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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 />";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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/>";    
                        return 
    false;
                    }
                    else
                    {
                        return 
    true;//Perform no action.
                    
    }
                }
                else
                {
                  return 
    true;//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 />";
                    return 
    false;
                }
                else
                { 
                    return 
    true;//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/>";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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/>";
                    return 
    false;
                }
                else
                {
                    return 
    true;//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>

  • #10
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Again, lots of your code needs some serious enhancement. You've done your validation basically right. The problem is that if your code doesn't find an error, it returns true too soon.

    Take this function as an example:

    PHP Code:
    function playerNameTeam1() {

        
    //this begins the loop
        
    for ($i 1$i <= 22$i++) {
            if (
    $_POST['team_1_player_' $i.'_name'] == "") {
                echo 
    "Enter <b>player name " .$i"</b> for <b>".team1($team)."</b> on line <b>" .$i ."</b><br /><br />";
                return 
    false;
            }
            else {
                return 
    true;// THIS IS YOUR PROBLEM
            
    }
        }


    Notice that you return true INSIDE the loop. That means that if, say, a name for player 1 is entered, the function will immediately return true and it won't even check the rest of the names.

    I re-wrote that function for you, ensuring to put the return true outside the loop:


    PHP Code:
    function playerNameTeam1() {

        for(
    $i 1$i <= 22$i++) {
            
    $post_name 'team_1_player_' $i '_name';
            if(empty(
    $POST[$post_name])) {
                echo 
    'Your error message here';
                return 
    false// This will cause your validation to error on the first problem it sees
            
    }
        }
        
        return 
    true// Make sure you return true OUTSIDE the loop so that all of the values get checked
        


  • #11
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks, it works now, though if I could enlist your help in a couple of things:

    1. How can I code it so that it shows all the validation errors at once like before, and not one by one?
    2. If there is a validation error, it doesnt show the 'Go Back' link like it did before, it only shows it with the end result.

    Thanks

  • #12
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Edit: ABOVE POST BY VICTORIA IS A SPAM LINK

    I'll help you through this one, but take some time and really think through your code and I bet you can find a solution on your own.

    Going back to the function I gave you a second ago:

    PHP Code:
    function playerNameTeam1() {

        
    // Initialize an error
        
    $error_string '';

        for(
    $i 1$i <= 22$i++) {
            
    $post_name 'team_1_player_' $i '_name';
            if(empty(
    $POST[$post_name])) {
                
    $error_string .= 'Your error message here<br/>';
                
    // Not returning false here anymore
            
    }
        }

        
    // Now check to see if there was an error by seeing if there is text inside $error_string
        
    if(!empty($error_string)) {
            echo 
    $error_string;
            return 
    false// return false to indicate an error
        
    }

        
    // PHP only get's here if there was no error, so return true
        
    return true;


    I'm not sure about your GO BACK link. It should appear since it's outside the execution of the PHP script entirely...

  • #13
    New Coder
    Join Date
    Oct 2008
    Posts
    75
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Yeh, that Go Back link is certainly a weird case. Thanks for all your help.

    Though I would just like to ask, you keep mentioning my code needs serious enhancement, in what terms do you mean this? Logic?

    Thanks.

  • #14
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    Actually, I figured it out. When you return false outside of a function, you are stopping the execution of the page entirely.

    So, the returns in your validation checks up here are causing the problem.

    PHP Code:
    if(!playerNameTeam1()) 
                {
                    return 
    false;
                }
                if(!
    playerNameValidationTeam1()) {
                    return 
    false;
                }
                if(!
    checkIfNameExistsTeam1()) {
                    return 
    false;
                }    
                if(!
    goalsAndPointsValidationTeam1()) {
                    return 
    false;
                }    
                if(!
    playerNameTeam2()) {
                    return 
    false;
                }    
                if(!
    playerNameValidationTeam2()) {
                    return 
    false;
                }    
                if(!
    checkIfNameExistsTeam2()) {
                    return 
    false;
                }    
                if(!
    goalsAndPointsValidationTeam2()) {
                    return 
    false;
                } 
    Try something like this:

    PHP Code:
    if(!playerNameTeam1() || !playerNameValidationTeam1()) {
                    echo 
    'additional errors';
                } else {
                   
    teamsArray(); 
                } 
    That checks each function. They return false on error, so I invert the result using !. That way, I'm checking to see if any one of the functions returned an error, echo out any additional message you want. Otherwise, show the result.

  • #15
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    As far as improving the code goes, things like your team1() function with all those if statements can be vastly improved using an array. You can also combine most of your separate team1/team2 validation functions by using an argument in your function call. Those are the biggest things. There may be some other minor problems, but I didn't look too close.

    I'd go ahead and get the validation working. It's probably worth starting a new thread if you're interested in improving your code some. I don't have time to go through that right now, unfortunately. I'm sure someone here would be happy to help, though.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •