// include formhelpers.php in the resources directory
include '../resources/formhelpers.php';

$servername = "localhost";
$username = "goldc802_tom";
$password = "cheide10";
$dbname = "goldc802_golfersData";

// Create connection to server and database
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$ferror=array();

//open Places table to get date, day, place, time data
$sql = "SELECT * FROM Places" ;
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// define global arrays of dates, days, courses, time
$i=0;
while($row = mysqli_fetch_assoc($result)) {
$dates[$i]=$row["Date"];
$courses[$i]=$row["Place"];
$startTime[$i]=$row["Time"];
++$i;
}
} else {
$ferror[]= 'Error accessing the Places table.';
}

//open Names table to get player's info
$sql = "SELECT * FROM Names" ;
$result_player = mysqli_query($conn, $sql);

if (mysqli_num_rows($result_player) > 0) {
//define global array of players
$i=0;
while($row =mysqli_fetch_assoc($result_player)) {
$row_data[$i][0]=$row["ID"];
$row_data[$i][1]=$row["Name"];
$row_data[$i][2]=$row["Email"];
$row_data[$i][3]=$row["play_string"];
$i++;
}
} else {
$ferror[]= 'Error accessing the Names table.';
}

$conn->close();

//display any file errors
if ($ferror){
$ferror_text = '

This page will not save your changes. Notify webmaster that the following errors occurred:

';
$ferror_text .= '
  • ';
    $ferror_text .= implode('
  • ',$ferror);
    $ferror_text .= '
';
}else{
$ferror_text = '';
}
print $ferror_text;

//main page logic
// If the form is submitted, validate and then process or redisplay
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass then to show_forms()
if ($form_errors = validate_form()){
show_form($form_errors);
}else{
// the submitted form is valid so process it
process_form();
}
}else{
// The form wasn't submitted so display it
show_form();
}

function show_form($errors = ''){
// if errors were passed, put them in $error_text (with HTML markup)
if ($errors) {
$error_text = '

You need to correct the following errors:

';
$error_text .= '
  • ';
    $error_text .= implode('
  • ',$errors);
    $error_text .= '
';
}else{
$error_text = '';
}
//jump out of php mode to make displaying easier
?>





Select your name from the SELECT YOUR NAME drop down menu. Then click the GET SCHEDULE button to get the schedule you have previously entered (click this button even if it is your first time). Check the row under the dates in the date row that you are intending to play golf. Any inputs or changes must be made by 8 AM on Tuesday morning to be counted for that week. All changes after that time must be called in to the golf course.


If you have guests playing select Guest 1 and/or Guest 2 from the SELECT YOUR NAME drop down menu. After you click the GET SCHEDULE button make sure that someone else has not already scheduled guests for the week you intend to bring guests. If the intended week is clear then check the date you are bringing guest(s).








Get your existing schedule



$dummy= array();
do_table($dummy,"NO");
?>

All dates are Thursday. Check Golfing Notices for additional notices.



} //The end of show_form()

function validate_form(){
$errors = array();
//player selection is required
if ($_POST['player'] == 'none'){
$errors[] ='You must select your name.';
}
return $errors;
}


function process_form(){
//declare $row_data as global
global $row_data;

if ((array_key_exists('player', $_POST)) && (array_key_exists('action', $_POST))){
$name = strip_tags($_POST['player']);
$action = strip_tags($_POST['action']);
//get player's real name and row number in file
$i=0;
foreach ($row_data as $value){
$first=$value[0];
$second=$value[1];
//for existing action player is player ID
//for save action player is player name
if ($action=="existing"){
if ($first==$name){
$id=$first;
$realName=$second;
$row_num=$i;
}
}else{
if ($second==$name){
$id=$first;
$realName=$second;
$row_num=$i;
}
}
++$i;
}

//if action is existing send existing database dates back
//if action is save then save dates in post
if ($action == "existing"){
//jump out of php mode to make displaying easier
?>



Check the row under the dates in the date row that you are intending to play golf. Any inputs or changes must be made by 8 AM on Tuesday morning to be counted for that week. All changes after that time must be called in to the golf course.




This is the existing schedule for



do_table($row_data[$row_num]);
?>



Save the schedule entered above


}else{
//save post data
print "Saving dates for " . $_POST['player'] . ". If you don't see data with your name on the next line your changes were not saved.
";
// $new_line[0]=$id;
// $new_line[1]=$realName;
$count=count($GLOBALS['dates']);
for($i=0; $i< $count; $i++){
foreach($_POST['cb'] as $yes){
if ($yes==$i){
$bool="yes";
}
}
if ($bool=="yes"){
$new_line[$i]="1";
//reset flag
$bool="no";
}else{
$new_line[$i]="0";
}
}
$line = implode(",",$new_line);

$servername = "localhost";
$username = "goldc802_tom";
$password = "cheide10";
$dbname = "goldc802_golfersData";

// Create connection to server and database
$conn1 = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn1->connect_error) {
die("Connection failed: " . $conn1->connect_error);
}

$sql = "UPDATE Names SET play_string = '$line' WHERE Name= '$realName'";

if (mysqli_query($conn1, $sql)) {
echo "Here is the data saved for " . "$realName" . ": " . "$line";
} else {
echo "Error updating record: " . mysqli_error($conn1);
}

$conn1->close();

?>



Click here to restart process

}
}

} //end of process_form()

function do_table($row, $play_row="YES"){

print '';
print ' ';
foreach ($GLOBALS['dates'] as $value){
print " ";
}
print ' ';
foreach ($GLOBALS['courses'] as $value){
print " ";
}
print ' ';
print ' ';
foreach ($GLOBALS['startTime'] as $value){
print " ";
}
print ' ';
if($play_row=="YES"){
print ' ';
$plyr_string = explode(",",$row[3]);
$i=0;
foreach ($GLOBALS['dates'] as $value){
if (array_key_exists($i, $plyr_string)) {
if ($plyr_string[$i]=="1") {
print '';
}else{
print '';
}
}else{
print '';
}
++$i;
}
}
print ' ';
print '
$value
$value
$value
PlayPlayPlay
';
}

?>