FlameDra Posted January 4, 2012 Share Posted January 4, 2012 Hello, Thanks to my new found knowledge of forms, I was able to make the following application! 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 : The result is : 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! Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2012 Share Posted January 4, 2012 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.) Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304148 Share on other sites More sharing options...
PaulRyan Posted January 4, 2012 Share Posted January 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304149 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304163 Share on other sites More sharing options...
KevinM1 Posted January 4, 2012 Share Posted January 4, 2012 foreach Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304165 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 foreach I went through that before, but the example was quite different. I don't get what the $id => $name is doing. Could you explain a bit? Sorry if I'm too much trouble. :S Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304166 Share on other sites More sharing options...
KevinM1 Posted January 4, 2012 Share Posted January 4, 2012 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]. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304169 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 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]? Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304171 Share on other sites More sharing options...
Vel Posted January 4, 2012 Share Posted January 4, 2012 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] Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304174 Share on other sites More sharing options...
KevinM1 Posted January 4, 2012 Share Posted January 4, 2012 Exactly. $array[$row][$column] Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304176 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 But isn't playerName only holding the name of the player and not anything else? How is it a multi-layer array then? Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304178 Share on other sites More sharing options...
KevinM1 Posted January 4, 2012 Share Posted January 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304180 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 So, $_POST[] is an array, and $_POST[playerName[]] is an array inside the main $_POST[] array? Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304181 Share on other sites More sharing options...
Vel Posted January 4, 2012 Share Posted January 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304183 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304184 Share on other sites More sharing options...
KevinM1 Posted January 4, 2012 Share Posted January 4, 2012 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'. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304213 Share on other sites More sharing options...
FlameDra Posted January 4, 2012 Author Share Posted January 4, 2012 Yeah, I knew that. Thats why I added player1/player2/player3 as names since they all had to be unique. Quote Link to comment https://forums.phpfreaks.com/topic/254342-using-functions-to-make-this-neater/#findComment-1304214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.