<?php

/*
DESCRIPTION: Displays the interface for viewing and modifying workout data.
NOTES: There are two parts to this file
First we pull the data from the database and display if there is any.
Second, when 'modify' is selected, we transfer the data into forms. (We 
do this in one file, since we only have to make one database call).
*/
require_once "db.mysql.php";
//Main MySQL access Class
require_once "functions.class.php";
//Common functions used in application
## Create new mysql access object ##
$showWorkout = new database_mysql();
$showWorkout->connect();
## SQL Statement
$sql = "SELECT * FROM `usrweighttbl`, `usrreptbl`, `usrsetstbl` , `usrexercisetbl` , `musclegrouptbl`" . " WHERE `usrwghtsetsid` = `usrsetsid` AND `usrwghtexerid` = `usrexerid` AND `usrexermusclegrp` = `musclegrpid` " . "AND `usrwghtdate` = \"2008-01-11\" AND `usrrepsessid` = `usrwghtsessid` ORDER BY MuscleGrpName LIMIT 0, 30";
## Execute Query ##
$showWorkout->query($sql);
## Number of recurds found in query
$num = $showWorkout->num_rows();
## If there are no records returned, redirect to the enter workout page
$myurl = $_SERVER['HTTP_HOST'];
if ($num == 0) {
    header("Location:http://{$myurl}/workout-tracker/showworkout.php?enter=1");
}
## Populate our arrays ##
$i = 0;
while ($row = $showWorkout->fetch_array()) {
    $UsrWghtID[$i] = $row['UsrWghtID'];
## ERROR CHECK: If we don't have a least one ID go back.
if (!$UsrWghtID[0]) {
    header("Location:http://{$myurl}/workout-tracker/showworkout.php?modify=1");
}
## Capture our actual set values for updating the database.
for ($i = 0; $i < $num; $i++) {
    for ($m = 0; $m < $UsrSetsQTY[$i]; $m++) {
        $UsrWghtSetEntry[$i][$m] = $_POST['UsrWghtSetEntry'][$i][$m];
        //echo "WGHT Value = ".$UsrWghtSetEntry[$i][$m]."<BR>";
    }
}
for ($i = 0; $i < $num; $i++) {
    for ($m = 0; $m < $UsrSetsQTY[$i]; $m++) {
        $UsrRepSetEntry[$i][$m] = $_POST['UsrRepSetEntry'][$i][$m];
        //echo "REP Value = ".$UsrRepSetEntry[$i][$m]."<BR>";
    }
}
## Update the record set to the database.
$updateWorkout = new database_mysql();
$updateWorkout->connect();
for ($i = 0; $i < $num; $i++) {
    for ($m = 0; $m < 1; $m++) {
        $sql = "UPDATE `wolf`.`usrweighttbl` SET `UsrWghtSet1` = " . $UsrWghtSetEntry[$i][0] . ", `UsrWghtSet2` =" . " '" . $UsrWghtSetEntry[$i][1] . "', `UsrWghtSet3` = '" . $UsrWghtSetEntry[$i][2] . "', `UsrWghtSet4` =" . " '" . $UsrWghtSetEntry[$i][3] . "', `UsrWghtSet5` = '" . $UsrWghtSetEntry[$i][4] . "', `UsrWghtSet5` =" . " '" . $UsrWghtSetEntry[$i][4] . "', `UsrWghtSet6` = '" . $UsrWghtSetEntry[$i][5] . "', `UsrWghtSet7` =" . " '" . $UsrWghtSetEntry[$i][6] . "', `UsrWghtSet8` = '" . $UsrWghtSetEntry[$i][7] . "', `UsrWghtSet9` =" . " '" . $UsrWghtSetEntry[$i][8] . "', `UsrWghtSet10` = '" . $UsrWghtSetEntry[$i][9] . "' WHERE `usrweighttbl`.`UsrWghtID` = " . $UsrWghtID[$i] . " LIMIT 1;";
        $updateWorkout->query($sql);
    }
    for ($m = 0; $m < 1; $m++) {
        $sql = "UPDATE `wolf`.`usrreptbl` SET `UsrRepSet1` = " . $UsrRepSetEntry[$i][0] . ", `UsrRepSet2` =" . " '" . $UsrRepSetEntry[$i][1] . "', `UsrRepSet3` = '" . $UsrRepSetEntry[$i][2] . "', `UsrRepSet4` =" . " '" . $UsrRepSetEntry[$i][3] . "', `UsrRepSet5` = '" . $UsrRepSetEntry[$i][4] . "', `UsrRepSet5` =" . " '" . $UsrRepSetEntry[$i][4] . "', `UsrRepSet6` = '" . $UsrRepSetEntry[$i][5] . "', `UsrRepSet7` =" . " '" . $UsrRepSetEntry[$i][6] . "', `UsrRepSet8` = '" . $UsrRepSetEntry[$i][7] . "', `UsrRepSet9` =" . " '" . $UsrRepSetEntry[$i][8] . "', `UsrRepSet10` = '" . $UsrRepSetEntry[$i][9] . "' WHERE `usrreptbl`.`UsrRepSessID` = " . $UsrWghtSessID[$i] . " LIMIT 1;";
        $updateWorkout->query($sql);
    }
}
header("Location:http://{$myurl}/workout-tracker/showworkout.php?modify=1");