2015-11-19 117 views
0

嗨,你能幫助我嗎?我正在開發一個高爾夫障礙網站。但我在PHP語言方面的知識並不多,我只是PHP的初學者。 我需要記錄每位高爾夫選手的所有比分,並計算20場比賽中的最佳10個比分來獲得差點評分。不是10個最低分,而是10個最高分。我一直在研究它一個月,但我無法得到高爾夫障礙的確切代碼和邏輯。請幫幫我。高爾夫障礙網站

<?php 
 
include "class.agolfhandicap.php"; 
 
include "conn.php"; 
 
$gh=new agolfhandicap(); 
 

 

 
// Get the user name if submitted. 
 
if(isset($_POST['user'])){ 
 
$user=$_POST['user']; 
 
$gh->setuser($user); 
 
} 
 
if(isset($_POST['delete'])){   //delete the record 
 
    $id=$_POST['id']; 
 
    $gh->deleteGame($id); 
 
} 
 
if(isset($_POST['edit'])){   //edit the record 
 
    $id=$_POST['id']; 
 
} 
 
// If no user yet, ask for it. 
 
if(!isset($_POST['user'])){ 
 
print "<form name='user' action='$_SERVER[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 
 
include "conn.php"; 
 
class agolfhandicap 
 
{ 
 
var $user;   // Username of golfer 
 
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. 
 

 
// 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(isset($_POST['submit'])){ 
 
// print "submit=".$_POST['submit']; 
 
// $id=$_POST['id']; 
 
$user=$_POST['user']; 
 
$this->user=$user; 
 
$date =$_POST['date1']; 
 
$date=date('M/d/Y'); 
 

 
$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_query($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_query($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=isset($_POST['date1']); 
 
$score=isset($_POST['score1']); 
 
$crat=isset($_POST['crat1']); 
 
$srat=isset($_POST['srat1']); 
 
$course=isset($_POST['course1']); 
 
}else{     //If editing get the values from the database 
 
$queryGetPages = "SELECT * FROM `golfhcapu` WHERE `id`='$id'"; 
 
$resultGetPages = mysql_query($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 "<style>table{border-collapse:collapse}</style>"; 
 
print "<table border=3 cellpadding=6 >"; 
 
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='$_SERVER[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='20' 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='$_SERVER[PHP_SELF]' method='POST'>\n"; 
 
print "<input type='hidden' name='user' value='$this->user'><br>"; 
 
print "<input type='submit' name='cancel' value='Cancel'>"; 
 
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 "<style>table{border-collapse:collapse}</style>"; 
 
print "<table border=3 cellpadding=6 >"; 
 
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='$_SERVER[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_query($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_query($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_query($sql) or die ("Delete failed: error was ".mysql_error()); 
 
    return; 
 
} 
 
} 
 
// End of class 
 
?>

+0

Stackoverflow不是一個社區,您可以只問人們爲您推薦禮儀代碼。請向我們展示一些您已經嘗試過的代碼,並確認您遇到的具體問題 – ThomasVdBerge

+1

目前尚不清楚導致問題的哪一部分。這聽起來像它可能只是一個簡單的數據庫查詢 - 按分數排序(降序)並獲得前10個結果。你有沒有嘗試過任何東西?發生了什麼?請在問題中包含您努力的成果。 –

+1

我會在這裏等待我的代碼。 –

回答

0

假設你存儲的遊戲分數數據庫中的所有你需要做的就是查詢分數和爲了通過分數降序查詢和結果限制爲10

+0

謝謝你給我的想法。非常感謝你! –

+0

僅供參考當我回答時,我沒有看到您在編輯時添加的代碼。 – surprisedbadger

+0

我會爲你給我的想法制作另一個代碼。再次感謝。 –