<?php
  include "include.php";
  session_start();
?>
<!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>MobileSELECT</title>
  <!-- Ajax-->



   <link href="style.css" rel="stylesheet" type="text/css" />

  </head>

  <body>
  <?php
  
  //create array with values between commas
  $SelectedNeeds = explode(",", $_GET["array"]);
  $FeaturesArrayF = array();
  $FinalSuitablePhones = array();
  $PhonesF = array();
  $arrays = stripslashes($_GET["arrays"]);

	
	/* 2.b SAVE THE RANKING */
	//need to grab these values from the previous page

	//Get the length of the array
	$length = count($SelectedNeeds);
	
/* 3. FIND THE RELATED FEATURES */	
$con = get_database_connection();
  
// loop through the SelectedNeeds  
for($x=0;$x<$length;$x++)
{
$arrayValue = $SelectedNeeds[$x];

//find the related FeatureID for each selected Requirement
$sql = "SELECT FeatureID FROM FeatureRequirement WHERE RequirementID ='".$arrayValue."'";
$result = mysqli_query($con,$sql);


//place it in the Features Array
	while($row = mysqli_fetch_array($result)){
		$ID = $row['FeatureID'];
		if ($ID) array_push($FeaturesArrayF,$ID);
	}

}
foreach($FeaturesArrayF as $y){
		$searchQuery = "SELECT DISTINCT PhoneID FROM PhoneFeature WHERE FeatureID ='".$y."' AND Value = '1'";
		$result2 = mysqli_query($con,$searchQuery);

		while($row = mysqli_fetch_array($result2)){
			$FinalSuitablePhones[] = $row['PhoneID'];
		}
	}
		//find the length of the FeaturesArray (aka the number of related features)
		$noOfFeatures = count($FeaturesArrayF);
		
		//find the count of the PhoneIDs in the SuitablePhonesArray
		$PhoneIDOccurence = array_count_values($FinalSuitablePhones);
		
		
	
		//If the length is the same as the count we have a MATCH
		$PhonesF = array_keys($PhoneIDOccurence, $noOfFeatures);
	

//If there are no Selected Requirements fill the arrays with the results from the previous page
if ($SelectedNeeds[0]=="" && $SelectedNeeds[1]==""){
	$_SESSION['FinalMost'] = $_SESSION['Most'];
	$_SESSION['FinalWell'] = $_SESSION['Well'];
	$_SESSION['FinalSuit'] = $_SESSION['Suit'];
	
}
else {

$_SESSION['FinalMost'] = array_intersect($_SESSION['Most'],$PhonesF);
$_SESSION['FinalWell'] = array_intersect($_SESSION['Well'],$PhonesF);
$_SESSION['FinalSuit'] = array_intersect($_SESSION['Suit'],$PhonesF);


}

	foreach($_SESSION['FinalMost'] as $array_value){
		$query1String = "PhoneID = '".$array_value."' OR ";
		$string1 = $string1." ".$query1String;
	}
	
	
	
	//create Second Priority Query String
	foreach($_SESSION['FinalWell'] as $array_value){
	$query2String = "PhoneID = '".$array_value."' OR ";
	$string2 = $string2." ".$query2String;
	}
	
	foreach($_SESSION['FinalSuit'] as $array_value){
	$query3String = "PhoneID = '".$array_value."' OR ";
	$string3 = $string3." ".$query3String;
	}
	
	
	$phoneCountID = 0;
	//FIRST RANKED PHONES			
	
	$result = mysqli_query($con,"SELECT COUNT(PhoneID) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string1."PhoneID = '0' GROUP BY `Brand`");
	
	while($row = mysqli_fetch_array($result)){
		$phoneCountID++;
    $count = $row['PhoneCount'];
	$total = $total + $count;
	}
	echo "
	<div class='phone-panel'>
<div class='phone-listing'>
        <h3>Phone Recommendations</h3>
        <div class='most-suitable-box'>
        <div class='most-suitable-header'>
<div class='suitable-text'>First (".$total.")</div></div>
        <ul id='double' class='phone'>
            <span class='code-comment'><!--2 col--></span>";
	
	
	
	//Display the Brand Names, followed by the count of the models in brackets
	$result = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string1."PhoneID = '0' GROUP BY `Brand`");
	while($row = mysqli_fetch_array($result)){
			$count = $row['PhoneCount'];
			$brand = $row['Brand'];
			echo "<li class='phone'><a href='#' id='phonescroller.php?Brand=".$brand."&Priority=1b&arrays=".$arrays."'>".$brand." (".$count.")</a></li>";
			
			
	}
	
			echo"</ul>";
			
			
		
			
			

	 //SECOND RANKED PHONES 
	 $result2 = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string2."PhoneID = '0' GROUP BY `Brand`");
	
	while($row = mysqli_fetch_array($result2)){
		$phoneCountID++;
    $count = $row['PhoneCount'];
	$total2 = $total2 + $count;
	
	
			$result2b = mysqli_query($con,"SELECT PhoneID, Brand FROM `Phone`  WHERE ".$string2." PhoneID = '0' AND (`Brand` ='".$brand."')");
			
	//generate query strings that is used to display the phones in boxes
			while($row = mysqli_fetch_array($result2b)){
				$phoneid = $row['PhoneID'];
				$phonequerystring2 = $phonequerystring2."OR PhoneID = '".$phoneid."' ";
				$brand = $row['Brand'];
			}
			$phonequerystring2 = $phonequerystring2." ) AND Brand = '".$brand."'";
	}
	echo "<div class='well-suited-box'>
        <div class='well-suited-header'>
<div class='suitable-text'>Second (".$total2.")</div></div>
        <ul id='double' class='phone'>
            <span class='code-comment'><!--2 col--></span>";
			
			
	
			
			
	
	
	$result2c = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string2."PhoneID = '0' GROUP BY `Brand`");
	while($row = mysqli_fetch_array($result2c)){
			$count = $row['PhoneCount'];
			$brand = $row['Brand'];
			echo "<li class='phone'><a href='#' id='phonescroller.php?Brand=".$brand."&Priority=2b&arrays=".$arrays."'>".$brand." (".$count.")</a></li>";
			
	}
	
	
	
echo"</ul>";






 //THIRD RANKED PHONES 
	 
	 $result = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand FROM `Phone`  WHERE ".$string3."PhoneID = '0' GROUP BY `Brand`");
	
	while($row = mysqli_fetch_array($result)){
    $count = $row['PhoneCount'];
	$total3 = $total3 + $count;
	}
	echo "<div class='least-suited-box'>
        <div class='least-suited-header'>
<div class='suitable-text'>Third (".$total3.")</div></div>
        <ul id='double' class='phone'>
            <span class='code-comment'><!--2 col--></span>";
	
	
	$result = mysqli_query($con,"SELECT COUNT(*) AS PhoneCount, Brand, PhoneID FROM `Phone`  WHERE ".$string3."PhoneID = '0' GROUP BY `Brand`");
	while($row = mysqli_fetch_array($result)){
			$count = $row['PhoneCount'];
			$brand = $row['Brand'];
			$phoneid = $row['PhoneID'];
			echo "<li class='phone'><a href='#' id='phonescroller.php?Brand=".$brand."&Priority=3b&arrays=".$arrays."'>".$brand." (".$count.")</a></li>";
	}


	
	
	


echo"</ul></div></div>";	  
		  
echo "<div id='populate-phones'></div>";

echo "</div>";
	?>
    
</body>
</html>
