Location: PHPKode > scripts > Weight Loss Tracker > weight-loss-tracker/index.php
<?php
include('functions/functions_general.php');
include('config.php');
if (!checkLogin($_SESSION['weight_loss_tracker_username'],$_SESSION['weight_loss_tracker_user_password'])) {
	header('Location:login-register.php');
}
$num_entries = 10;

// Clear foods
if ($_GET['action'] == 'clear_food') {
	$date = time();
	$date_url = date('Y-m-d',$date);
	if ($_GET['day'] != '') {
		$compare_date = $_GET['day'];
	} else {
		$compare_date = $date_url;
	}
	$date = date('Y-m-d H:i:s',strtotime($compare_date));
	$clear = mysql_query('
		DELETE FROM weight_loss_tracker_days
		WHERE days_date = "' . $date . '"');
	$result_div .= getResultDiv('The day has been cleared','success');
}

// Change BMR
if ($_POST['action'] == 'change_bmr') {
	$update = mysql_query('
		UPDATE weight_loss_tracker_users
		SET user_bmr = "' . removeInvalidChars($_POST['new_bmr'],'0123456789') . '"');
	$result_div .= getResultDiv('Your BMR has been updated','success');
}

// Get user info		
$result = mysql_query('
	SELECT *
	FROM weight_loss_tracker_users
	WHERE user_id  = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
$user = mysql_fetch_array($result);

// Add foods for the day
if ($_POST['action'] == 'add_foods') {
	$row_i = 1;
	if ($_GET['day'] != '') {
		$date = date('Y-m-d H:i:s',strtotime($_GET['day']));
	} else {
		$date = date('Y-m-d');
		$date = date('Y-m-d H:i:s',strtotime($date));
	}
	while ($row_i <= $num_entries) {
		if ($_POST['food_item_' . $row_i] != '') {
			if ($total < $weightlosstracker_settings["max_items"]) {
				$results .= $_POST['food_item_' . $row_i] . ',';
			}
		}
		$row_i++;
	}
	if ($results != '') {
		$result = mysql_query('
			SELECT *
			FROM weight_loss_tracker_days
			WHERE days_date = "' . $date . '" and
				user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
		if (mysql_num_rows($result) > 0) {
			$row = mysql_fetch_array($result);
			$results = $results . $row['days_tally'];
			$update = mysql_query('
				UPDATE weight_loss_tracker_days
				SET days_tally = "' . $results . '"
				WHERE days_date = "' . $date . '" and
					user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
		} else {
			$insert = mysql_query('
				INSERT INTO weight_loss_tracker_days
				SET days_tally = "' . $results . '",
					days_date = "' .$date . '",
					user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
					
		}
		$cal_array = explode(',',$results);
		$total_cal_array = count($cal_array);
		$cal_i = 0;
		while (($cal_i+1) <$total_cal_array) {
			$result = mysql_query('
				SELECT *
				FROM weight_loss_tracker_items
				WHERE items_id = "' . $cal_array[$cal_i] . '"');
			$row = mysql_fetch_array($result);
			$cal_count = $cal_count + $row['items_cal'];
			$cal_i++;
		}
		$update = mysql_query('
			UPDATE weight_loss_tracker_days
			SET days_total = "' . $cal_count . '"
			WHERE days_date = "' . $date . '" and
				user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
	}
	if (mysql_error() != '') {
		$result_div .= getResultDiv('The foods have been added','success');
	} else {
		$result_div .= getResultDiv(mysql_error());
	}
}

// Generate last 30 days
$date_i = 0;
$date = time();
	$date_formatted = date('M j',$date);
	$date_url = date('Y-m-d',$date);
if ($_GET['day'] != '') {
	$compare_date = $_GET['day'];
} else {
	$compare_date = $date_url;
}
while ($date_i < 30)  {
	$date_formatted = date('M j',$date);
	$date_url = date('Y-m-d',$date);
	$result = mysql_query('
		SELECT *
		FROM weight_loss_tracker_days
		WHERE days_date = "' . $date_url . '" and
				user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
	if (mysql_num_rows($result) < 1) {
		$cal = '-';
		$style = '';
	} else {
		$row = mysql_fetch_array($result);
		$cal = $row['days_total'];
		if ($cal > $user['user_bmr']) {
			$style = 'red';
		} else {
			$style = 'green';
		}
		$cal = $cal - $user['user_bmr'];
		if ($cal < 0) {
			$cal = $cal-$cal-$cal;
		}
	}
	if ($compare_date != $date_url) {
		$date_col .= '<tr><td><a href="index.php?day=' . $date_url . '">'. $date_formatted . '</a></td><td> (<span class="' . $style . '">' . $cal . '</span>)</td></tr>';
	} else {
		$date_col .= '<tr><td><span class="selected-date">' . $date_formatted . '</td><td> (<span class="' . $style . '">' . $cal . '</span>)</span></td></tr>';
	}
	$date = $date - 60*60*24;
	$date_i++;
}
$date_col = '<table class="noborder" cellspacing="0" cellpadding="0">' .$date_col . '</table>';

// Get food dropdown
$result = mysql_query('
	SELECT *
	FROM weight_loss_tracker_items
	WHERE user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"
	ORDER BY items_name ASC');
if (mysql_num_rows($result) > 0) {
	while ($row = mysql_fetch_array($result)) {
		$item_dropdown .= '<option value="' . $row['items_id'] . '">' . $row['items_name'] . ' (' . $row['items_cal'] . ')</option>';
	} 
}

$row_i = 1;
if ($item_dropdown != '') {
	while ($row_i <= $num_entries) {
		if ($item_dropdown != '') {
			$select_input = '<select name="food_item_' . $row_i . '"><option value=""></option>' . $item_dropdown . '</select>';
			$food_submit = '<br /><input type="submit" value="Add &gt;&gt;" />';
		}
		$food_table .= '
			<tr>
				<td>' . $row_i . '</td>
				<td>' . $select_input  . '</td>
			</tr>';
		$row_i++;
	}
} else {
	$food_table = '<p><strong><a href="add-foods.php">Click here to add foods to your food database.</a></strong></p>';
}

$food_table = '
	<table class="main-table wide">
		<form action="index.php?day=' . $_GET['day'] . '" method="post">
			<input type="hidden" name="action" value="add_foods" />
			' . $food_table . '</table>' . $food_submit;
			
// Get user info		
$result = mysql_query('
	SELECT *
	FROM weight_loss_tracker_users
	WHERE user_id  = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
$user = mysql_fetch_array($result);

// Get selected day's tally
$date = time();
$date_url = date('Y-m-d',$date);
if ($_GET['day'] != '') {
	$compare_date = $_GET['day'];
} else {
	$compare_date = $date_url;
}
$date = date('Y-m-d H:i:s',strtotime($compare_date));
$result = mysql_query('
	SELECT *
	FROM weight_loss_tracker_days
	WHERE days_date = "' . $date . '" and
		user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
	echo mysql_error();
if (mysql_num_rows($result) > 0) {
	$row = mysql_fetch_array($result);
	$cal_array = explode(',',$row['days_tally']);
	$total_cal_array = count($cal_array);
	$cal_i = 0;
	while (($cal_i+1) < $total_cal_array) {
		if ($cal_array[$cal_i] != '') {
			$result = mysql_query('
				SELECT *
				FROM weight_loss_tracker_items
				WHERE items_id = "' . $cal_array[$cal_i] . '"');
			$row = mysql_fetch_array($result);
			$today_table .= '<tr><td>' . $row['items_name'] . '</td><td>' . $row['items_cal'] . '</td></tr>';
			$cal_count = $cal_count + $row['items_cal'];
		}
		$cal_i++;
	}
	
//
	if ($today_table != '') {
		$today_table = '<table class="main-table wide">' . $today_table . '</table>';
		$clear_food = ' (<a href="index.php?day=' . $_GET['day'] . '&action=clear_food"  onclick="return confirm(\'Are you sure you want to clear this day?\')">Clear</a>)';
	} else {
		$today_table = '<p><strong>You have not entered any food data for today.</strong></p>';
	}
}


// Calculate last 30 days
$result = mysql_query('
	SELECT days_total
	FROM weight_loss_tracker_days
	WHERE user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '" and
	days_total > 0
	LIMIT 30');
$day_count = 0;
$tally = 0;
if (mysql_num_rows($result) > 0) {
	while ($row = mysql_fetch_array($result)) {
		$day_count++;
		$tally = $tally + $row['days_total'];
	}
	$average = $user['user_bmr'] * $day_count;
	$total_lost = $tally - $average;
	if ($total_lost > 0) {
		$more = 'more';
		$gain = 'gained';
		$poundage_lost = round($total_lost / 3500);
		$daily_loss = round($total_lost / $day_count);
		$style = 'red';
	} else {
		$total_lost = $total_lost -  $total_lost - $total_lost;
		$more = 'less';
		$gain = 'lost';
		$poundage_lost = round($total_lost / 3500);
		$daily_loss = round($total_lost / $day_count);
		$style = 'green';
	}
} else {
	$more = 'less';
	$gain = 'lost';
	$total_lost = 0;
	$daily_loss  = 0;
	$style = 'green';
	$poundage_lost = 0;
}
	$loss_display = '<strong>For instructions, click the "Instructions" link above</strong>. Over the last 30 days that have have tracked your caloric intake, you have taken in <strong><span class="' . $style  . '">' . $total_lost . ' ' . $more . ' calories</span></strong> than you burn. This is an average of <strong><span class="' . $style . '">' . $daily_loss . '</span></strong> calories a day. In terms of fat weight, you would have <strong><span class="' . $style . '">' . $gain . ' roughly ' . $poundage_lost . ' pounds</span></strong>.';

// Calculate all time
$result = mysql_query('
	SELECT days_total
	FROM weight_loss_tracker_days
	WHERE user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '" and
	days_total > 0');
$day_count = 0;
$tally = 0;
if (mysql_num_rows($result) > 0) {
	while ($row = mysql_fetch_array($result)) {
		$day_count++;
		$tally = $tally + $row['days_total'];
	}
	$average = $user['user_bmr'] * $day_count;
	$total_lost = $tally - $average;
	if ($total_lost > 0) {
		$more = 'more';
		$gain = 'gained';
		$poundage_lost = round($total_lost / 3500);
		$daily_loss = round($total_lost / $day_count);
		$style = 'red';
	} else {
		$total_lost = $total_lost -  $total_lost - $total_lost;
		$more = 'less';
		$gain = 'lost';
		$poundage_lost = round($total_lost / 3500);
		$daily_loss = round($total_lost / $day_count);
		$style = 'green';
	}
} else {
	$gain = 'lost';
	$poundage_lost = 0;
	$total_lost = 0;
}
$overall_loss_text = ' Since you started tracking your calories, you have <strong><span class="' . $style . '">' . $gain . ' ' . $total_lost . ' calories</span></strong> (' . $poundage_lost . ' pounds in fat weight)';

$loss_display = '<p>' . $loss_display  . $overall_loss_text .'</p>';



?>
<head>
	<title>Home Page</title>
	<link href="styles/default.css" rel="stylesheet" type="text/css"></link>
</head>
<body>
<div class="main">
<div class="nav-div"><a href="index.php">Home Page</a> | <a href="add-foods.php">Add Foods</a> | <a href="instructions.php">Instructions</a> | <a href="login-register.php?action=logout">Log Out</a></div>
<?php echo $result_div; ?>
<h1>Home Page</h1>
<?php echo $loss_display; ?>

<div class="bmr-div">
<form action="index.php?day=<?php echo $_GET['day']; ?>" method="post">
<input type="hidden" name="action" value="change_bmr" />
<table>
	<tr>
		<td>Your current BMR is set to <span class="bmr-span"><?php echo $user['user_bmr']; ?> calories</span></td>
		<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Set to:</td>
		<td><input type="text" name="new_bmr" size="7" maxlength="5" /></td>
		<td><input type="submit" value="Change &gt;&gt;" /></td>
	</tr>
</table>
</form>
</div><br />
<table class="super-table">
	<tr>
		<td class="half-width"></td>
		<td class="td-1" valign="top">
			<img src="images/spacer.gif" width="200" height="1" />
			<div class="td-heading-1">Select Date</div>Defaults to today
			<div class="date-col-div wide"><?php echo $date_col; ?></div><br />
		</td>
		<td class="td-2" valign="top">
			<img src="images/spacer.gif" width="200" height="1" />
			<div class="td-heading-2">Add Daily Foods</div> Eaten on selected day
			<?php echo $food_table; ?><br />
		</td>
		<td class="td-3" valign="top">
			<img src="images/spacer.gif" width="200" height="1" />
			<div class="td-heading-3">Foods Eaten</div>On this day <?php echo $clear_food; ?>
			<?php echo $today_table; ?><br />
		</td>
		<td class="half-width"></td>
	</tr>
</table>
<hr />
<div class="align-center"><a href="http://www.my-health-and-fitness.org" target="_blank">Weight Loss Tracker</a> is Powered by <a href="http://www.my-health-and-fitness.org" target="_blank">www.My-Health-and-Fitness.org</a></div></div>
</body>
Return current item: Weight Loss Tracker