<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Page Three</title>
<?php

//verifying  session for user
session_start();
	if (isset($_SESSION['login']))
	{
		$username = $_SESSION['login'];
	}
	else{
		header("location: register.php");
	}
	
	
include("functions.php");
include("login.php");
include("LPScript.php");


//initializing variables
$message ="";
$portion = 2; //number of portion that a food can be splited into
$direction = "MIN"; //direction of linear programming
$array_food = $array_food_final = array();
$food_likes = $food_likes_neutral = $food_all = array();


//////// first food set to try to generate the meal plan = user's likes


//getting the user's likes
$query = "SELECT * FROM likes WHERE username = '".$username."' ";
$result = mysqli_query($db_connection, $query);
if (!$result) echo "<script type='text/javascript'>alert('Database query failed');</script>";
$row = array();
$a=0;
$numberLikes = mysqli_num_rows($result);

while($row[$a]=mysqli_fetch_assoc($result)){
	$a++;
}

//getting the all the foods
$query = "SELECT * FROM food_des";
$res = mysqli_query($db_connection, $query);
if (!$res) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
$b=0;

//creating set of foods with the user's likes
while($r=mysqli_fetch_assoc($res)){
	for($a=0;$a<count($row)-1;$a++){
		if($r['NDB_No']==$row[$a]['NDB_No']){
			$food_likes[$b]=$r;
			$b++;
		}
	}
}

$a=0;


//////// second food set to try to generate the meal plan = user's likes + neutral food (all foods but disliked foods)


//getting the user's dislikes
$query = "SELECT * FROM dislikes WHERE username = '".$username."' ";
$res = mysqli_query($db_connection, $query);
if (!$res) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
$dislike= mysqli_fetch_assoc($res);

//creating set of foods with all foods but disliked foods
if(mysqli_num_rows($res)==0){
	$query = "SELECT * FROM food_des";
}
else{
	$query = "SELECT * FROM food_des WHERE NDB_No != '".$dislike['NDB_No']."' ";
	while($dislike= mysqli_fetch_assoc($res)){
		$query .= "AND NDB_No != '".$dislike['NDB_No']."' ";
	}
}
$res = mysqli_query($db_connection, $query);
if (!$res) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
$a=0;
while($food_likes_neutral[$a] = mysqli_fetch_assoc($res)){
	$a++;
}


//////// third food set to try to generate the meal plan = all foods


//creating set of foods with all foods
$query = "SELECT * FROM food_des";
$res = mysqli_query($db_connection, $query);
if (!$res) echo "<script type='text/javascript'>alert('Database query failed');</script>"; 
$a=0;
while($food_all[$a] = mysqli_fetch_assoc($res)){
	$a++;
}
?>

<script language="javascript" type="text/javascript">


function enviar() {
	document.categorieForm.submit();
}

//set class name to focus (normal style to describe non-error)
function setStyleFocus(element)
{
	element.className="focus";
}
</script>
<link rel="stylesheet" type="text/css" href="format.css">
</head>
<body >
<!--The upper div creates the upper green block in the top of the webpage  -->
<div  id="upper" style="position:fixed;  top: 0; left:0;">
	<!--creates the 'Logout' button -->
    <div style="position:relative;text-align:left;float: left;"> <input type="button" value="Logout" onclick="location.href='logout.php'" /> </div>
    <!--creates the 'Previous Page' button -->
    <div align="right" style="position:relative;text-align:right;float:right;"><input name="previousPage" type="button" value="Previous Page" onclick="location.href='foods.php'" style=" margin-right:25px"/></div>

</div>
	  <form id="categorieForm" name="categorieForm" method="post" action="">
      
      
<!--The information div creates the green block in the center of the webpage  -->   
<div id="information" style="width: 70%">

<label for="Categories">
<?php

//adjusting sets before trying to generate the meal plan
unset($food_likes[(count($food_likes)-1)]);
unset($food_likes_neutral[(count($food_likes_neutral)-1)]);
unset($food_all[(count($food_all)-1)]);



////////trying first set


//initializing variables
$flag = false;
$solution = 0;
if(count($food_likes)>0){
echo "Meal plan based on what you like:<br />
<br />";
	do{
		//getting the set of foods and the portion number and trying to generate, using linear programming, a set of food that will satisfy the user’s nutrient ranges
		$message = lp($portion,$food_likes);
		
		//initializing variables		
		$values=array();
		$foods=array();
		$position=array();
		$i=0;
		while($message[$i]!="@"&&count($message)>0){
			$i++;
		}
		$b=0;
		
		//getting results generated by the lp function and saved in message variable
		for($i=$i+2;$i<count($message);$i=$i+2)
		{
			if($message[$i]!='0'){
				$values[$b]=$message[$i];
				$position = $message[$i+1];
				$foods[$b]=$food_likes[$message[$i+1]];
				$b++;
			}
		}
		
		//checking if this set of foods will satisfy the user’s nutrient ranges. 
		$flag = checkSolution($values,$foods,$portion,$direction);
		
		//changing the set a little bit
		if($flag){
			$solution++;
			$b=0;
			$array_food = $food_likes;
			$food_likes = array();
			for($i=0;$i<count($array_food);$i++){
				if($foods[0]["NDB_No"]!=$array_food[$i]["NDB_No"]&&$foods[1]["NDB_No"]!=$array_food[$i]["NDB_No"]){
					$food_likes[$b]=$array_food[$i];
					$b++;
				}
			}
		}
	//trying again in case of finding solution
	}while($flag&&$solution<3);	
}
if($solution==0) echo "<b>No solution found</b><br /><br />";



////////trying second set


//initializing variables
$flag = false;
$solution = 0;
if(count($food_likes_neutral)>0){
	echo "Meal plan based on what you like and what you don't dislike:<br />
<br />";
	do{
		
		//getting the set of foods and the portion number and trying to generate, using linear programming, a set of food that will satisfy the user’s nutrient ranges
		$message = lp($portion,$food_likes_neutral);
		
		//initializing variables		
		$values=array();
		$foods=array();
		$position=array();
		$i=0;
		while($message[$i]!="@"&&count($message)>0){
			$i++;
		}
		$b=0;
		
		//getting results generated by the lp function and saved in message variable
		for($i=$i+2;$i<count($message);$i=$i+2)
		{
			if($message[$i]!='0'){
				$values[$b]=$message[$i];
				$position = $message[$i+1];
				$foods[$b]=$food_likes_neutral[$message[$i+1]];
				$b++;
			}
		}
		
		//checking if this set of foods will satisfy the user’s nutrient ranges. 
		$flag = checkSolution($values,$foods,$portion,$direction);
		
		//changing the set a little bit
		if($flag){
			$solution++;
			$b=0;
			$array_food = $food_likes_neutral;
			$food_likes_neutral = array();
			for($i=0;$i<count($array_food);$i++){
				if($foods[0]["NDB_No"]!=$array_food[$i]["NDB_No"]&&$foods[1]["NDB_No"]!=$array_food[$i]["NDB_No"]){
					$food_likes_neutral[$b]=$array_food[$i];
					$b++;
				}
			}
		}
	//trying again in case of finding solution
	}while($flag&&$solution<3);	
}
if($solution==0) echo "<b>No solution found</b><br /><br />";



////////trying third set


//initializing variables
$flag = false;
if(count($food_all)>0&&$solution==0){
echo "Meal plan based on all foods, including your dislikes:<br />
<br />";
	$solution = 0;
	do{
		
		//getting the set of foods and the portion number and trying to generate, using linear programming, a set of food that will satisfy the user’s nutrient ranges
		$message = lp($portion,$food_all);
		
		//initializing variables		
		$values=array();
		$foods=array();
		$position=array();
		$i=0;
		while($message[$i]!="@"&&count($message)>0){
			$i++;
		}
		$b=0;
		
		//getting results generated by the lp function and saved in message variable
		for($i=$i+2;$i<count($message);$i=$i+2)
		{
			if($message[$i]!='0'){
				$values[$b]=$message[$i];
				$position = $message[$i+1];
				$foods[$b]=$food_all[$message[$i+1]];
				$b++;
			}
		}
		
		//checking if this set of foods will satisfy the user’s nutrient ranges. 
		$flag = checkSolution($values,$foods,$portion,$direction);
		
		//changing the set a little bit
		if($flag){
			$solution++;
			$b=0;
			$array_food = $food_likes_neutral;
			$food_all = array();
			for($i=0;$i<count($array_food);$i++){
				if($foods[0]["NDB_No"]!=$array_food[$i]["NDB_No"]&&$foods[1]["NDB_No"]!=$array_food[$i]["NDB_No"]){
					$food_all[$b]=$array_food[$i];
					$b++;
				}
			}
		}
	//trying again in case of finding solution
	}while($flag&&$solution<3);	
}
if($solution==0) echo "<b>No solution found</b><br /><br />";

		
		?><br />
        </label>

	</div>
            </form>
</body>
</html>