Location: PHPKode > projects > GolfstatsPHP > golfstatsphp_3.0/tourneycount.php
<?
include("dbasecon.inc.php");
include("session.inc.php");
include ("header.php");
include ("statscoursefunctions.inc.php");

//some variables, we could pass from a form if I get down to it, later we might be adding in rank points

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<? showheader("Counting Tournament::$tourneyname"); ?>

<?

if ($HTTP_POST_VARS['tourneyid']){
//first is 96,72,48,24

$tourneyid=$HTTP_POST_VARS['tourneyid'];
$money1=$HTTP_POST_VARS['money1'];
$money2=$HTTP_POST_VARS['money2'];
$money3=$HTTP_POST_VARS['money3'];
$money4=$HTTP_POST_VARS['money4'];
$money5=$HTTP_POST_VARS['money5'];


$holecounter=0;

$usersql="select userid as tourney_userid from games where gameid='$tourneyid' and tournament=1";
$userquery=mysql_query($usersql);
$userrow=mysql_fetch_array($userquery);
$userexist=mysql_numrows($userquery); //check to see if actually exist

$tourneysql="select *, date_format(date,'%d/%m/%Y') as real_date from tourney where tourneyid='$tourneyid'";
				$tourneyquery=mysql_query($tourneysql);
				$tourneyrow=mysql_fetch_array($tourneyquery);
				$tourneyexist=mysql_numrows($tourneyquery);
if ($tourneyexist) {
extract($tourneyrow);
} else {
echo "Application error.";
exit();
}
		
		//now get the tourney venue details
$coursechecksql="select * from courses where courseid='$tourneyvenue'";
				$coursecheckquery=mysql_query($coursechecksql);
				$coursecheckrow=mysql_fetch_array($coursecheckquery);
				$coursecheckexist=mysql_numrows($coursecheckquery);

if ($coursecheckexist) {				
extract($coursecheckrow);
} else {
echo "Application error.";
exit();
}



//get the leaderboard!
//now get the leaderion details according to our club, tied at stableford will go via lower handicap
$leadersql="select *, userid as leader_id from games where gameid='$tourneyid' and tournament=1 
						order by stableford DESC, handicap ASC, net_score ASC, gross_score ASC";
				$leaderquery=mysql_query($leadersql);
				$leaderrow=mysql_fetch_array($leaderquery);
				$leaderexist=mysql_numrows($leaderquery);		
		
		
				
?>


<?
//here's where we count the tourney stats!

//first check if there are already tourney stat, if so, just extract and don't calculate
$tourneystatsql="select * from tourney_stat where tourneyid='$tourneyid'";
				$tourneystatquery=mysql_query($tourneystatsql);
				$tourneystatrow=mysql_fetch_array($tourneystatquery);
				$tourneystatexist=mysql_numrows($tourneystatquery);
if ($tourneystatexist) {

echo "<p>Already have an existing tournament.<p>";

} else {}

##################START OF CALCULATION: Basic##################################################

######################SCORES##########################################
//1.get top_gross_score
$statsql="select min(gross_score) as top_gross from games where gameid='$tourneyid' and tournament=1";
//$statsql="select gross_score as top_gross from games where gameid='$tourneyid' order by top_gross ASC limit 1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$statexist=mysql_numrows($statquery);
$top_gross=$statrow["top_gross"];

$statsql="select userid as top_gross_score_users from games where gameid='$tourneyid' and gross_score='$top_gross' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_gross_user="$top_gross_user"."(".$statrow["top_gross_score_users"].")";
} while ($statrow=mysql_fetch_array($statquery));
//1.final product
$top_gross_score="$top_gross"."&nbsp;"."$top_gross_user";



//2.get top_net_score
$statsql="select min(net_score) as top_net from games where gameid='$tourneyid' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$top_net=$statrow["top_net"];

$statsql="select userid as top_net_score_users from games where gameid='$tourneyid' and net_score='$top_net' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_net_user="$top_net_user"."(".$statrow["top_net_score_users"].")";
} while ($statrow=mysql_fetch_array($statquery));
//2.final product
$top_net_score="$top_net"."&nbsp;"."$top_net_user";



//3.get top_gir, we need to convert these char types to integer with cast (only for mysql4 and above)
$statsql="select cast(gir as signed) as top_gir from games where gameid='$tourneyid' and tournament=1 and gir<>0 order by top_gir DESC limit 1";
$statquery=mysql_query($statsql);
$statexist=mysql_num_rows($statquery); //check to see if eagles gir exist
$statrow=mysql_fetch_array($statquery);

//echo $statsql;
if ($statexist) {
$top_gir=$statrow["top_gir"];

$statsql="select userid as top_gir_users from games where gameid='$tourneyid' and gir='$top_gir' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_gir_user="$top_gir_user"."(".$statrow["top_gir_users"].")";
} while ($statrow=mysql_fetch_array($statquery));

//3.final product
//get percentageround(1.95583, 2)
$top_gir2=round(($top_gir/18)*100,2);
$top_gir="$top_gir2"."%&nbsp;"."$top_gir_user";


} //endif statexist for gir




//3.get top_sandsave, we need to convert these char types to integer with cast
$statsql="select cast(sandsave as signed) as top_sandsave from games where gameid='$tourneyid' and sandsave<>0 and tournament=1 order by top_sandsave DESC limit 1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$statexist=mysql_num_rows($statquery); //check to see if eagles gir exist
//echo $statsql;
if ($statexist) {
$top_sandsave=$statrow["top_sandsave"];

$statsql="select userid as top_sandsave_users from games where gameid='$tourneyid' and sandsave='$top_sandsave' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_sandsave_user="$top_sandsave_user"."(".$statrow["top_sandsave_users"].")";
} while ($statrow=mysql_fetch_array($statquery));


} //endif statexist for sandsave

//4.get top_drive

$statsql="select longest_drive as top_drive from games where gameid='$tourneyid' and longest_drive<>'na' and tournament=1 order by top_drive DESC limit 1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$statexist=mysql_numrows($statquery); //check to see if actually exist

if ($statexist) {
$top_drive=$statrow["top_drive"];
$statsql="select userid as top_drive_users from games where gameid='$tourneyid' and longest_drive='$top_drive' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_drive_user="$top_drive_user"."(".$statrow["top_drive_users"].")";
} while ($statrow=mysql_fetch_array($statquery));

//4.final product
$top_drive="$top_drive".$measureshort."&nbsp;"."$top_drive_user";

} //endif statexist for drive

//5.get top_fwhits

$statsql="select cast(fairway_hit as signed) as top_fwhit from games where gameid='$tourneyid' and fairway_hit<>'0' and fairway_hit<>'na' and tournament=1 order by top_fwhit DESC limit 1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$statexist=mysql_numrows($statquery); //check to see if actually exist

if ($statexist) {
$top_fwhit=$statrow["top_fwhit"];
$statsql="select userid as top_fwhit_users from games where gameid='$tourneyid' and fairway_hit='$top_fwhit' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_fwhit_user="$top_fwhit_user"."(".$statrow["top_fwhit_users"].")";
} while ($statrow=mysql_fetch_array($statquery));

//5.final product
//get the percentage. If the course is unconventional, i.e more or less than 4 par3s, well, too bad. we divide by 14.
$top_fwhit2=round(($top_fwhit/14)*100,2);
$top_fwhit="$top_fwhit2"."%&nbsp;"."$top_fwhit_user";

} //endif statexist for drive


//6.get top_putts

$statsql="select total_putts as top_putts from games where gameid='$tourneyid' and total_putts<>'na' and tournament=1 order by top_putts ASC limit 1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);
$statexist=mysql_numrows($statquery); //check to see if actually exist

if ($statexist) {
$top_putts=$statrow["top_putts"];
$statsql="select userid as top_putts_users from games where gameid='$tourneyid' and total_putts='$top_putts' and tournament=1";
$statquery=mysql_query($statsql);
$statrow=mysql_fetch_array($statquery);

do {
extract($statrow);
$top_putts_user="$top_putts_user"."(".$statrow["top_putts_users"].")";
} while ($statrow=mysql_fetch_array($statquery));

//6.final product
$top_putts="$top_putts"."&nbsp;"."$top_putts_user";

} //endif statexist for putts


####################Advanced Calculation ###############################
//this is tough because we basically have to count who has the most eagles for the tourney.
//no worries, we'll have to figure out how to loop it. First, get the users involved in this tourney
//loop through each user, and loop trhough each hole that this user went through in this tourney
//record:
/*
1) Most eagles: top_gross_eagles. Each loop will have top_gross_eagles. if total_gross_eagles>top_gross_eagles of previous, 
then it becomes the new one. If same, then total_gross_eagles is same, but gross_eagles_user will be gross_eagles_user.
(new user).

This is for most eagles gross and net, and same for birdies and pars.

2) Total is easier. Each loop will record total_gross_eagles=total_gross_eagles+total_gross_eagles. Same for all.

3) Average is also tough. For each loop, two variables, par5hole (no of par5s) and par5total, and then par5total/par5hole.
Also the same for par4 and par3.
*/


//1st MAIN loop, get users involved in this tourney
$usersql="select userid as tourney_userid from games where gameid='$tourneyid' and tournament=1";
$userquery=mysql_query($usersql);
$userrow=mysql_fetch_array($userquery);
$userexist=mysql_numrows($userquery); //check to see if actually exist



if ($userexist) {

do {

//obviously first we need to reinit all vars
//NOTE: hole in one in a par 3 is an eagle!
$top_gross_eagles_user=0;
$top_net_eagles_user=0;
$top_gross_birdies_user=0;
$top_net_birdies_user=0;
$top_gross_pars_user=0;
$top_net_pars_user=0;
$top_gross_bogeys_user=0;
$top_net_bogeys_user=0;
$top_gross_others_user=0;
$top_net_others_user=0;

extract($userrow);

//2nd main loop, loop through each hole for each user
$hole=0;
				do {
				$hole++;
				$holename="hole".$hole;
				$holesql="select par, gross,gross_name,net,net_name from $holename where gameid='$tourneyid'
								  and userid='$tourney_userid' ";

									
									
				##########GROSS Hole in one#########################
				
				$holesql2=$holesql." And gross_name='Hole in one' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$holepar=$holerow["par"];
				//hole in one for par3 is an eagle!
				if ($holepar=3) {
				//for top score
				$top_gross_eagles_user=$top_gross_eagles_user+$holeexist;
				
				//for total score
				$total_gross_eagles=$total_gross_eagles+$holeexist;				

				}
				} //endif holeexist
				##########NET Hole in One#########################

				$holesql2=$holesql." And net_name='Hole in one' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$holepar=$holerow["par"];
				//hole in one for par3 is an eagle!
				if ($holepar=3) {
				$top_net_eagles_user=$top_net_eagles_user+$holeexist;
				//for total score
				$total_net_eagles=$total_net_eagles+$holeexist;				
				}
				} //endif holeexist
									

				##########GROSS EagleS#########################
				
				$holesql2=$holesql." And gross_name='Eagle' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$holepar=$holerow["par"];
				//hole in one for par3 is an eagle!
				if ($holepar=3) {
  			$holesql2=$holesql." And gross_name='Eagle' or gross_name='Hole in one' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				}
				//for top score
				$top_gross_eagles_user=$top_gross_eagles_user+$holeexist;
				
				//for total score
				$total_gross_eagles=$total_gross_eagles+$holeexist;				
				} //endif holeexist
				##########NET EagleS#########################

				$holesql2=$holesql." And net_name='Eagle' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_net_eagles_user=$top_net_eagles_user+$holeexist;
				//for total score
				$total_net_eagles=$total_net_eagles+$holeexist;				

				} //endif holeexist
				##########GROSS BirdieS#########################
				$holesql2=$holesql." And gross_name='Birdie' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_gross_birdies_user=$top_gross_birdies_user+$holeexist;
				$total_gross_birdies=$total_gross_birdies+$holeexist;				

				} //endif holeexist
				##########NET BirdieS#########################
				$holesql2=$holesql." And net_name='Birdie' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_net_birdies_user=$top_net_birdies_user+$holeexist;
				$total_net_birdies=$total_net_birdies+$holeexist;				

				}


				##########GROSS PARS#########################
				$holesql2=$holesql." And gross_name='Par' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_gross_pars_user=$top_gross_pars_user+$holeexist;
				$total_gross_pars=$total_gross_pars+$holeexist;				

				}
//				echo "$tourney_userid par on $holename:$top_gross_pars_user<p>";

				##########NET PARS#########################
				$holesql2=$holesql." And net_name='Par' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_net_pars_user=$top_net_pars_user+$holeexist;
				$total_net_pars=$total_net_pars+$holeexist;				

				}
				##########GROSS BogeyS#########################
				$holesql2=$holesql." And gross_name='Bogey' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_gross_bogeys_user=$top_gross_bogeys_user+$holeexist;
				$total_gross_bogeys=$total_gross_bogeys+$holeexist;				

//				echo "$tourney_userid Bogey on $holename:$top_gross_Bogeys_user<p>";
					}
				##########NET BogeyS#########################
				$holesql2=$holesql." And net_name='Bogey' ";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$top_net_bogeys_user=$top_net_bogeys_user+$holeexist;
				$total_net_bogeys=$total_net_bogeys+$holeexist;				
				}


				##########GROSS otherS#########################
				$holesql2=$holesql." And (gross_name='others' or gross_name='Triple Bogey' or gross_name='Double Bogey')";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$total_gross_others=$total_gross_others+$holeexist;				

//				echo "$tourney_userid other on $holename:$top_gross_others_user<p>";
					}
				##########NET otherS#########################
				$holesql2=$holesql." And (net_name='others' or net_name='Triple Bogey' or net_name='Double Bogey')";
				$holequery=mysql_query($holesql2);
				$holerow=mysql_fetch_array($holequery);
				$holeexist=mysql_numrows($holequery); //check to see if actually exist
				if ($holeexist) {
				$total_net_others=$total_net_others+$holeexist;				
				}

				
###############Counting Par3 Averages###################
$holesql="select par, gross,gross_name,net,net_name from $holename where gameid='$tourneyid'
					and userid='$tourney_userid' ";
	$holequery=mysql_query($holesql);
	$holerow=mysql_fetch_array($holequery);

$holepar=$holerow["par"];
$gross=$holerow["gross"];
$net=$holerow["net"];

if ($holepar==3) {
$par3total_gross=$par3total_gross+$gross;
$par3total_net=$par3total_net+$net;
} elseif ($holepar==4) {
$par4total_gross=$par4total_gross+$gross;
$par4total_net=$par4total_net+$net;

} else {
$par5total_gross=$par5total_gross+$gross;
$par5total_net=$par5total_net+$net;

}

//count how many par3s,par4s and par5s once!If counter is set to 1 then forget this loop				
if ($holecounter==0) {
if ($holepar==3) {
$par3hole=$par3hole+1;
} elseif ($holepar==4) {
$par4hole=$par4hole+1;
} else {
$par5hole=$par5hole+1;
} //endif holepars
}//endif holecounter				

				} while ($hole<18);
$holecounter=1;
	
//Some cleanup code		

####################GROSS eagleS######################		
if ($top_gross_eagles_user<>0) {
if ($top_gross_eagles_user > $top_gross_eagles) {
$top_gross_eagles=$top_gross_eagles_user;
$top_gross_eagles_userid="("."$tourney_userid".")";
} elseif ($top_gross_eagles_user == $top_gross_eagles) {
$top_gross_eagles_userid=$top_gross_eagles_userid."("."$tourney_userid".")";
} //endif topgrosseaglesuser
} else { //$top_gross_eagles='NA';
}
####################net eagleS######################		
if ($top_net_eagles_user<>0) {
if ($top_net_eagles_user > $top_net_eagles) {
$top_net_eagles=$top_net_eagles_user;
$top_net_eagles_userid="("."$tourney_userid".")";
} elseif ($top_net_eagles_user == $top_net_eagles) {
$top_net_eagles_userid=$top_net_eagles_userid."("."$tourney_userid".")";
} //endif topneteaglesuser
} else {}

####################GROSS birdieS######################		
if ($top_gross_birdies_user<>0){
if ($top_gross_birdies_user > $top_gross_birdies) {
$top_gross_birdies=$top_gross_birdies_user;
$top_gross_birdies_userid="("."$tourney_userid".")";
} elseif ($top_gross_birdies_user == $top_gross_birdies) {
$top_gross_birdies_userid=$top_gross_birdies_userid."("."$tourney_userid".")";
}//endif topgrossbirdiesuser
} else { 
//$top_gross_birdies='0';
}

####################net birdieS######################
if ($top_net_birdies_user<>0) {
if ($top_net_birdies_user > $top_net_birdies) {
$top_net_birdies=$top_net_birdies_user;
$top_net_birdies_userid="("."$tourney_userid".")";
} elseif ($top_net_birdies_user == $top_net_birdies) {
$top_net_birdies_userid=$top_net_birdies_userid."("."$tourney_userid".")";
} //endif topnetbirdiesuser
} else {}

####################GROSS PARS######################		
if ($top_gross_pars_user<>0) {
if ($top_gross_pars_user > $top_gross_pars) {
$top_gross_pars=$top_gross_pars_user;
$top_gross_pars_userid="("."$tourney_userid".")";
} elseif ($top_gross_pars_user == $top_gross_pars) {
$top_gross_pars_userid=$top_gross_pars_userid."("."$tourney_userid".")";
} //endif topgrossparsuser
} else {}

####################net PARS######################		
if ($top_net_pars_user<>0) {
if ($top_net_pars_user > $top_net_pars) {
$top_net_pars=$top_net_pars_user;
$top_net_pars_userid="("."$tourney_userid".")";
} elseif ($top_net_pars_user == $top_net_pars) {
$top_net_pars_userid=$top_net_pars_userid."("."$tourney_userid".")";
} //endif topnetparsuser
} else {}


####################GROSS bogeys######################		
if ($top_gross_bogeys_user<>0) {
if ($top_gross_bogeys_user > $top_gross_bogeys) {
$top_gross_bogeys=$top_gross_bogeys_user;
$top_gross_bogeys_userid="("."$tourney_userid".")";
} elseif ($top_gross_bogeys_user == $top_gross_bogeys) {
$top_gross_bogeys_userid=$top_gross_bogeys_userid."("."$tourney_userid".")";
} //endif topgrossbogeysuser
} else {}

####################net bogeys######################		
if ($top_net_bogeys_user<>0) {
if ($top_net_bogeys_user > $top_net_bogeys) {
$top_net_bogeys=$top_net_bogeys_user;
$top_net_bogeys_userid="("."$tourney_userid".")";
} elseif ($top_net_bogeys_user == $top_net_bogeys) {
$top_net_bogeys_userid=$top_net_bogeys_userid."("."$tourney_userid".")";
} //endif topnetbogeysuser
} else {}




} while ($userrow=mysql_fetch_array($userquery));

//final calculation of totalholes played for the tourney by all users
$par3hole=$par3hole*$userexist;
$par4hole=$par4hole*$userexist;
$par5hole=$par5hole*$userexist;

$par3avg_gross=$par3total_gross/$par3hole;
$par3avg_net=$par3total_net/$par3hole;
$par4avg_gross=$par4total_gross/$par4hole;
$par4avg_net=$par4total_net/$par4hole;
$par5avg_gross=$par5total_gross/$par5hole;
$par5avg_net=$par5total_net/$par5hole;


} else {
echo "No users in this tourney?";
exit();
}




if (!$top_gross_score) { $top_gross_score = "NA"; }
if (!$top_net_score) { $top_net_score = "NA"; }
if (!$top_gir) { $top_gir = "NA"; }
if (!$top_sandsave) { $top_sandsave = "NA"; }
if (!$top_gross_eagles) { $top_gross_eagles = "NA"; }
if (!$top_net_eagles) { $top_net_eagles = "NA"; }
if (!$top_gross_birdies) { $top_gross_birdies = "NA"; }
if (!$top_net_birdies) { $top_net_birdies = "NA"; }
if (!$top_gross_pars) { $top_gross_pars = "NA"; }
if (!$top_net_pars) { $top_net_pars = "NA"; }
if (!$top_drive) { $top_drive = "NA"; }
if (!$top_fwhit) { $top_fwhit = "NA"; }
if (!$top_putts) { $top_putts = "NA"; }


if (!$total_gross_eagles) { $total_gross_eagles = "NA"; }
if (!$total_net_eagles) { $total_net_eagles = "NA"; }
if (!$total_gross_birdies) { $total_gross_birdies = "NA"; }
if (!$total_net_birdies) { $total_net_birdies = "NA"; }
if (!$total_gross_pars) { $total_gross_pars = "NA"; }
if (!$total_net_pars) { $total_net_pars = "NA"; }


//echo $top_gir;
//echo $top_fwhit;
//actually, lets check
//ok, time to chuck everything into the table. First test if there's existing tourney stats first

$tourneystatchecksql="select * from tourney_stat where tourneyid='$tourneyid'";
				$tourneystatcheckquery=mysql_query($tourneystatchecksql);
				$tourneystatcheckrow=mysql_fetch_array($tourneystatcheckquery);
				$tourneystatcheckexist=mysql_numrows($tourneystatcheckquery);

if ($tourneystatcheckexist) {
$tourneystatadd="UPDATE tourney_stat 
SET top_gross_score= '$top_gross_score', top_net_score= '$top_net_score', top_gir= '$top_gir', top_sandsave= '$top_sandsave', top_gross_eagles= '$top_gross_eagles $top_gross_eagles_userid',
top_net_eagles= '$top_net_eagles $top_net_eagles_userid', top_gross_birdies= '$top_gross_birdies $top_gross_birdies_userid', 
top_net_birdies= '$top_net_birdies $top_net_birdies_userid', top_gross_pars= '$top_gross_pars $top_gross_pars_userid',
top_net_pars= '$top_net_pars $top_net_pars_userid', top_gross_bogeys= '$top_gross_bogeys $top_gross_bogeys_userid', 
top_net_bogeys= '$top_net_bogeys $top_net_bogeys_userid', top_drive= '$top_drive', top_fairway_hit= '$top_fwhit', 
top_putts= '$top_putts', total_gross_eagles= '$total_gross_eagles', total_net_eagles= '$total_net_eagles', 
total_gross_birdies= '$total_gross_birdies', total_net_birdies= '$total_net_birdies', total_gross_pars= '$total_gross_pars', 
total_net_pars= '$total_net_pars', total_gross_bogeys= '$total_gross_bogeys', total_net_bogeys= '$total_net_bogeys', 
total_gross_others= '$total_gross_others', total_net_others= '$total_net_others', par3avg_gross='$par3avg_gross',
par3avg_net='$par3avg_net', par4avg_gross='$par4avg_gross', par4avg_net='$par4avg_net', par5avg_gross='$par5avg_gross',
par5avg_net='$par5avg_net' WHERE tourneyid='$tourneyid'";


} else {	

$tourneystatadd="INSERT INTO tourney_stat
(`tourneyid`, `top_gross_score`, `top_net_score`, `top_gir`, `top_sandsave`,`top_gross_eagles`, `top_net_eagles`, `top_gross_birdies`, `top_net_birdies`, `top_gross_pars`, 
`top_net_pars`, `top_gross_bogeys`, `top_net_bogeys`, `top_drive`, `top_fairway_hit`, `top_putts`,
 `total_gross_eagles`, `total_net_eagles`, `total_gross_birdies`, `total_net_birdies`, `total_gross_pars`, 
 `total_net_pars`, `total_gross_bogeys`, `total_net_bogeys`, `total_gross_others`, `total_net_others`,
  `par3avg_gross`, `par3avg_net`, `par4avg_gross`, `par4avg_net`, `par5avg_gross`, `par5avg_net`)
VALUES
('$tourneyid', '$top_gross_score', '$top_net_score', '$top_gir', '$top_sandsave', '$top_gross_eagles $top_gross_eagles_userid',
 '$top_net_eagles $top_net_eagles_userid', '$top_gross_birdies $top_gross_birdies_userid', '$top_net_birdies $top_net_birdies_userid', 
'$top_gross_pars $top_gross_pars_userid', '$top_net_pars $top_net_pars_userid', '$top_gross_bogeys $top_gross_bogeys_userid', 
'$top_net_bogeys $top_net_bogeys_userid','$top_drive', '$top_fwhit','$top_putts','$total_gross_eagles','$total_net_eagles',
'$total_gross_birdies','$total_net_birdies','$total_gross_pars','$total_net_pars','$total_gross_bogeys','$total_net_bogeys'
,'$total_gross_others','$total_net_others','$par3avg_gross','$par3avg_net','$par4avg_gross','$par4avg_net','$par5avg_gross','$par5avg_net')";

} //endif tourneystatcheckexist

$tourneystataddquery=mysql_query($tourneystatadd);
//}//endif tourney stat exist



//ok, now report back what has been done
$tourneystatsql2="select * from tourney_stat where tourneyid='$tourneyid'";
				$tourneystatquery2=mysql_query($tourneystatsql2);
				$tourneystatrow2=mysql_fetch_array($tourneystatquery2);
				$tourneystatexist2=mysql_numrows($tourneystatquery2);

extract($tourneystatrow2);


?>
<table border=0 width=100%>
<tr class="bg0">
<td colspan="4">
<b>Tournament Statistics
</td>
</tr>
<tr class="bg1"><td>Category</td><td>Gross</td><td>Net</td></tr>
<tr><td class="bg2">Top Score</td><td><?echo "$top_gross_score";?></td><td><?echo "$top_net_score";?></td></tr>
<tr><td class="bg2">Most Eagles</td><td><?echo "$top_gross_eagles";?></td><td><?echo "$top_net_eagles";?></td></tr>
<tr><td class="bg2">Most Birdies</td><td><?echo "$top_gross_birdies";?></td><td><?echo "$top_net_birdies";?></td></tr>
<tr><td class="bg2">Most Pars</td><td><?echo "$top_gross_pars";?></td><td><?echo "$top_net_pars";?></td></tr>
<tr><td class="bg2">Most Bogeys</td><td><?echo "$top_gross_bogeys";?></td><td><?echo "$top_net_bogeys";?></td></tr>
<tr><td class="bg2">Total Eagles</td><td><?echo "$total_gross_eagles";?></td><td><?echo "$total_net_eagles";?></td></tr>
<tr><td class="bg2">Total Birdies</td><td><?echo "$total_gross_birdies";?></td><td><?echo "$total_net_birdies";?></td></tr>
<tr><td class="bg2">Total Pars</td><td><?echo "$total_gross_pars";?></td><td><?echo "$total_net_pars";?></td></tr>
<tr><td class="bg2">Total Bogeys</td><td><?echo "$total_gross_bogeys";?></td><td><?echo "$total_net_bogeys";?></td></tr>
<tr><td class="bg2">Total Others</td><td><?echo "$total_gross_others";?></td><td><?echo "$total_net_others";?></td></tr>
<tr><td class="bg2">Average Par 3 Scores</td><td><?echo "$par3avg_gross";?></td><td><?echo "$par3avg_net";?></td></tr>
<tr><td class="bg2">Average Par 4 Scores</td><td><?echo "$par4avg_gross";?></td><td><?echo "$par4avg_net";?></td></tr>
<tr><td class="bg2">Average Par 5 Scores</td><td><?echo "$par5avg_gross";?></td><td><?echo "$par5avg_net";?></td></tr>

<tr class="bg1"><td>Category</td><td colspan="2">Stat</td></tr>
<tr><td class="bg2">Longest Drive</td><td colspan=2><?echo "$top_drive";?></td></tr>
<tr><td class="bg2">Top GIR</td><td colspan=2><?echo "$top_gir";?></td></tr>
<tr><td class="bg2">Top Sand Save</td><td colspan=2><?echo "$top_sandsave";?></td></tr>
<tr><td class="bg2">Top Driving Accuracy</td><td colspan=2><?echo "$top_fairway_hit";?></td></tr>
<tr><td class="bg2">Top Putts per Round</td><td colspan=2><?echo "$top_putts";?></td></tr>


</table>


<table border=0 width=80%>
<tr class="bg0">
<td colspan="7">
<b>Full Leaderboard
</td>
</tr>
<tr class="bg2">
<td>&nbsp;</td><td>Player Name</td><td>Handicap</td><td>Gross Score</td><td>Net Score</td><td>Points</td><td>Money</td><td>Card</td>
</tr>
<?
$playerposition=0;
if ($leaderexist) {				

//ok, here's where we chuck in the placing for this particular tourney
do {
extract($leaderrow);
$playerposition++;		
//now get the player details
$leaderusersql="select name as leader_name, surname as leader_surname, userid as leaderboard_id from users_tour where userid='$leader_id'";
				$leaderuserquery=mysql_query($leaderusersql);
				$leaderuserrow=mysql_fetch_array($leaderuserquery);
				$leaderuserexist=mysql_numrows($leaderuserquery);


extract($leaderuserrow);
//ok, lets count to coursepar
$gross_par=$gross_score-$coursepar;
$net_par=$net_score-$coursepar;

if ($gross_par > 0) {
$gross_par="+"."$gross_par";
} elseif ($gross_par==0) {
$gross_par="E";
}

if ($net_par > 0) {
$net_par="+"."$net_par";
} elseif ($net_par==0) {
$net_par="E";
}

//now we update the ranking to reflect the added money and points (in the future)
			
//now stuff the money into the respective positions!
if ($playerposition==1) {
$playermoney=$money1;

$winnersql="Select tourneytype from tourney 
							where tourneyid='$tourneyid'";
				$winnerquery=mysql_query($winnersql);
				$winnerrow=mysql_fetch_array($winnerquery);

$tourneytype=$winnerrow['tourneytype'];

//potentially lots of if statements
if ($tourneytype=="System 36") {

$champ_score_type="stableford";
} elseif ($tourneytype=="Stroke Play (Gross)") {
$champ_score_type="gross_score";
} elseif ($tourneytype=="Stroke Play (Net)") {
$champ_score_type="net_score";
}//endif tourneytype





//this is a special section to put in the winner name and winner score in tourney table

//$winnersql="Select $champ_score_type from games 
	//						where gameid='$tourneyid' and userid='leaderboard_id'";
		//		$winnerquery=mysql_query($winnersql);
			//	$winnerrow=mysql_fetch_array($winnerquery);

				$champ_score=${$champ_score_type};
				
$winnersql="UPDATE tourney SET championid='$leaderboard_id', championscore='$champ_score' 
							where tourneyid='$tourneyid'";
				$winnerquery=mysql_query($winnersql);


				
} elseif ($playerposition==2) {
$playermoney=$money2;
} elseif ($playerposition==3) {
$playermoney=$money3;
} elseif ($playerposition==4) {
$playermoney=$money4;
} elseif ($playerposition==5) {
$playermoney=$money5;
} else {
$playermoney=0;
}//endif playerposition

echo "<tr class='bg4'><td>$playerposition</td><td><a href='players2.php?userid=$leaderboard_id&action=details&fullname=$leader_name $leader_surname'>$leader_name $leader_surname</a>
</td><td>$handicap</td><td>$gross_score [$gross_par]</td><td>$net_score [$net_par]</td><td>$stableford</td><td>$playermoney</td>
<td><a href='#' title=\"$tourneyid\" onClick=\"window.open('players3.php?userid=$leaderboard_id&gameid=$tourneyid','$tourneyid','left=100,top=0,height=730,width=620,scrollbars=yes');return false;\">[+]</a></td></tr>";


//if (!$tourneyexist) {

//ok, stuff the position into table games!
$placingsql="UPDATE games SET tourneyplace='$playerposition'
							where userid='$leaderboard_id' and gameid='$tourneyid' and tournament=1";
				$placingquery=mysql_query($placingsql);


$moneysql="UPDATE games SET money='$playermoney'
							where userid='$leaderboard_id' and gameid='$tourneyid' and tournament=1";
				$moneyquery=mysql_query($moneysql);

//get the total money for each player
$getmoneysql="select sum(money) as total_money from games where userid='$leaderboard_id'";								
				$getmoneyquery=mysql_query($getmoneysql);				
				$getmoneyrow=mysql_fetch_array($getmoneyquery);
				$getmoneyexist=mysql_numrows($getmoneyquery);

				
				$total_money=$getmoneyrow['total_money'];				

				//now update rank with the new total money

$putmoneysql="UPDATE ranking SET total_money='$total_money'
							where userid='$leaderboard_id'";
				$putmoneyquery=mysql_query($putmoneysql);

//				} //endif tourneyexist
//lets keep track of the total for each column so we can divide it later for average
//1.handicap
$total_handicap=$total_handicap+$handicap;
//2.gross
$total_gross_score=$total_gross_score+$gross_score;
$total_gross_par=$total_gross_par+$gross_par;

//2.net
$total_net_score=$total_net_score+$net_score;
$total_net_par=$total_net_par+$net_par;

//4.stableford
$total_stableford=$total_stableford+$stableford;

				
} while ($leaderrow=mysql_fetch_array($leaderquery));
//get the average

$avg_handicap=round($total_handicap/$userexist,2);
$avg_gross=round($total_gross_score/$userexist,2);
$avg_net=round($total_net_score/$userexist,2);
$avg_gross_par=round($total_gross_par/$userexist,2);
$avg_net_par=round($total_net_par/$userexist,2);
$avg_stableford=round($total_stableford/$userexist,2);

if ($avg_gross_par > 0) {
$avg_gross_par="+"."$avg_gross_par";
} elseif ($avg_gross_par==0) {
$avg_gross_par="E";
}

if ($avg_net_par > 0) {
$avg_net_par="+"."$avg_net_par";
} elseif ($avg_net_par==0) {
$avg_net_par="E";
}


echo "<tr class='bg4'><td colspan=2><b>Average Stats</td><td>$avg_handicap</td><td>$avg_gross [$avg_gross_par]</td><td>$avg_net [$avg_net_par]</td><td>$avg_stableford</td>
<td>&nbsp;</a></td>
</tr>";

} else {
echo "Application error.";
exit();
}

?>


</table>
<?
} else {


echo "<center>Illegal access to management system.</center>";
}//endif http post vars tourneyid



?>

<? showfooter(); ?>
Return current item: GolfstatsPHP