Jump to content

Using Functions to make this Neater?


FlameDra

Recommended Posts

Hello,

Thanks to my new found knowledge of forms, I was able to make the following application! :D

 

Let me just tell you a bit about what it does. I basically made this to calculate the percentage of the total scores from League of Legends, a game that me and some of my friends play. The game is played with 5 Players per team, and they can each Kill, Die, or Assist people throughout the game.

The purpose of this application is to find out the % Kill, % Death and % Assist per Player in a team throughout one game. It calculates this by doing something like a Player1Kills/TotalKills*100 calculation.

 

Here is the code which I wrote :

<!--
Author : Soufin 'FlameDra' Rahimeen
Date Started : 4/1/2011
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>League of Legends - Calculator</title>
    </head>
    <body>
        <form action="" method="POST">
            Player 1 : <input type="text" name="player1" value="Player Name"></input> 
            K : <input type="text" name="player1K" value="Kills"></input>
            D : <input type="text" name="player1D" value="Deaths"></input>
            A : <input type="text" name="player1A" value="Assists"></input><br />
            
            Player 2 : <input type="text" name="player2" value="Player Name"></input> 
            K : <input type="text" name="player2K" value="Kills"></input>
            D : <input type="text" name="player2D" value="Deaths"></input>
            A : <input type="text" name="player2A" value="Assists"></input><br />
            
            Player 3 : <input type="text" name="player3" value="Player Name"></input> 
            K : <input type="text" name="player3K" value="Kills"></input>
            D : <input type="text" name="player3D" value="Deaths"></input>
            A : <input type="text" name="player3A" value="Assists"></input><br />
            
            Player 4 : <input type="text" name="player4" value="Player Name"></input> 
            K : <input type="text" name="player4K" value="Kills"></input>
            D : <input type="text" name="player4D" value="Deaths"></input>
            A : <input type="text" name="player4A" value="Assists"></input><br />
            
            Player 5 : <input type="text" name="player5" value="Player Name"></input> 
            K : <input type="text" name="player5K" value="Kills"></input>
            D : <input type="text" name="player5D" value="Deaths"></input>
            A : <input type="text" name="player5A" value="Assists"></input><br />
            
            <br />
            <input type="Submit" value="Submit"></input>
            <br />
            <br />
            
        </form>
        
        <?php
        
        $totalK = $_POST['player1K'] + $_POST['player2K'] + $_POST['player3K'] + $_POST['player4K'] + $_POST['player5K'];
        $totalD = $_POST['player1D'] + $_POST['player2D'] + $_POST['player3D'] + $_POST['player4D'] + $_POST['player5D'];
        $totalA = $_POST['player1A'] + $_POST['player2A'] + $_POST['player3A'] + $_POST['player4A'] + $_POST['player5A'];
        
        ?>
        
        <br />
        
        <!-- Player 1 Area  -->
        Player <?php echo $_POST['player1']?> has 
        <?php 
        
        $player1Kpercent = $_POST['player1K']/$totalK*100;
        echo round("$player1Kpercent") . "%";
        
        ?>
        of total kills. He has 
        
        <?php
        
        $player1Dpercent = $_POST['player1D']/$totalD*100;
        echo round("$player1Dpercent") . "%";
        
        ?>
        
        of total deaths, and he has
        
        <?php
        
        $player1Apercent = $_POST['player1A']/$totalA*100;
        echo round("$player1Apercent") . "%";
        
        ?>
        
        of total Assists.
        
        
        <br />
        
        <!-- Player 2 Area  -->
        Player <?php echo $_POST['player2']?> has 
        <?php 
        
        $player2Kpercent = $_POST['player2K']/$totalK*100;
        echo round("$player2Kpercent") . "%";
        
        ?>
        of total kills. He has 
        
        <?php
        
        $player2Dpercent = $_POST['player2D']/$totalD*100;
        echo round("$player2Dpercent") . "%";
        
        ?>
        
        of total deaths, and he has
        
        <?php
        
        $player2Apercent = $_POST['player2A']/$totalA*100;
        echo round("$player2Apercent") . "%";
        
        ?>
        
        of total Assists.
        
        <br />
        
        <!-- Player 3 Area  -->
        Player <?php echo $_POST['player3']?> has 
        <?php 
        
        $player3Kpercent = $_POST['player3K']/$totalK*100;
        echo round("$player3Kpercent") . "%";
        
        ?>
        of total kills. He has 
        
        <?php
        
        $player3Dpercent = $_POST['player3D']/$totalD*100;
        echo round("$player3Dpercent") . "%";
        
        ?>
        
        of total deaths, and he has
        
        <?php
        
        $player3Apercent = $_POST['player3A']/$totalA*100;
        echo round("$player3Apercent") . "%";
        
        ?>
        
        of total Assists.
        
        
        <br />
        
        <!-- Player 4 Area  -->
        Player <?php echo $_POST['player4']?> has 
        <?php 
        
        $player4Kpercent = $_POST['player4K']/$totalK*100;
        echo round("$player4Kpercent") . "%";
        
        ?>
        of total kills. He has 
        
        <?php
        
        $player4Dpercent = $_POST['player4D']/$totalD*100;
        echo round("$player4Dpercent") . "%";
        
        ?>
        
        of total deaths, and he has
        
        <?php
        
        $player4Apercent = $_POST['player4A']/$totalA*100;
        echo round("$player4Apercent") . "%";
        
        ?>
        
        of total Assists.
        
        <br />
        
        <!-- Player 5 Area  -->
        Player <?php echo $_POST['player5']?> has 
        <?php 
        
        $player5Kpercent = $_POST['player5K']/$totalK*100;
        echo round("$player5Kpercent") . "%";
        
        ?>
        of total kills. He has 
        
        <?php
        
        $player5Dpercent = $_POST['player5D']/$totalD*100;
        echo round("$player5Dpercent") . "%";
        
        ?>
        
        of total deaths, and he has
        
        <?php
        
        $player5Apercent = $_POST['player5A']/$totalA*100;
        echo round("$player5Apercent") . "%";
        
        ?>
        
        of total Assists.
        
        <b />
        
        
       
      </body>
</html>

 

Just for example, some screenshots.

 

For values input like :

2zg9f28.jpg

 

The result is :

23tte9.jpg

 

Now, as you can see the code is quite long and may be hard to understand in case I want to expand it in the future.

So, I was wondering how I can use functions to make the code more readable and shorter.

I'm sorry, I'm not used to using functions (yet).

 

Thanks in advance! :)

Link to comment
Share on other sites

The thing that would help most, would be to use loops and array data structures.

 

Even with functions, without having loops and appropriate data structures, you would still be faced with writing x amount of function calls that operate on individually named and hard-coded scaler variables (as call time parameters.)

Link to comment
Share on other sites

I would place each player and stat into an array, setting the form element names to something like "playerKills[1]" "playerKills[2]" etc etc.

 

Then use a for each loop to calculate the data and echo it out.

 

My working example:

<!--
Author : Soufin 'FlameDra' Rahimeen
Date Started : 4/1/2011
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>League of Legends - Calculator</title>
    </head>
    <body>
        <form action="" method="POST">
            Player 1 : <input type="text" name="playerName[1]" value="Player Name"></input> 
            K : <input type="text" name="playerKills[1]" value="Kills"></input>
            D : <input type="text" name="playerDeaths[1]" value="Deaths"></input>
            A : <input type="text" name="playerAssists[1]" value="Assists"></input><br />
            
            Player 2 : <input type="text" name="playerName[2]" value="Player Name"></input> 
            K : <input type="text" name="playerKills[2]" value="Kills"></input>
            D : <input type="text" name="playerDeaths[2]" value="Deaths"></input>
            A : <input type="text" name="playerAssists[2]" value="Assists"></input><br />
            
            Player 3 : <input type="text" name="playerName[3]" value="Player Name"></input> 
            K : <input type="text" name="playerKills[3]" value="Kills"></input>
            D : <input type="text" name="playerDeaths[3]" value="Deaths"></input>
            A : <input type="text" name="playerAssists[3]" value="Assists"></input><br />
            
            Player 4 : <input type="text" name="playerName[4]" value="Player Name"></input> 
            K : <input type="text" name="playerKills[4]" value="Kills"></input>
            D : <input type="text" name="playerDeaths[4]" value="Deaths"></input>
            A : <input type="text" name="playerAssists[4]" value="Assists"></input><br />
            
            Player 5 : <input type="text" name="playerName[5]" value="Player Name"></input> 
            K : <input type="text" name="playerKills[5]" value="Kills"></input>
            D : <input type="text" name="playerDeaths[5]" value="Deaths"></input>
            A : <input type="text" name="playerAssists[5]" value="Assists"></input><br />
            
            <br />
            <input type="Submit" value="Submit"></input>
            <br />
            <br />
            
        </form>
        
<?PHP

  if($_SERVER['REQUEST_METHOD'] == 'POST') {

    //### Use array sum to get the total kills, deaths and assists.
    $totalKills   = array_sum($_POST['playerKills']);
    $totalDeaths  = array_sum($_POST['playerDeaths']);
    $totalAssists = array_sum($_POST['playerAssists']);

    //### Use a foreach loop to calculate each stat and echo it.
    foreach($_POST['playerName'] AS $id => $name) {
      echo 'Player '.$name.' has '.round($_POST['playerKills'][$id]/$totalKills*100).'% of total kills. '; // Kill Percentage
      echo 'He has '.round($_POST['playerDeaths'][$id]/$totalDeaths*100).'% of total deathss. '; // Death Percentage
      echo 'He has '.round($_POST['playerAssists'][$id]/$totalAssists*100).'% of total assists. <br>'; // Assist Percentage
    }

  }
?>

 

Regards, PaulRyan.

Link to comment
Share on other sites

Thanks :)

But could you elaborate the foreach loop a bit more?

 

foreach($_POST['playerName'] AS $id => $name) {
      echo 'Player '.$name.' has '.round($_POST['playerKills'][$id]/$totalKills*100).'% of total kills. '; // Kill Percentage
      echo 'He has '.round($_POST['playerDeaths'][$id]/$totalDeaths*100).'% of total deathss. '; // Death Percentage
      echo 'He has '.round($_POST['playerAssists'][$id]/$totalAssists*100).'% of total assists. <br>'; // Assist Percentage
    }

 

What is the $id and $name variables and what are they doing?

Link to comment
Share on other sites

Foreach iterates over an array.  There are two possible forms:

 

foreach ($array as $value) {
   // do something with each individual array value
}

 

and

 

foreach ($array as $key => $value) {
   // do something with each key/value pair
}

 

$array can be any array you want to iterate over.  You'd simply use its variable name in place of $array.  $key and $value are what are returned during the process.  You can name them any legit variable name.

 

In the example above, $_POST['playerName'] is itself an array.  $id is each array key, and $name is each array value at $_POST['playerName'][$id].

Link to comment
Share on other sites

Thanks for taking your time to clear that up.

 

But by the way, shouldn't it be $_POST['playerName[$id]'] instead of $_POST['playerName'][$id]?

No, when dealing with multi-layer arrays you always lay it out $_POST['playerName'][$id]

Link to comment
Share on other sites

But isn't playerName only holding the name of the player and not anything else? How is it a multi-layer array then?

 

Take a look at how PaulRyan wrote the form.  Note, specifically, "playerName[1]", "playerName[2]", etc.  Using array notation tells PHP to handle all playerNames as an array on POST.

Link to comment
Share on other sites

So, $_POST[] is an array, and $_POST[playerName[]] is an array inside the main $_POST[] array?

Yes, because you have multiple inputs with the same name they form and array inside the $_POST array.

Ah, thank you guys!

All this makes much more sense to me now. :)

Link to comment
Share on other sites

So, $_POST[] is an array, and $_POST[playerName[]] is an array inside the main $_POST[] array?

Yes, because you have multiple inputs with the same name they form and array inside the $_POST array.

 

That's only if you use array notation.  A bunch of inputs with the name "playerName[]" will form an array.  A bunch of inputs with the name "playerName" will result in only the last one being 'seen'.

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.