Guest User

Handicap System

a guest
May 26th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.31 KB | None | 0 0
  1. <?php
  2. // This is an example script using the agolfhandicap class to record games played
  3. // and calculate your golf handicap index.
  4. // It is a multi user database and can keep track of games for different players.
  5. // First enter your name or user name then the game information.
  6. // This script really should be used with a user/password login system for security.
  7. //
  8. // Version 1.1.0: Added Edit and Delete for a record in the database.
  9. //
  10. // ############ You must edit the username, password and database name further down.
  11.  
  12. include "class.agolfhandicap.php";
  13. $gh=new agolfhandicap();
  14.  
  15. // ######### Edit the next line to put your MySQL username, password, and database name. ########
  16. $gh->setupdb('username','password','databasename');
  17.  
  18. // Get the user name if submitted.
  19. if($_POST['user']){
  20.  $user=$_POST['user'];
  21.  $gh->setuser($user);
  22. }
  23. if($_POST['delete']){          //delete the record
  24.   $id=$_POST['id'];
  25.   $gh->deleteGame($id);
  26. }
  27. if($_POST['edit']){           //edit the record
  28.   $id=$_POST['id'];
  29. }
  30. // If no user yet, ask for it.
  31. if(!isset($_POST['user'])){
  32.  print "<form name='usr' action='$PHP_SELF' method='POST'>";
  33.  print "Enter your user name:<input type='text' name='user' size='20'>";
  34.  print "<input type='hidden' name='hid' value='true'>";
  35.  print "<input type='submit' value='Submit' name='submits'>";
  36.  print "</form>";
  37. }else{
  38. // We have a username, so get new game data and display handicap and all games.
  39. print "<h2>Golf Game Database and Handicap Calculator for $user</h2>\n";
  40. $newgame=$gh->getnewgame($id);
  41. $id=($_POST['edit']=="")? "":$id;
  42. $gh->showform($id);
  43. $hc=$gh->gethcap();
  44. print ($hc==0)?"<h3>You need at least 5 games for a handicap index.</h3>":"<h3>Handicap for $user is $hc</h3><br>";
  45. // If you just want the data without displaying it, use next line.
  46. //$al=$gh->getAll();
  47. $gh->showall();
  48. }
  49. ?>
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. <?php
  60. // class.agolfhandicap.php
  61. // version 1.0.1, 27 August, 2005
  62. //
  63. // AGolfHandicap class is a multi user database and can keep track of games for
  64. //  multiple players and calculate their handicap indexes.
  65. // First enter your name or user name then the game information. It then
  66. //  calculates the handicap differential for the entered game using the score,
  67. //  course rating, and the slope rating.
  68. // A minimum of 5 games must be played before a handicap index can be calculated.
  69. //
  70. // This script really should be used with a user/password login system for security.
  71. //
  72. // version 1.0.1 -- 4 September, 2005
  73. //    Corrected a possible error when there are no games played or less than
  74. //    five games playes.
  75. // version 1.1.0 -- 9 September,2005
  76. //    Added a check to make sure all fields of a game are entered before
  77. //     saving it in the database.
  78. //    Added a Delete button to each line of the table displaying the games played.
  79. //    Added an Edit button to each line of the table displaying the games played.
  80. //    Made the date save in the database in the format of mm/dd/yyyy (with leading
  81. //     zeros) to fix a sorting bug.
  82. //
  83. // License
  84. //
  85. // Copyright (C) 2005 George A. Clarke, [email protected], http://gaclarke.com/
  86. //
  87. // This program is free software; you can redistribute it and/or modify it under
  88. // the terms of the GNU General Public License as published by the Free Software
  89. // Foundation; either version 2 of the License, or (at your option) any later
  90. // version.
  91. //
  92. // This program is distributed in the hope that it will be useful, but WITHOUT
  93. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  94. // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  95. //
  96. // You should have received a copy of the GNU General Public License along with
  97. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  98. // Place - Suite 330, Boston, MA 02111-1307, USA.
  99. //
  100.  
  101. class agolfhandicap
  102. {
  103. var $user;            // Username of golfer
  104. var $dbuser;          // Username for database
  105. var $pw;              // Password for database
  106. var $dbase;           // Database name
  107. var $db;
  108. var $all = array();   // Array used to return all game information from database
  109. var $use=array(0=>0,0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,8,9,10);
  110.                       // $use is an array used to determine how many games
  111.                       // to use to calculate the handicap index.
  112.  
  113. // Set up the MYSql database access.
  114. // $dbuser = database username
  115. // $pw = database password
  116. // $dbase = database name
  117.  
  118. function setupdb($dbuser,$pw,$dbase)
  119. {
  120.  $this->dbuser=$dbuser;
  121.  $this->pw=$pw;
  122.  $this->dbase=$dbase;
  123.  $this->db = mysql_connect('localhost',$dbuser,$pw);
  124. }
  125.  
  126. // Sets the player's name.
  127. function setuser($user)
  128. {
  129.  $this->user=$user;
  130.  return true;
  131. }
  132.  
  133. // Retrieves the submitted new game information and stores it in the database.
  134. function getnewgame($id="")
  135. {
  136. if($_POST['submit']){
  137. // print "submit=".$_POST['submit'];
  138. // $id=$_POST['id'];
  139.  $user=$_POST['user'];
  140.  $this->user=$user;
  141.  $date =$_POST['date1'];
  142. $d=explode("/",$date);
  143. $date=date("m/d/Y",mktime(0,0,0,$d[0],$d[1],$d[2]));
  144.  
  145.  $score = $_POST['score1'];
  146.  $crat = $_POST['crat1'];
  147.  $srat = $_POST['srat1'];
  148.  $course = $_POST['course1'];
  149. if(!$date || !$score || !$crat || !$srat || !$course){
  150.  return false;
  151. }else{
  152.  $hcap=round(($score-$crat)*113/$srat,1);
  153. if($id==""){
  154. $queryInsert = "INSERT INTO `golfhcapu` (user,course,date,score,crating,srating,hcapdiff) VALUES ('$user','$course','$date','$score','$crat','$srat','$hcap')";
  155. $resultGetPages = mysql_db_query($this->dbase, $queryInsert) or die ("Query failed: error was ".mysql_error());
  156. }else{
  157.     $upd="UPDATE `golfhcapu` SET user='$user',date='$date',course='$course',score='$score',crating='$crat',srating='$srat',hcapdiff='$hcap' WHERE id='$id'";
  158.     $result=mysql_db_query($this->dbase,$upd);
  159. }
  160.  $_POST['edit']="";
  161.  $_POST['id']="";
  162.  $_POST['date1']="";
  163.  $_POST['score1']="";
  164.  $_POST['crat1']="";
  165.  $_POST['srat1']="";
  166.  $_POST['course1']="";
  167. return true;
  168. }
  169. }else{
  170. return false;
  171. }
  172. }
  173.  
  174. // This function displays a form to fill out to input the following
  175. // new game information:
  176. //   Date
  177. //   18 hole score
  178. //   Course Rating
  179. //   Slope Rating of the course
  180. //   Name of the golf course
  181. function showform($id="")   //Give it the id number of a record to edit it.
  182. {
  183. if($id == ""){           //If not editing, get the POST data
  184. $date=$_POST['date1'];
  185. $score=$_POST['score1'];
  186. $crat=$_POST['crat1'];
  187. $srat=$_POST['srat1'];
  188. $course=$_POST['course1'];
  189. }else{                  //If editing get the values from the database
  190. $queryGetPages = "SELECT * FROM `golfhcapu` WHERE `id`='$id'";
  191. $resultGetPages = mysql_db_query($this->dbase, $queryGetPages) or die ("Query failed: error was ".mysql_error());
  192. $row = mysql_fetch_array($resultGetPages);
  193.  $date=$row['date'];
  194.  $score=$row['score'];
  195.  $crat=$row['crating'];
  196.  $srat=$row['srating'];
  197.  $course=$row['course'];
  198. }
  199. print ($id=="")?"<h4>Enter a new game.</h4>":"<h4>Edit the game</h4>";
  200. print "<table border=1>";
  201. print "<tr align='center'>";
  202. print "<td>Date<br>(mm/dd/yyyy)</td>";
  203. print "<td>Adjusted<br>Gross Score</td>";
  204. print "<td>USGA Course<br>Rating</td>";
  205. print "<td>USGA Slope<br>Rating</td>";
  206. print "<td>Course Name</td>";
  207. print "</tr>";
  208. print "<tr align='center'>";
  209. print "<form name='frm' action='$PHP_SELF' method='POST'>\n";
  210. print "<input type='hidden' name='user' value='$this->user'>\n";
  211. print "<input type='hidden' name='id' value='$id'>\n";
  212. print "<td><input type='text' size='15' name='date1' value='$date'></td>\n";
  213. print "<td><input type='text' size='5' name='score1' value='$score'></td>\n";
  214. print "<td><input type='text' size='5' name='crat1' value='$crat'></td>\n";
  215. print "<td><input type='text' size='5' name='srat1' value='$srat'></td>\n";
  216. print "<td><input type='text' size='30' name='course1' value='$course'></td>\n";
  217. print "</tr></table>\n";
  218. print "<br><table><tr>";
  219. print "<td><input type='submit' name='submit' value='Submit'>\n";
  220. print "</form></td>";
  221. print "<td><form name='frm' action='$PHP_SELF' method='POST'>\n";
  222. print "<input type='hidden' name='user' value='$this->user'>\n";
  223. print "<input type='submit' name='cancel' value='Cancel'>\n";
  224. print "</form></td></tr></table>";
  225. return true;
  226. }
  227.  
  228.  
  229. // Displays a table of all the games played by user with the latest game first. The
  230. // information displayed is:
  231. //    Game number
  232. //    Date of the game
  233. //    Adjusted Gross Score
  234. //    USGA Course Rating
  235. //    USGA Slope Rating of the course
  236. //    Handicap Differential of the game as calculated by this class
  237. //    Course name
  238.  
  239. function showall()
  240. {
  241. if($all=$this->getAll()){;
  242.  print "<table border=1>";
  243.  print "<tr align='center'>";
  244.  print "<td>Game<br>Number</td>";
  245.  print "<td>Date</td>";
  246.  print "<td>Adjusted<br>Gross Score</td>";
  247.  print "<td>USGA Course<br>Rating</td>";
  248.  print "<td>USGA Slope<br>Rating</td>";
  249.  print "<td>Handicap<br>Differential</td>";
  250.  print "<td>Course Name</td>";
  251.  print "<td>Edit</td>";
  252.  print "<td>Delete</td>";
  253.  print "</tr>";
  254.  $n=count($all)-1;
  255.  for($i=$n;$i>=0;$i--){
  256. $id=$all[$i]['id'];
  257.   $j=$i+1;
  258.   print "<tr align='center'>";
  259. print "<form method='POST' action='$PHP_SELF'>\n";
  260. print "<input type='hidden' name='user' value='$this->user'>\n";
  261. print "<input type='hidden' name='id' value='$id'>\n";
  262.   print "<td>$j</td>";
  263.   print "<td>".$all[$i]['date']."</td>";
  264.   print "<td>".$all[$i]['score']."</td>";
  265.   print "<td>".$all[$i]['crating']."</td>";
  266.   print "<td>".$all[$i]['srating']."</td>";
  267.   print "<td>".$all[$i]['hcapdiff']."</td>";
  268.   print "<td>".$all[$i]['course']."</td>";
  269. print "<td><input type='submit' name='edit' value='Edit'></td>\n";
  270. print "<td><input type='submit' name='delete' value='Delete'></td>\n";
  271. print "</form>";
  272.   print "</tr>";
  273.  }
  274.  print "</table>";
  275. }
  276.  
  277. }
  278.  
  279. // Retrieves the information for each game entered for 'user' in the database
  280. // and returns it in an multidimensional array.
  281. //
  282. //                  Data for the first game:
  283. //  array[0]['id']                    id number of record in database
  284. //  array[0]['date']                  date game was played
  285. //  array[0]['score']                 adjusted gross score of game
  286. //  array[0]['crating']               course rating
  287. //  array[0]['srating']               slope rating
  288. //  array[0]['hcapdiff']              handicap differential
  289. //  array[0]['course']                course name
  290.  
  291. //                  Data for the second game:
  292. //  array[1]['id']
  293. //  array[1]['date']
  294. //  array[1]['score']
  295. //  array[1]['crating']
  296. //  array[1]['srating']
  297. //  array[1]['hcapdiff']
  298. //  array[1]['course']
  299. //
  300. //        etc
  301. //
  302. function getAll()
  303. {
  304. $queryGetPages = "SELECT * FROM `golfhcapu` WHERE `user`='$this->user' ORDER BY `date`";
  305. $resultGetPages = mysql_db_query($this->dbase, $queryGetPages) or die ("Query failed: error was ".mysql_error());
  306. $n=mysql_num_rows($resultGetPages);
  307. if($n > 0){
  308.  $i=0;
  309.  while ($row = mysql_fetch_array($resultGetPages)){
  310.  $this->all[$i]['id']=$row['id'];
  311.  $this->all[$i]['date']=$row['date'];
  312.  $this->all[$i]['score']=$row['score'];
  313.  $this->all[$i]['crating']=$row['crating'];
  314.  $this->all[$i]['srating']=$row['srating'];
  315.  $this->all[$i]['hcapdiff']=$row['hcapdiff'];
  316.  $this->all[$i]['course']=$row['course'];
  317.  $i=$i+1;
  318.  }
  319.  return $this->all;
  320. }else{
  321.  return false;
  322. }
  323. }
  324.  
  325. // Reads the database and retrieves up to the last 20 games played.
  326. // Determines how many of these games to use, and which ones, to calculate
  327. //  the handicap index.
  328. // Using the chosen games, calculates the handicap index and returns it.
  329. // If fewer than five games have been played, returns 0.
  330. // A minimum of five games must have been played to determine the handicap index.
  331. //
  332. function gethcap()
  333. {
  334. $queryGetGames = "SELECT * FROM `golfhcapu` WHERE `user`='$this->user' ORDER BY `date` DESC LIMIT 20";
  335. $resultGetGames = mysql_db_query($this->dbase, $queryGetGames) or die ("Query failed: error was ".mysql_error());
  336. $nr=mysql_num_rows($resultGetGames);
  337. if($nr > 4){
  338.  $tot=0;
  339.  $n=$this->use[$nr];
  340.  for($i=0;$i<$nr;$i++){
  341.   $row = mysql_fetch_array($resultGetGames);
  342.   $hcd[$i]=$row['hcapdiff'];
  343.  }
  344.  sort($hcd);
  345.  for($i=0;$i<$n;$i++){
  346.   $tot=$tot+$hcd[$i];
  347.  }
  348.  $hcap=floor(10*(($tot/$n)*.96))/10;
  349.  return $hcap;
  350. }else{
  351.  return 0;
  352. }
  353. }
  354. function deleteGame($id)
  355. {
  356.     $sql = "DELETE FROM golfhcapu WHERE id=$id";
  357.     $result = mysql_db_query($this->dbase, $sql) or die ("Delete failed: error was ".mysql_error());
  358.     return;
  359. }
  360. }
  361. //   End of class
Advertisement
Add Comment
Please, Sign In to add comment