Location: PHPKode > scripts > Weight Loss Tracker > weight-loss-tracker/add-foods.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 = 25;

$row_i = 1;
while ($row_i <= $num_entries) {
	$food_table .= '
		<tr>
			<td>' . $row_i . '</td>
			<td><input type="text" size="30" maxlength="50" name="food_name_' . $row_i . '" /></td>
			<td><input type="text" size="5" maxlength="5" name="food_cal_' . $row_i . '" /></td>
		</tr>';
	$row_i++;
}

if ($_POST['action'] == 'add_items') {
	$row_i = 1;
	$result = mysql_query('
		SELECT *
		FROM weight_loss_tracker_items
		WHERE user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
	$total = mysql_num_rows($result);
	while ($row_i <= $num_entries) {
		if ($_POST['food_name_' . $row_i] != '' && $_POST['food_cal_' . $row_i] > '0') {
			if ($total < $weightlosstracker_settings["max_items"]) {
				$name = removeInvalidChars($_POST['food_name_' . $row_i],'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 _-');
				$cal = removeInvalidChars($_POST['food_cal_' . $row_i],'0123456789');
				$insert = mysql_query('
					INSERT INTO weight_loss_tracker_items
					SET items_name = "' . $name . '",
						items_cal = "' . $cal . '",
						user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
				$total++;
				$results .= $comma . 'Added ' . $name;
				$comma = ', ';
			}
		}
		$row_i++;
	}
	if ($results != '') {
		$result_div .= getResultDiv($results,'success');
	} else {
		$result_div .= getResultDiv('No items were added');
	}
}

$food_table = '<table class="main-table">' . $food_table . '</table>';

$result = mysql_query('
	SELECT items_id
	FROM weight_loss_tracker_items
	WHERE user_id = "' . $_SESSION['weight_loss_tracker_user_id'] . '"');
$total = mysql_num_rows($result);
?>
<head>
	<title>Add Foods</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>
<h1>Add Food Items</h1>

<p>Use this form to add food items to your personal food database. Once you've added foods to your database, you can use them in your daily tally. You can add up to <strong><?php echo $weightlosstracker_settings["max_items"]; ?> items</strong>, and you currently have <strong><?php echo $total; ?></strong>.</p>
<?php echo $result_div; ?>
<form action="add-foods.php" method="post">
	<input type="hidden" name="action" value="add_items" />
	<input type="submit" value="Add &gt;&gt;" /><br /><br />
	<?php echo $food_table; ?><br />
	<input type="submit" value="Add &gt;&gt;" />
</form>
<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