waynem801 Posted July 11, 2007 Share Posted July 11, 2007 Ok, so i'll explain the best i can .. I've found a free php script that uses a DB to store a golf players score and handicap... Problem is, the way the php script is written it doesn't even require the person to be a registered user. It just ask the person to enter a username "so they could make up one if they wanted" and then it uses the username they entered to store the scores in the DB under that name. So here's my real problem... I'm trying to integrate this with my website. I have a forum on my website powered by " Vanilla". I would like to setup this script so that only people that are members of the forum can use the script. AND the script will look to see if the user is logged in or not, and if they are, it will us there data as the username for the php script to store the scores etc.. in. I'm sorry, but i dunno very much about php, anything i try to do right now with php takes hours and hours, i'm learning as much as possible, but i get stuck "with what ya'll would consider easy problems" all the time :-\ . So if anyone could help point me in the right direction with what i'm trying to do i will be forever in debt. Below is the php script and the SQL that came with it. First things first, i need to figure out the best place to put the SQL tables, since i'm trying to use the vanilla username db. here's the SQL that came with it too. CREATE TABLE `golfhcapu` ( `id` int(11) NOT NULL auto_increment, `user` varchar(30) NOT NULL default '', `course` varchar(50) default NULL, `date` varchar(10) NOT NULL default '', `score` int(11) NOT NULL default '0', `crating` decimal(11,1) NOT NULL default '0.0', `srating` int(11) default NULL, `hcapdiff` decimal(11,1) NOT NULL default '0.0', PRIMARY KEY (`id`) ) TYPE=MyISAM AUTO_INCREMENT=16 ; There are 2 php files for this script, here they are below. 1. agolfhandicap.php 2. class.agolfhandicap.php <?php // This is an example script using the agolfhandicap class to record games played // and calculate your golf handicap index. // It is a multi user database and can keep track of games for different players. // First enter your name or user name then the game information. // This script really should be used with a user/password login system for security. // // Version 1.1.0: Added Edit and Delete for a record in the database. // // ############ You must edit the username, password and database name further down. include "class.agolfhandicap.php"; $gh=new agolfhandicap(); // ######### Edit the next line to put your MySQL username, password, and database name. ######## $gh->setupdb('','',''); // Get the user name if submitted. if($_POST['user']){ $user=$_POST['user']; $gh->setuser($user); } if($_POST['delete']){ //delete the record $id=$_POST['id']; $gh->deleteGame($id); } if($_POST['edit']){ //edit the record $id=$_POST['id']; } // If no user yet, ask for it. if(!isset($_POST['user'])){ print "<form name='usr' action='$PHP_SELF' method='POST'>"; print "Enter your user name:<input type='text' name='user' size='20'>"; print "<input type='hidden' name='hid' value='true'>"; print "<input type='submit' value='Submit' name='submits'>"; print "</form>"; }else{ // We have a username, so get new game data and display handicap and all games. print "<h2>Golf Game Database and Handicap Calculator for $user</h2>\n"; $newgame=$gh->getnewgame($id); $id=($_POST['edit']=="")? "":$id; $gh->showform($id); $hc=$gh->gethcap(); print ($hc==0)?"<h3>You need at least 5 games for a handicap index.</h3>":"<h3>Handicap for $user is $hc</h3><br>"; // If you just want the data without displaying it, use next line. //$al=$gh->getAll(); $gh->showall(); } ?> <?php // class.agolfhandicap.php // version 1.0.1, 27 August, 2005 // // AGolfHandicap class is a multi user database and can keep track of games for // multiple players and calculate their handicap indexes. // First enter your name or user name then the game information. It then // calculates the handicap differential for the entered game using the score, // course rating, and the slope rating. // A minimum of 5 games must be played before a handicap index can be calculated. // // This script really should be used with a user/password login system for security. // // version 1.0.1 -- 4 September, 2005 // Corrected a possible error when there are no games played or less than // five games playes. // version 1.1.0 -- 9 September,2005 // Added a check to make sure all fields of a game are entered before // saving it in the database. // Added a Delete button to each line of the table displaying the games played. // Added an Edit button to each line of the table displaying the games played. // Made the date save in the database in the format of mm/dd/yyyy (with leading // zeros) to fix a sorting bug. // // License // // Copyright (C) 2005 George A. Clarke, webmaster@gaclarke.com, http://gaclarke.com/ // // This program is free software; you can redistribute it and/or modify it under // the terms of the GNU General Public License as published by the Free Software // Foundation; either version 2 of the License, or (at your option) any later // version. // // This program is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along with // this program; if not, write to the Free Software Foundation, Inc., 59 Temple // Place - Suite 330, Boston, MA 02111-1307, USA. // class agolfhandicap { var $user; // Username of golfer var $dbuser; // Username for database var $pw; // Password for database var $dbase; // Database name var $db; var $all = array(); // Array used to return all game information from database 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); // $use is an array used to determine how many games // to use to calculate the handicap index. // Set up the MYSql database access. // $dbuser = database username // $pw = database password // $dbase = database name function setupdb($dbuser,$pw,$dbase) { $this->dbuser=$dbuser; $this->pw=$pw; $this->dbase=$dbase; $this->db = mysql_connect('localhost',$dbuser,$pw); } // Sets the player's name. function setuser($user) { $this->user=$user; return true; } // Retrieves the submitted new game information and stores it in the database. function getnewgame($id="") { if($_POST['submit']){ // print "submit=".$_POST['submit']; // $id=$_POST['id']; $user=$_POST['user']; $this->user=$user; $date =$_POST['date1']; $d=explode("/",$date); $date=date("m/d/Y",mktime(0,0,0,$d[0],$d[1],$d[2])); $score = $_POST['score1']; $crat = $_POST['crat1']; $srat = $_POST['srat1']; $course = $_POST['course1']; if(!$date || !$score || !$crat || !$srat || !$course){ return false; }else{ $hcap=round(($score-$crat)*113/$srat,1); if($id==""){ $queryInsert = "INSERT INTO `golfhcapu` (user,course,date,score,crating,srating,hcapdiff) VALUES ('$user','$course','$date','$score','$crat','$srat','$hcap')"; $resultGetPages = mysql_db_query($this->dbase, $queryInsert) or die ("Query failed: error was ".mysql_error()); }else{ $upd="UPDATE `golfhcapu` SET user='$user',date='$date',course='$course',score='$score',crating='$crat',srating='$srat',hcapdiff='$hcap' WHERE id='$id'"; $result=mysql_db_query($this->dbase,$upd); } $_POST['edit']=""; $_POST['id']=""; $_POST['date1']=""; $_POST['score1']=""; $_POST['crat1']=""; $_POST['srat1']=""; $_POST['course1']=""; return true; } }else{ return false; } } // This function displays a form to fill out to input the following // new game information: // Date // 18 hole score // Course Rating // Slope Rating of the course // Name of the golf course function showform($id="") //Give it the id number of a record to edit it. { if($id == ""){ //If not editing, get the POST data $date=$_POST['date1']; $score=$_POST['score1']; $crat=$_POST['crat1']; $srat=$_POST['srat1']; $course=$_POST['course1']; }else{ //If editing get the values from the database $queryGetPages = "SELECT * FROM `golfhcapu` WHERE `id`='$id'"; $resultGetPages = mysql_db_query($this->dbase, $queryGetPages) or die ("Query failed: error was ".mysql_error()); $row = mysql_fetch_array($resultGetPages); $date=$row['date']; $score=$row['score']; $crat=$row['crating']; $srat=$row['srating']; $course=$row['course']; } print ($id=="")?"<h4>Enter a new game.</h4>":"<h4>Edit the game</h4>"; print "<table border=1>"; print "<tr align='center'>"; print "<td>Date<br>(mm/dd/yyyy)</td>"; print "<td>Adjusted<br>Gross Score</td>"; print "<td>USGA Course<br>Rating</td>"; print "<td>USGA Slope<br>Rating</td>"; print "<td>Course Name</td>"; print "</tr>"; print "<tr align='center'>"; print "<form name='frm' action='$PHP_SELF' method='POST'>\n"; print "<input type='hidden' name='user' value='$this->user'>\n"; print "<input type='hidden' name='id' value='$id'>\n"; print "<td><input type='text' size='15' name='date1' value='$date'></td>\n"; print "<td><input type='text' size='5' name='score1' value='$score'></td>\n"; print "<td><input type='text' size='5' name='crat1' value='$crat'></td>\n"; print "<td><input type='text' size='5' name='srat1' value='$srat'></td>\n"; print "<td><input type='text' size='30' name='course1' value='$course'></td>\n"; print "</tr></table>\n"; print "<br><table><tr>"; print "<td><input type='submit' name='submit' value='Submit'>\n"; print "</form></td>"; print "<td><form name='frm' action='$PHP_SELF' method='POST'>\n"; print "<input type='hidden' name='user' value='$this->user'>\n"; print "<input type='submit' name='cancel' value='Cancel'>\n"; print "</form></td></tr></table>"; return true; } // Displays a table of all the games played by user with the latest game first. The // information displayed is: // Game number // Date of the game // Adjusted Gross Score // USGA Course Rating // USGA Slope Rating of the course // Handicap Differential of the game as calculated by this class // Course name function showall() { if($all=$this->getAll()){; print "<table border=1>"; print "<tr align='center'>"; print "<td>Game<br>Number</td>"; print "<td>Date</td>"; print "<td>Adjusted<br>Gross Score</td>"; print "<td>USGA Course<br>Rating</td>"; print "<td>USGA Slope<br>Rating</td>"; print "<td>Handicap<br>Differential</td>"; print "<td>Course Name</td>"; print "<td>Edit</td>"; print "<td>Delete</td>"; print "</tr>"; $n=count($all)-1; for($i=$n;$i>=0;$i--){ $id=$all[$i]['id']; $j=$i+1; print "<tr align='center'>"; print "<form method='POST' action='$PHP_SELF'>\n"; print "<input type='hidden' name='user' value='$this->user'>\n"; print "<input type='hidden' name='id' value='$id'>\n"; print "<td>$j</td>"; print "<td>".$all[$i]['date']."</td>"; print "<td>".$all[$i]['score']."</td>"; print "<td>".$all[$i]['crating']."</td>"; print "<td>".$all[$i]['srating']."</td>"; print "<td>".$all[$i]['hcapdiff']."</td>"; print "<td>".$all[$i]['course']."</td>"; print "<td><input type='submit' name='edit' value='Edit'></td>\n"; print "<td><input type='submit' name='delete' value='Delete'></td>\n"; print "</form>"; print "</tr>"; } print "</table>"; } } // Retrieves the information for each game entered for 'user' in the database // and returns it in an multidimensional array. // // Data for the first game: // array[0]['id'] id number of record in database // array[0]['date'] date game was played // array[0]['score'] adjusted gross score of game // array[0]['crating'] course rating // array[0]['srating'] slope rating // array[0]['hcapdiff'] handicap differential // array[0]['course'] course name // Data for the second game: // array[1]['id'] // array[1]['date'] // array[1]['score'] // array[1]['crating'] // array[1]['srating'] // array[1]['hcapdiff'] // array[1]['course'] // // etc // function getAll() { $queryGetPages = "SELECT * FROM `golfhcapu` WHERE `user`='$this->user' ORDER BY `date`"; $resultGetPages = mysql_db_query($this->dbase, $queryGetPages) or die ("Query failed: error was ".mysql_error()); $n=mysql_num_rows($resultGetPages); if($n > 0){ $i=0; while ($row = mysql_fetch_array($resultGetPages)){ $this->all[$i]['id']=$row['id']; $this->all[$i]['date']=$row['date']; $this->all[$i]['score']=$row['score']; $this->all[$i]['crating']=$row['crating']; $this->all[$i]['srating']=$row['srating']; $this->all[$i]['hcapdiff']=$row['hcapdiff']; $this->all[$i]['course']=$row['course']; $i=$i+1; } return $this->all; }else{ return false; } } // Reads the database and retrieves up to the last 20 games played. // Determines how many of these games to use, and which ones, to calculate // the handicap index. // Using the chosen games, calculates the handicap index and returns it. // If fewer than five games have been played, returns 0. // A minimum of five games must have been played to determine the handicap index. // function gethcap() { $queryGetGames = "SELECT * FROM `golfhcapu` WHERE `user`='$this->user' ORDER BY `date` DESC LIMIT 20"; $resultGetGames = mysql_db_query($this->dbase, $queryGetGames) or die ("Query failed: error was ".mysql_error()); $nr=mysql_num_rows($resultGetGames); if($nr > 4){ $tot=0; $n=$this->use[$nr]; for($i=0;$i<$nr;$i++){ $row = mysql_fetch_array($resultGetGames); $hcd[$i]=$row['hcapdiff']; } sort($hcd); for($i=0;$i<$n;$i++){ $tot=$tot+$hcd[$i]; } $hcap=floor(10*(($tot/$n)*.96))/10; return $hcap; }else{ return 0; } } function deleteGame($id) { $sql = "DELETE FROM golfhcapu WHERE id=$id"; $result = mysql_db_query($this->dbase, $sql) or die ("Delete failed: error was ".mysql_error()); return; } } // End of class ?> And here are the resources.. Demo of script as is.. http://www.gaclarke.com/agolfhandicap/handicapu.php and where the script came from.. http://phpclasses.promoxy.com/browse/package/2546.html And here's my golf site I'm trying to integrate this with... http://www.ncgolfers.com Thank you very much, Wayne Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 In reality everything should be the same, with one exception. You just need to include the way vanilla checks for an authenticated user and use the cookies instead of POST (or session however vanilla works) Should only take 1-2 hours tops. But yea, just include the authentication check into this page and change the $_POST['user'] to either the $_COOKIE['user'] or $_SESSION['user']. You should be fine. Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 11, 2007 Author Share Posted July 11, 2007 In reality everything should be the same, with one exception. You just need to include the way vanilla checks for an authenticated user and use the cookies instead of POST (or session however vanilla works) Should only take 1-2 hours tops. But yea, just include the authentication check into this page and change the $_POST['user'] to either the $_COOKIE['user'] or $_SESSION['user']. You should be fine. Ok, ??? so what about the SQL file , Do i still use that and leave out a couple of things, or should i insert the fields where my user data is stored with Vanilla? thanks, wayne Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 Just leave it, nothing wrong with it. Although to make 3nf you might want to change "user" to "userid" and store the users id there. No need to combine that data with the users table and make it more confusing keep it a separate table. Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 11, 2007 Author Share Posted July 11, 2007 ok Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 11, 2007 Author Share Posted July 11, 2007 sorry, i was just re-reading your last post.. What does "3nf" mean ??? wayne Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 Its used for database lingo, 3rd normal form. http://en.wikipedia.org/wiki/Third_normal_form Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 11, 2007 Author Share Posted July 11, 2007 ah ic wayne Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 11, 2007 Author Share Posted July 11, 2007 O.k., I think I've figured out how to check for user sessions outside vanilla Here's the link that describes it http://lussumo.com/community/?CommentID=56154 (lussumocookieone outside of vanilla. # 4) So I'm thinking I take that code from there and add it to the top of the php script i'm trying to install. would this be correct? Also I guess I would then take your suggestion a use $_SESSION['user'] as a replacement of $_POST['user']??? Thanks, wayne ps. bare with me, I'm learning as I go, "all new to this" and this is my first website I've ever built. Quote Link to comment Share on other sites More sharing options...
per1os Posted July 11, 2007 Share Posted July 11, 2007 You are on the right track as it seems to me. Give it a try. Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 21, 2007 Author Share Posted July 21, 2007 I can't seem to figure out how to set this up correctly, I have got it so my site checks for the user session though, but i'm stuck after that... need a push in the right direction.. ??? :-\ maybe someone could explain how these two scripts fit together first i dunno i'm completely lost lol... Im also very unsure how i pull the session ID and apply it to the user so the script always knows who's data it is and who's data it has to store it too. Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 21, 2007 Author Share Posted July 21, 2007 These are the errors i'm getting after i changed the Postuser to session user as suggested.. Notice: Undefined index: user in /home/11170/domains/ncgolfers.com/html/north-carolina/agolfhandicap.php on line 19 Notice: Undefined index: delete in /home/11170/domains/ncgolfers.com/html/north-carolina/agolfhandicap.php on line 23 Notice: Undefined index: edit in /home/11170/domains/ncgolfers.com/html/north-carolina/agolfhandicap.php on line 27 Notice: Undefined variable: PHP_SELF in /home/11170/domains/ncgolfers.com/html/north-carolina/agolfhandicap.php on line 32 Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 21, 2007 Author Share Posted July 21, 2007 I've included this code in all of the pages of my site that are outside of vanilla to check the user session and to state whether their logged in or not, and display if they are, and show a login/register button if they're not... Here's that code, cause it might help out with my problem.. <?php include("north-carolina/community/appg/settings.php"); //these are the files vanilla uses for sessions etc.. include("north-carolina/community/appg/init_people.php"); //these are the files vanilla uses for sessions etc.. if ($Context->Session->UserID) { echo "Signed in as <b>{$Context->Session->User->Name}</b>."; echo ' <a href="http://www.ncgolfers.com/north-carolina/community/people.php?PostBackAction=SignOutNow">'.('[ Sign Out ]').'</a>'; } else { echo "Welcome, Guest."; echo ' <a href="http://www.ncgolfers.com/north-carolina/community/people.php?ReturnUrl=http://www.ncgolfers.com/north-carolina/community/">'.('[ Login').'</a>'; echo " or"; echo ' <a href="http://www.ncgolfers.com/north-carolina/community/people.php?PostBackAction=ApplyForm">'.('Register ]').'</a>'; } ?> Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 21, 2007 Author Share Posted July 21, 2007 bump Quote Link to comment Share on other sites More sharing options...
waynem801 Posted July 22, 2007 Author Share Posted July 22, 2007 Anyone??? Quote Link to comment 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.